Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

graphql - use queries in mutations - create a nested object

I have a very simple model with post that embeds several comments

I wondered how I should do a mutation to add a new comment to the post

As I already have queries defined to get back a postwith a given id, I wanted to try to have the following mutation syntax working

mutation {
  post(id: "57334cdcb6d7fb172d4680bb") {
    addComment(data: {
      text: "test comment"
    })
  }
}

but I can't seem to find a way to make it work. Even if I'm in a mutation, output type being a post addComment is seen as a field post should have.

Do you guys have any idea ?

Thanks

like image 758
Martin Ratinaud Avatar asked Nov 09 '22 15:11

Martin Ratinaud


1 Answers

You can't embed fields into other fields like that.

You would create a new input object for your post mutation

input CommentInput {
 text: String
}

type Mutation {
 post(id: ID!, addComment: CommentInput): Post
}

In your resolver you look for the addComment variable and call the addComment resolver with the arguments.

Your mutation would be

mutation {
  post(id: "57334cdcb6d7fb172d4680bb", 
    addComment: {
      text: "test comment"
    }) {
    id
    comment {
      text
    }
  }
}
like image 183
Corey Avatar answered Dec 15 '22 07:12

Corey