Search code examples
graphqlgraphql-java

Populate an object and pass it in a request parameter in GraphQL


How can we populate an object and pass it in a request parameter in GraphQL. For example, i want to get rid of sequence of parameters here in the GraphQL request and want to create an object and pass it as a single parameter.

{
 allBooks(first:20, orderBy:"myRating"){
  isn
  title
  authors
  publisher
 }
}

Solution

  • You can use GraphQL Input Types.

    In your schema definition you need to define the input into something like:

    input BooksInput {
      first: Int
      orderBy: String
    }
    

    And require it as the args type in your query, then use it as:

    {
     allBooks($input: {first:20, orderBy:"myRating"}){
      isn
      title
      authors
      publisher
     }
    }