1

在升级之前我们有

import express from 'express';
import { graphqlExpress, graphiqlExpress } from 'apollo-server-express';

const app = express();

app.use(
   '/graphql',
   bodyParser.json(),
   graphqlExpress(req => ({
   schema,
   tracing: true,
   context: { req },
 })),
);

app.use(
 '/graphiql',
 graphiqlExpress({
   endpointURL: '/graphql',
 }),
);

在我们的解析器中,我们可以获得 req 并设置 req.session.token 如下,

const customResover = {
Query: {
   custom: async (root, args, context) => {
   console.log(' resolver called with args', args);
   const { req } = context;  
   ... fetch token info and set 
   req.session.token = ${token};
   ...

但是随着升级到 2.0.0 版,代码更改为以下代码,我不确定如何修复 CustomResolver,设置会话令牌,知道如何完成上述操作吗?

import express from 'express';
import { ApolloServer, gql } from 'apollo-server-express';
import { typeDefs, resolvers } from './schema/';

const app = express();
const apollo = new ApolloServer({
   typeDefs
   resolvers,
   engine: false
});

apollo.applyMiddleware({
   app,
});
4

1 回答 1

1

https://www.apollographql.com/docs/apollo-server/migration-two-dot.html#request-headers

const apollo = new ApolloServer({
  typeDefs
  resolvers,
  context: ({ req }) => ({ req })
  engine: false
});

解决了它,但是 Cookie 出现了问题,令牌无法进入浏览器。

于 2018-08-15T05:04:03.653 回答