Как получить доступ к контексту в GraphQL из ваших резольверов - PullRequest
1 голос
/ 10 июля 2019

Я просто хочу отправить запрос всем своим распознавателям через контекстное поле, но когда я получаю к нему доступ от одного из моих распознавателей, он возвращает ноль.

app.use('/graphql', graphqlHTTP(async (request, response, graphQLParams) => ({
  schema: schema,
  context:{token_1:null,test:request},
  graphiql:true
})));

Это часть моей схемы. Сначала я вхожу в систему, чтобы установить токен, но когда я хочу получить доступ к context.token_1 из другого преобразователя (BuyItems), он возвращает ноль.

  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}
    }
  }
...