设置 Redis cookie 时,Apollo 客户端未连接到服务器
Posted
技术标签:
【中文标题】设置 Redis cookie 时,Apollo 客户端未连接到服务器【英文标题】:Apollo client not connecting to server when Redis cookie is set 【发布时间】:2021-12-20 05:51:03 【问题描述】:当我尝试登录设置 cookie 然后刷新页面时,我没有从 apollo 服务器得到任何响应,并且 graphql 客户端发出的每个请求都保持(待处理)状态。 删除 cookie 后,一切似乎都正常。我什至不确定如何调试它并且对后端有一点经验,所以任何建议都会有所帮助。
这是我从客户端设置连接的方式:
const link = createHttpLink(
uri: 'http://localhost:5000/graphql',
credentials: 'include',
);
const apolloLink = ApolloLink.from([
errorLink,
link
]);
const apolloClient = new ApolloClient(
cache: new InMemoryCache(),
link: apolloLink,
);
和服务器:
useContainer(Container);
const establishDatabaseConnection = async (): Promise<void> =>
try
await createDatabaseConnection();
catch (error)
console.error(error);
;
const initExpressGraphql = async () =>
const app = express();
const redis = new Redis();
const RedisStore = connectRedis(session);
const corsOptions =
origin: 'http://localhost:3000',
credentials: true,
;
const schema = await buildSchema(
resolvers: RESOLVERS,
container: Container,
);
const apolloServer = new ApolloServer(
schema: schema as GraphQLSchema,
context: ( req, res : any) => ( req, res ),
introspection: true,
plugins: [
ApolloServerLoaderPlugin(
typeormGetConnection: getConnection, // for use with TypeORM
),
],
);
app.use(
session(
store: new RedisStore(
client: redis as any,
),
name: 'rds',
secret: 'verysecretdata',
resave: false,
saveUninitialized: false,
cookie:
httpOnly: true,
secure: process.env.NODE_ENV === 'production',
maxAge: 1000 * 60 * 60 * 24 * 7 * 365, // 7 years
sameSite: 'lax',
,
)
);
apolloServer.applyMiddleware(
app,
cors: corsOptions
)
const PORT = process.env.PORT || 5000;
app.listen(PORT, () => console.log(`Server started on port $PORT`));
;
const startServer = async (): Promise<void> =>
await establishDatabaseConnection();
initExpressGraphql();
;
startServer();
【问题讨论】:
【参考方案1】:重启redis服务器后问题解决了
【讨论】:
只要没有人回答,您就应该删除它,而不是自己回答您的问题。例如,请参阅meta.***.com/questions/300117/…以上是关于设置 Redis cookie 时,Apollo 客户端未连接到服务器的主要内容,如果未能解决你的问题,请参考以下文章
如何使用 Apollo 从 GraphQL Mutation 设置 Auth token cookie
如何从 graphQL apollo 突变或查询上的 cookie 更新授权标头