1

I just want to send the request to all my resolvers through the context field, but when I access it from one of my resolvers, it returns null.

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

These are part of my schema. Firstly I Login to set the token ,but when I want to access the context.token_1 from the other resolver (BuyItems), it returns null.

  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}
    }
  }
Herku
  • 7,198
  • 27
  • 36
ZoicanDenis
  • 21
  • 1
  • 7

0 Answers0