Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to query the same field multiple times with graphql

Tags:

graphql

What I want to do is to query for a particular field multiple times with different arguments. Is that possible in GraphQL?

Something like this:

query {     myItem(size: 100, type: 2) {         id,         name     }     myItem(size: 150, type: 2) {         id,         name     }     myItem(size: 10, type: 1) {         id,         name     } } 
like image 430
Oli Avatar asked May 22 '18 08:05

Oli


People also ask

How do I use multiple queries in GraphQL?

Multiple OperationsIf a request has two or more operations, then each operation must have a name. A request can only execute one operation, so you must also include the operation name to execute in the request (see the “operations” field for requests). Every operation name in a request must be unique.

Can GraphQL schema have multiple queries?

When doing query batching, the GraphQL server executes multiple queries in a single request. But those queries are still independent from each other. They just happen to be executed one after the other, to avoid the latency from multiple requests.

Why GraphQL is overkill?

GraphQL can be overkillSince GraphQL is so complex and powerful, it can commonly be overkill for smaller applications. If you have a simple/straight-forward app in front of you, it will be easier to work with a REST API.


1 Answers

Yes, this is possible, but not in this form. GraphQL server will reject such query as a field with the same name used multiple times, but with different arguments.

You need to use aliases:

query {   item1: myItem(size: 100, type: 2) {     id,     name   }   item2: myItem(size: 150, type: 2) {     id,     name   }   item3: myItem(size: 10, type: 1) {     id,     name   } } 

You can find more info on aliases here:

http://graphql.org/learn/queries/#aliases

like image 157
tenshi Avatar answered Sep 26 '22 08:09

tenshi