"code": "BAD_USER_INPUT", In apollo-server error

Viewed 2067

I am using MERNG stack for developing the application. I am getting this error in apollo-server playground as

{
  "errors": [
    {
      "message": "Errors",
      "locations": [
        {
          "line": 2,
          "column": 3
        }
      ],
      "path": [
        "register"
      ],
      "extensions": {
        "errors": {},
        "code": "BAD_USER_INPUT",
        "exception": {
          "errors": {},
          "stacktrace": [
            "UserInputError: Errors",

In the project all the user inputs are in string my typeDef.js looks like this

const { gql } = require("apollo-server");

module.exports = gql`
  type Post {
    id: ID!
    body: String!
    createdAt: String!
    username: String!
  }
  type User {
    id: ID!
    email: String!
    token: String!
    username: String!
    createdAt: String!
  }
  input RegisterInput {
    username: String!
    password: String!
    confirmPassword: String!
    email: String!
  }
  type Query {
    getPosts: [Post]
    getPost(postId: ID!): Post
  }
  type Mutation {
    register(registerInput: RegisterInput): User!
    login(username:String!, password:String!):User!
  }
`;

The resolver for the mutation registerInput looks like this

const bcrypt = require("bcryptjs");
const jwt = require("jsonwebtoken");
const { UserInputError } = require("apollo-server");

const User = require("../../models/User");
const { SECRET_KEY } = require("../../config");
const {
  validateRegisterInput,
  validateLogin,
} = require("../../utils/validators");
module.exports = {
  Mutation: {
    async register(
      _,
      { registerInput: { username, password, confirmPassword, email } },
      context,
      info
    ) {
      //validate the users
      const { errors, valid } = validateRegisterInput(
        username,
        password,
        confirmPassword,
        email
      );
      if (!valid) {
        throw new UserInputError("Errors", { errors });
      }
      const user = await User.findOne({ username });
      if (user) {
        throw new UserInputError("username exists", {
          errors: {
            username: "username is taken",
          },
        });
      }

      password = await bcrypt.hash(password, 12);
      const newUser = User({
        username,
        email,
        password,
        createdAt: new Date().toString(),
      });

      const res = await newUser.save();

      const token = jwt.sign(
        {
          id: user.id,
          email: user.email,
          username: user.username,
        },
        SECRET_KEY,
        { expiresIn: "1hr" }
      );
      return {
        ...res._doc,
        id: res._id,
        token,
      };
    },
  },
};

I have defined all user validation in other file.

when I query register mutation in playground its throwing error as above. The query is looks like this

mutation{
  register(
    registerInput:{
      username:"user"
      password:"12345678"
      confirmPassword:"12345678"
      email:"hello@gmail.com"
      
    }
  ){
    id
   username
    token
    email
    
  }
}

I know we will get BAD_USER_INPUT error if the GraphQL operation includes an invalid value for a field argument. But I think I have given correct input values for the register.

Can somebody help with what's wrong in the code ?

0 Answers
Related