要解决Apollo-Client和Express-Graphql后端会话不持久化的问题,可以尝试以下步骤:
const cors = require('cors');
app.use(cors());
import { ApolloClient, HttpLink, InMemoryCache } from '@apollo/client';
import { setContext } from '@apollo/client/link/context';
const httpLink = new HttpLink({
uri: 'http://localhost:4000/graphql',
credentials: 'include',
});
const client = new ApolloClient({
link: httpLink,
cache: new InMemoryCache(),
});
const { ApolloServer } = require('apollo-server-express');
const server = new ApolloServer({
typeDefs,
resolvers,
context: ({ req, res }) => ({ req, res }),
});
这些步骤应该能够解决Apollo-Client和Express-Graphql后端会话不持久化的问题。记得在设置跨域请求和会话管理时,遵循安全最佳实践来保护用户的身份验证信息。