'GraphQl filter in list field
I'm working on GraphQL API, and I want to filter my data "Products" by sellerId knowing that a product can be sold by several sellers, which means the sellers' field is an array.
Here is the query:
query GetProducts($filterObject:ProductWhereInput!){
products(where:$filterObject){
id
name
description
sku
price
sellers(where:$filterObject.sellers){
id
firstname
lastname
}
images{
url
fileName
}
}
}
Filter variable is defined like that
{
"filter":{
"sellerId":"ckzia0llkfngz0d09mrppd7kh"
}
}
and when I execute this query I get the error
"message": "unknown field 'filterObject.sellers' in variables"
I'm not sure if that's the correct method to apply the filter, it worked for me when I use it for single-value fields, but not with arrays.
If someone could help me, I'll be thankful.
Solution 1:[1]
Here you defined $filterObject
as a query variable
query GetProducts($filterObject:ProductWhereInput!)
But the way this line is written, it "uses" a variable that is not defined:
sellers(where:$filterObject.sellers)
It's looking for a variable named: "$filterObject.sellers
", not a property "sellers" of $filterObject
.
Possible solution 1 - server side:
You can change the sellers
field definition to use the whole $filterObject
and then the resolver function on the server can extract the field it needs.
sellers(where:$filterObject)
This solution makes sense if you have control over the server side.
Possible solution 2 - client side:
If you cannot change the code on the server side, you can define a separate variable and use it instead:
query GetProducts($filterObject:ProductWhereInput!, $filterSellersObject:ProductSellersWhereInput!){
//...
sellers(where:$filterSellersObject){
//...
(assuming ProductSellersWhereInput
is a defined type)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Tal Z |