Graphql Apollo设置cookies不起作用

Graphql Apollo设置cookies不起作用,cookies,graphql,apollo,Cookies,Graphql,Apollo,我想在阿波罗客户端设置cookies。 我正在设置工作正常的响应cookie,同时在graphql Playerd上设置“request.credentials”:“同源”或“包含”,并成功存储cookie。 当我试图从FE应用程序(React,Apollo客户端)存储cookie时,我的问题就开始了。当我输入凭据:“同一来源”时,应用程序正在运行,但cookies未保存。当我使用“include”时,查询被cors阻止 前端 const RouterComponent: FC = () =&g

我想在阿波罗客户端设置cookies。 我正在设置工作正常的响应cookie,同时在graphql Playerd上设置“request.credentials”:“同源”或“包含”,并成功存储cookie。 当我试图从FE应用程序(React,Apollo客户端)存储cookie时,我的问题就开始了。当我输入凭据:“同一来源”时,应用程序正在运行,但cookies未保存。当我使用“include”时,查询被cors阻止

前端

const RouterComponent: FC = () => {
  const cache = useMemo(() => {
    return new InMemoryCache();
  }, []);

  const client = useMemo(() => {
    const token = localStorage.getItem('token') || 'token';
    const httpLink = createHttpLink({
      uri: 'http://localhost:4000/graphql',
      credentials: 'include',
      headers: {
        authorization: token ? `Bearer ${token}` : '',
      },
    });

    const link = from([
      httpLink,
      createUploadLink({
        uri: 'http://localhost:4000/graphql',
      }),
    ]);
    return new ApolloClient({
      link,
      cache,
      typeDefs,
    });
  }, [cache]);

  return (
    <ApolloProvider client={client}>
      <Switch>
        <Route exact path="/" component={MainPage} />;
        <Route exact path="/:category" component={Category} />;
      </Switch>
    </ApolloProvider>
  );
};
SERVER.TS

const{ApolloServer}=require('apolloserverexpress');
const{typeDefs}=require('./typeDefs');
const{resolvers}=require('./resolvers');
const express=require('express');
const cors=需要('cors');
const path=require('path');
const bodyParser=require('body parser');
const mongoose=需要(“mongoose”);
const dotenv=require('dotenv');
const MONGO_CONNECTION=“”
常量app=express();
导出默认值(异步函数(){
试一试{
等待猫鼬。连接(猫鼬连接{
useNewUrlParser:true,
useUnifiedTopology:true})
const server=新服务器({
typeDefs,
解析器,
上下文:({req,res}:any)=>{
返回{
请求,
物件
};
},
});
常数corsConfig={
来源:“*”,
证书:正确,
AllowedHeader:['Content-Type','Authorization'],
};
dotenv.config();
应用程序使用(功能(请求:任意、回复:任意、下一步:任意){
res.setHeader('Access-Control-Allow-Origin','*');
res.setHeader('Access-Control-Allow-Methods'、'GET、POST、OPTIONS、PUT、PATCH、DELETE');
res.setHeader('Access-Control-Allow-Headers','X-request-With,content-type');
res.setHeader('Access-Control-Allow-Credentials',true);
next();
});
use(bodyParser.json());
use(bodyParser.urlencoded({extended:false}));
app.use('/images',express.static(path.join('images'));
const dir=path.join(process.cwd(),'images');
应用程序使用(快速静态(dir));
应用程序使用(express.static('public'))
app.use(express.static('files'))
applyMiddleware({app,cors:corsConfig});
app.listen({port:4000},()=>

console.log(`
包括
,源代码!=*?,如果我放在那里本地主机url相同结果
包括
,源代码!=*?,如果我放在那里本地主机url相同结果
games: (_: any, args: any, {req, res}: any) => {
        const refresh_token = sign({userId: "123"}, "secret", {expiresIn: "7d"})
        const access_token = sign({userId: "123"}, "secret", {expiresIn: "15min"})

        res.cookie('refresh-token', refresh_token, {expire: 60 * 60 * 24 * 7}) // 7 days
        res.cookie('access-token', access_token, {expire: 60 * 15}) // 15 mins

        return Game.find()
      }