简体   繁体   中英

GraphQL Error : Must provide name

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.

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 GraphQLInputObjectType not 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
    }
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM