res.cookie不适用于apollo server express

res.cookie不适用于apollo server express,express,graphql,apollo-client,apollo-server,Express,Graphql,Apollo Client,Apollo Server,我一直在尝试将cookie从服务器发送回客户端。我得到了响应数据,但在响应头中没有看到“set cookie” 我的阿波罗服务器配置: const server = new ApolloServer({ typeDefs, resolvers, context: ({ req, connection, res }) => ({ dummyModels: dummyModels, models: models, req, connection,

我一直在尝试将cookie从服务器发送回客户端。我得到了响应数据,但在响应头中没有看到“set cookie”

我的阿波罗服务器配置:

const server = new ApolloServer({
  typeDefs,
  resolvers,
  context: ({ req, connection, res }) => ({
    dummyModels: dummyModels,
    models: models,
    req,
    connection,
    res,
    currentUser: dummyModels.users[2],
    dummyUsers: dummyModels.dummyUsers,
  }),
});

app.use(cors({
  credentials: true,
  origin: 'http://localhost:3000',  
  // preflightContinue: true,
}));
const httpLink = createHttpLink({
  uri: 'http://172.16.16.130:4000/graphql',
  credentials: 'include',
  fetchOptions: {
    credentials: 'include',
  },
});

const wsLink = new WebSocketLink({
  uri: 'ws://172.16.16.130:4000/graphql',
  options: {
    reconnect: true,
    connectionParams: {
      headers: {
        'x-user-header': localStorage.getItem('userObject'),
      },
    },
  }
});

const terminatingLink = split(
  // split based on operation type
  ({ query }) => {
    const { kind, operation } = getMainDefinition(query);
    return kind === 'OperationDefinition' && operation === 'subscription';
  },
  wsLink,
  httpLink,
);

const link = ApolloLink.from([terminatingLink]);

const cache = new InMemoryCache();

export const client = new ApolloClient({
    link,
    cache,
});
我的解析器:

login: async (parent, args, context) => {
      const _include_headers = function(body, response, resolveWithFullResponse) {
        return {'headers': response.headers, 'data': body};
      };

      const loginRequestOptions = {
        method: 'POST',
        uri: 'http://localhost:3000/incorta/authservice/login',
        qs: {
          // access_token: 'xxxxx xxxxx', // -> uri + '?access_token=xxxxx%20xxxxx'
          user: args.input.username,
          pass: args.input.password,
          tenant: args.input.tenantName,
        },
        transform: _include_headers,
        json: true // Automatically parses the JSON string in the response
      };

      const loginResponse = await request(loginRequestOptions);
      console.log(loginResponse);

      context.res.cookie(
        'JSESSIONID',
        tough.Cookie.parse(loginResponse.headers['set-cookie'][0]).value,
        {
          // expires  : new Date(Date.now() + 9999999),
          // path: '/incorta/',
          // HttpOnly: false,
          // maxAge: 1000 * 60 * 60 * 24 * 99, // 99 days
        },
      );
      context.res.setHeader('Access-Control-Allow-Origin', 'http://localhost:3000');

      return loginResponse.data;
    },
注意:我使用request-promise-native发出请求

我的Apollo客户端配置:

const server = new ApolloServer({
  typeDefs,
  resolvers,
  context: ({ req, connection, res }) => ({
    dummyModels: dummyModels,
    models: models,
    req,
    connection,
    res,
    currentUser: dummyModels.users[2],
    dummyUsers: dummyModels.dummyUsers,
  }),
});

app.use(cors({
  credentials: true,
  origin: 'http://localhost:3000',  
  // preflightContinue: true,
}));
const httpLink = createHttpLink({
  uri: 'http://172.16.16.130:4000/graphql',
  credentials: 'include',
  fetchOptions: {
    credentials: 'include',
  },
});

const wsLink = new WebSocketLink({
  uri: 'ws://172.16.16.130:4000/graphql',
  options: {
    reconnect: true,
    connectionParams: {
      headers: {
        'x-user-header': localStorage.getItem('userObject'),
      },
    },
  }
});

const terminatingLink = split(
  // split based on operation type
  ({ query }) => {
    const { kind, operation } = getMainDefinition(query);
    return kind === 'OperationDefinition' && operation === 'subscription';
  },
  wsLink,
  httpLink,
);

const link = ApolloLink.from([terminatingLink]);

const cache = new InMemoryCache();

export const client = new ApolloClient({
    link,
    cache,
});
我尝试过修补选项。我不知道我缺少了什么。

您可以使用该软件包在apollo server中设置cookies

在您的解析器中,使用方法非常简单:

context.setCookies.push({
名称:“cookieName”,
值:“cookieContent”,
选项:{
域名:“example.com”,
到期日期:新日期(“2021-01-01T00:00:00”),
httpOnly:true,
最大年龄:3600,
路径:“/”,
sameSite:没错,
安全:正确
}
});