如何从解析器访问GraphQL中的上下文

如何从解析器访问GraphQL中的上下文,graphql,express-graphql,Graphql,Express Graphql,我只想通过上下文字段将请求发送给所有解析程序,但当我从其中一个解析程序访问它时,它返回null app.use('/graphql', graphqlHTTP(async (request, response, graphQLParams) => ({ schema: schema, context:{token_1:null,test:request}, graphiql:true }))); BuyItems :{ type: UserType,

我只想通过上下文字段将请求发送给所有解析程序,但当我从其中一个解析程序访问它时,它返回null

app.use('/graphql', graphqlHTTP(async (request, response, graphQLParams) => ({
  schema: schema,
  context:{token_1:null,test:request},
  graphiql:true
})));
  BuyItems :{
      type: UserType,
      args: {
        name: {type: GraphQLString},
        points: {type: GraphQLInt}
      },
      resolve(parent,args,context){
        console.log(context.token_1)
        return UserModel.findOneAndUpdate({name:args.name},{points:args.points})
      }
  },
  Login: {
      type: AuthType,
      args: {
        email: {type:GraphQLString},
        password: {type:GraphQLString}
      },
      async resolve(parent,args,context){
        const user = await UserModel.findOne({ email: args.email });
        if (!user) {
          throw new Error('User does not exist on login!');
        }
        const isEqual = await bcrypt.compare(args.password, user.password);
        if (!isEqual) {
          throw new Error('Password is incorrect!');
        }
        const token = jwt.sign(
          { userId: user.id, email: user.email },
          'somesupersecretkey',
          { expiresIn: '1h' }
        );
        context.token_1 = token;
        return {tokenExpiration: 1, userId: user.id, token:token}
    }
  }
这些是我的模式的一部分。首先,我登录设置令牌,但是当我想从其他解析器(BuyItems)访问context.token_1时,它返回null

app.use('/graphql', graphqlHTTP(async (request, response, graphQLParams) => ({
  schema: schema,
  context:{token_1:null,test:request},
  graphiql:true
})));
  BuyItems :{
      type: UserType,
      args: {
        name: {type: GraphQLString},
        points: {type: GraphQLInt}
      },
      resolve(parent,args,context){
        console.log(context.token_1)
        return UserModel.findOneAndUpdate({name:args.name},{points:args.points})
      }
  },
  Login: {
      type: AuthType,
      args: {
        email: {type:GraphQLString},
        password: {type:GraphQLString}
      },
      async resolve(parent,args,context){
        const user = await UserModel.findOne({ email: args.email });
        if (!user) {
          throw new Error('User does not exist on login!');
        }
        const isEqual = await bcrypt.compare(args.password, user.password);
        if (!isEqual) {
          throw new Error('Password is incorrect!');
        }
        const token = jwt.sign(
          { userId: user.id, email: user.email },
          'somesupersecretkey',
          { expiresIn: '1h' }
        );
        context.token_1 = token;
        return {tokenExpiration: 1, userId: user.id, token:token}
    }
  }

您已将令牌设置为空值,但我更改了“Login”解析程序中的context.token_1值。这不会更改所有冲突解决程序的值?我认为您不能更改一个冲突解决程序中的上下文值,而期望其他冲突解决程序中的值,请尝试使用令牌回调。或者使用express set and get函数。医生,谢谢!set和get函数是一个很好的选择:Dyou已将令牌设置为空值,但我更改了“Login”解析程序中的context.token_1值。这不会更改所有冲突解决程序的值?我认为您不能更改一个冲突解决程序中的上下文值,而期望其他冲突解决程序中的值,请尝试使用令牌回调。或者使用express set and get函数。医生,谢谢!set和get函数是一个很好的选择:D