【问题标题】:upgrading to apollo-server-express 2.0.0 context missing升级到 apollo-server-express 2.0.0 缺少上下文
【发布时间】:2019-01-21 23:07:00
【问题描述】:

升级前我们有

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,
});

【问题讨论】:

    标签: express graphql apollo-server express-graphql


    【解决方案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 出现问题,令牌无法进入浏览器。

    【讨论】:

      猜你喜欢
      • 2020-10-05
      • 1970-01-01
      • 1970-01-01
      • 2019-03-15
      • 1970-01-01
      • 2022-09-26
      • 1970-01-01
      • 2019-03-16
      • 1970-01-01
      相关资源
      最近更新 更多