【发布时间】:2020-06-28 09:08:36
【问题描述】:
我正在使用 graphql-yoga,并且正在编写访问 Microsoft Graph API 的 cookie。在前端,我有一个安装了 NextJS 的 apollo 客户端,它运行良好……正在开发中。当我部署服务器时,前端根本无法识别 cookie。在我的阅读中,我认为这与 NextJS 被服务器渲染有关(即使当我运行下一个构建时它说静态构建......)我确定问题出在此处(我将 cmets 留在里面,以显示我尝试将凭据设置为“包含”的所有地方)
export default function createApolloClient(initialState, ctx) {
// The `ctx` (NextPageContext) will only be present on the server.
// use it to extract auth headers (ctx.req) or similar.
return new ApolloClient({
ssrMode: Boolean(ctx),
link: new HttpLink({
fetch,
uri: process.env.NODE_ENV === 'development' ? endpoint : prodEndpoint,
credentials: 'include', // Additional fetch() options like `credentials` or `headers`
fetchOptions: {
credentials: 'include',
},
// request: operation => {
// operation.setContext({
// fetchOptions: {
// credentials: 'include',
// },
// });
// },
}),
connectToDevTools: true,
// credentials: 'include',
cache: new InMemoryCache().restore(initialState),
});
}
其他答案都涉及 CORS,但我在我的 GraphQL-Server 上设置了 CORS:
const opts = {
debug: process.env.NODE_ENV === 'development',
cors:
process.env.NODE_ENV === 'development'
? {
credentials: true,
origin: ['http://localhost:3000'],
}
: {
credentials: true,
origin: [
'...'
],
},
};
server.start(opts, () =>
console.log('Playground is running on http://localhost:4000'),
);
谁能指出我正确的方向?我查看前端的 ApolloClient 部分是否正确?提前致谢。
【问题讨论】:
标签: next.js apollo-client express-graphql