简体   繁体   中英

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.

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM