这种错误通常是因为集成 playground 没有正确设置“cookieDomain”,导致不能在本地使用 cookies。如果使用的是 @apollo/gateway 软件包,可以尝试下面的方法解决:
添加以下配置选项到你的 gateway 配置中:
new ApolloGateway({
// ...
playground: {
settings: {
'request.credentials': 'same-origin',
'schema.polling.enable': false,
'schema.polling.endpointFilter': '*localhost*',
'trace.hideTracingResponse': true,
},
},
});
这将配置集成 playground,以使用“同源”配置请求 credentials,并启用“禁用架构轮询”。
如果你使用的是其他软件包,可以尝试创建一个高级中间件,在一个新请求上设置 cookieDomain,如下所示:
const setCookieDomainMiddleware = (req, res, next) => {
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Headers', 'X-Requested-With,Content-Type');
res.header('Access-Control-Allow-Methods', 'POST, GET, OPTIONS');
res.header('Access-Control-Allow-Credentials', 'true');
res.header(
'Set-Cookie',
`__SID=${req.sessionID}; HttpOnly; SameSite=None; Secure; Domain=localhost`
);
next();
};
当使用集成 playground 时,请确保在使用“localhost”时使用下划线前缀。