Include the Same Query More than Once in a GraphQL Request

It can sometimes be useful to request two or more copies of the same query result in one GraphQL request.

We can repeat the same query twice, for example, using output naming as in the example below:

{
  getBook {
    title
  }

  secondCopy:getBook {
    title
  }
}

The label secondCopy is required to create a unique name in the output data.

The label we use will replace the query name in the output response, as below:

{
  "data": {
    "getBook": {
      "title": "Book A"
    },
    "secondCopy": {
      "title": "Book A"
    }
  }
}

We can request as many copies as desired in the query.

 

Leave a Reply

Your email address will not be published. Required fields are marked *