I constructed the following query in GraphQL playground:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Write your query or mutation here | |
{ | |
product(id:88466){ | |
id | |
name | |
price | |
} | |
product(name:"XBOX"){ | |
id | |
name | |
price | |
} | |
} |
In this query I try combine 2 calls, one to fetch a product by id and another one where I try to fetch a product by name.
However when I tried to execute this query it resulted in the following error message:
The query has non-mergable fields
The problem is that GraphQL tries to merge the results in one product result object which does not work.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"data": { | |
"product": [ | |
{ | |
"id": "88466", | |
"name": "xbox", | |
"price": "200$" | |
} | |
] | |
} | |
} |
To have the behavior I want I need to specify an alias for every query:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Write your query or mutation here | |
{ | |
productById:product(id:88466){ | |
id | |
name | |
price | |
} | |
productByName:product(name:"XBOX"){ | |
id | |
name | |
price | |
} | |
} |
Now when I execute the query, the query results are put in their own object:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"data": { | |
"productById": [ | |
{ | |
"id": "88466", | |
"name": "xbox", | |
"price": "200$" | |
} | |
], | |
"productByName": [ | |
{ | |
"id": "88466", | |
"name": "xbox", | |
"price": "200$" | |
} | |
] | |
} | |
} | |