пользователь не определен Express -GraphQl Ошибка - PullRequest
0 голосов
/ 07 апреля 2020

Вот мой запрос graphql:

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

Вот ответ:

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

Вот схема graphql:

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
        }
    `)

Вот функция, которая передается в значение root:

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

Все остальные запросы и мутации в MongoDB работают и возвращают правильные значения. В чем может быть проблема? Это ошибка в graphql или я где-то ошибся? PS: я следую учебнику

...