Cookies 必须由服务器设置。这将使用这样的快速响应对象来完成。确保它可以从您的 gql 上下文中访问。
res.cookie('X-Refresh-Token', refreshToken, {
maxAge: 900000, // 15 minutes in milliseconds
httpOnly: true, // ensure the cookie will not be exposed
secure: true, // set this in production to ensure HTTPS
sameOrigin: 'none' // set this in production if the server is separate domain
})
然后,您的客户端必须启用凭据。对于 apollo-boost,这将是:
new ApolloClient({
uri: 'http://localhost:8080',
credentials: 'include', // 'same-origin' if your server is same domain
})
为了确保 Cookie 在 GraphQL Playground 中传递:
new ApolloServer({
context: ({ req, res }) => ({ req, res }),
playground: {
settings: {
'request.credentials': 'include',
},
},
})
如果您还没有使用它,cookie parser middleware 可能会派上用场。