3

I am writing mutation for logging in user in NodeJS.

It is giving error "Must Provide Name".

Here is Browser GraphQL Query:

mutation{
  login(username:"dfgdfg",password:"test1234") {
    _id,
    name{
      fname,
      lname,
      mname
    }
  }
}

Here is my code

    const login = {
    type: UserType,
    args: {
        input:{
            name:'Input',
            type: new GraphQLNonNull(new GraphQLObjectType(
                {
                    username:{
                    name:'Username',
                    type: new GraphQLNonNull(GraphQLString)
                    },
                    password:{
                    name:'Password',
                    type: new GraphQLNonNull(GraphQLString)
                    }
                }
            ))

        }
    },
    resolve: async (_, input, context) => {
        let errors = [];
        return UserModel.findById("5b5c34a52092182f26e92a0b").exec();

    }
  }

module.exports = login;

Could anyone please help me out why it is giving error?

Thanks in advance.

Piyush Bansal
  • 1,635
  • 4
  • 16
  • 39
  • 1
    can you show your mutation or the query from the browser – Ashh Aug 04 '18 at 08:21
  • I updated the question and put graphQL browser mutation query. Please check. – Piyush Bansal Aug 04 '18 at 08:25
  • try to pass `name` in input parameter not in mutation `login` function parameter and mutation should be like this `mutation{ login(input: $input) { _id, name{ fname, lname, mname } } }` – Ashh Aug 04 '18 at 08:36

1 Answers1

5

It is also very helpful to describe where the error occurs. I assume it is thrown when you start the node server.

This specific error is thrown because you are missing the name property in line 8 of the object config. Also this type needs to be GraphQLInputObjectTypenot GraphQLObjectType.

args: {
    input: {
        type: new GraphQLNonNull(new GraphQLInputObjectType({
            name: 'LoginInput',
            fields: {
                username:{
                    name:'Username',
                    type: new GraphQLNonNull(GraphQLString)
                },
                password:{
                    name:'Password',
                    type: new GraphQLNonNull(GraphQLString)
                }
            }
        }))
    }
},

There are a bunch of more problems in your code:

All the name properties are not used in your code (you probably added them trying to fix the error).

Your query mismatches the schema definition, either have the two args username and password directly on the field instead of in an extra input type:

args: {
    username:{
        name:'Username',
        type: new GraphQLNonNull(GraphQLString)
    },
    password:{
        name:'Password',
        type: new GraphQLNonNull(GraphQLString)
    }
},

Or adopt your query as described by Anthony:

mutation{
  login(input: { username: "dfgdfg",password: "test1234" }) {
    _id,
    name{
      fname,
      lname,
      mname
    }
  }
}
Herku
  • 7,198
  • 27
  • 36