19

My Goal: I want to execute a mutation in GraphQL Playground.

My schema looks the following:

type Mutation {
    # Add a new comment
    addComment(comment: InputComment!): Comment
}

# Input type for a new Comment
input InputComment {
    # The comment text
    comment: String!
    # The id of the author
    author: String!
    # The id of the talk
    talkId: Long!
}

I found a lot of examples that will work if I have:

type Mutation {
    # Add a new comment
    addComment(comment: String!, author: String!, talkId: Long!): Comment
}

But I can't understand how I can create an object of type InputComment on the fly in GraphQL Playground.

E.g., for the last scenario I could just run:

mutation {
  addComment(
    comment: "My great comment"
    author: "The great author"
    talkId: 123
  ) {
    id
  }
}
B. Wasnie
  • 541
  • 1
  • 4
  • 12
  • 1
    When you ask questions on SO be sure to include the language you are using and any framework or library. This makes it much easier to help you. Many of us are using Apollo Server and Apollo client but there are notable others that have implemented Playground that may have different requirements, I don't know, but more information about your stack should be included. – Preston Aug 17 '19 at 00:36

2 Answers2

25
mutation {
  addComment(comment: {comment: "Cool", author: "Me", talkId: 12}) {
    createdOn
    id
  }
}
B. Wasnie
  • 541
  • 1
  • 4
  • 12
6

Also add a Comment type in your schema

type Comment {
   id: ID! 
   comment: String!
   author: String!
   talkId: Long!
}

# Input type for a new Comment
input InputComment {
    comment: String!
    author: String!
    talkId: Long!
}

type Mutation {
    # Add a new comment
    addComment(comment: InputComment!): Comment
}

##Then query should be
mutation {
  addComment(comment: {comment: "test comment", author: "Sample name", talkId: 123}) {
    id,
    comment,
    author,
    talkId
  }
}