0
votes

I trying to add mutation in graphql-yoga but every time I try to mutate the I get error saying

Post validation failed: title: Path title is required.",

I have no idea why.

Here is my code

resolver

Mutation: {
        createPost: async(root, args, ctx) => {

            console.log(args)

            try {

                const post = new Post({
                    title: args.title,
                    description: args.description,
                    content: args.content
                });
                const result = await post.save();
                console.log(result);
                return result
            }catch(err) {
                throw err
            }

        }
    }

schema

input postInput{
        title: String!
        description: String!
        content: String!
    }

    type Mutation {
        createPost(input: postInput): Post!
    }

This works fine if I remove the input type and directly do like this

type Mutation {
        createPost(title: String!,description: String!,content: String!): Post!
    }

log result

{ input:
   [Object: null prototype] {
     title: 'with input',
     description: 'this is de',
     content: 'this is conte' } }

Here Why am I getting [Object: null prototype]?

1

1 Answers

3
votes

You have to send your data in your resolver like this if you give input type like this on schema:

const post = new Post({
  title: args.input.title,
  description: args.input.description,
  content: args.input.content
});

It means, in args, we need a parameter called input which is of type Post.

And while giving the datas on graphql gui send data like this:

mutation {
  createPost(input:{
     title: 'with input',
     description: 'this is de',
     content: 'this is conte'}) {
   //return your id or others
  }
}