0

Here is my graphql query:

query {
  login(email:"please@work.com", password:"testing321"){
    token
  }
}

Here is the response:

{
  "errors": [
    {
      "message": "user is not defined",
      "locations": [
        {
          "line": 2,
          "column": 3
        }
      ],
      "path": [
        "login"
      ]
    }
  ],
  "data": null
}

Here is the graphql schema:

const { buildSchema } = require("graphql");

module.exports = buildSchema(`
        type AuthData {
            userId: ID!
            token: String!
            tokenExpiration: Int!
        }
        input UserInput {
            email: String!
            password: String!
        }

        type RootQuery {
            events: [Event!]!
            bookings: [Booking!]!
            login(email: String!, password: String!): AuthData!
        }

        type RootMutation {
            createEvent(eventInput: EventInput): Event
            createUser(userInput: UserInput): User
            bookEvent(eventId: ID!): Booking!
            cancelBooking(bookingId: ID!): Event!
        }

        schema {
            query: RootQuery
            mutation: RootMutation
        }
    `)

Here is the function that is passed to the root value:

const bcrypt = require("bcryptjs");
const jwt = require("jsonwebtoken");
const User = require("../../models/user");
module.exports = {
    login: async ({ email, password }) => {
        try {
            const user = await User.findOne({ email:email });
            if (!user) {
                throw new Error("User does not exist");
            }
            const isEqual = await bcrypt.compare(password, user.password);
            if (!isEqual) {
                throw new Error("Password is not correct");
            }
        }catch(err){
            throw err;
        }
        const token = jwt.sign({userId: user.id, email: user.email},
             "somesecretkey", 
            {expiresIn: "1h"});
        return { userId: user.id, token: token, tokenExpiration: 1};
    }
}

All other queries and mutations to MongoDB work and return the correct values. What could the problem be? Is this a bug with graphql or did I make a mistake somewhere? PS: I am following a tutorial

Alan Karanja
  • 51
  • 1
  • 3
  • The error is originating would be inside whatever functions you passed to your root value, so we'd need to see that and any related code. – Daniel Rearden Apr 07 '20 at 15:35
  • As an aside, you [probably shouldn't be using buildSchema](https://stackoverflow.com/questions/53984094/notable-differences-between-buildschema-and-graphqlschema/53987189#53987189). – Daniel Rearden Apr 07 '20 at 15:36

0 Answers0