Reputation: 2662
I have a query that look like this:
query MyQuery {
products {
edges {
node {
featured
id
image {
altText
mediaItemUrl
slug
}
productId
name
onSale
}
}
}
}
What I want is only fetch the result that featured
field is true
, if the featured
is false, then it never shown in the result.
Something like query like below in mysql:
SELECT id,image,name, featured FROM products WHERE featured = 'false'
But in graphql query above, I can't query the featured = false.
I tried:
query MyQuery {
products {
edges {
node {
featured @include(if: false)
id
... other field I need
}
}
}
}
But what this query do is, if featured
field is true, then included the featured
field in the result, else don't included the field in the result.This is not what I want.
What I want is,
If featured
field of a product is true
, then include the products
into the result, else, remove the whole product from the result.
How can I achieve this in the MyQuery
above?
Upvotes: 4
Views: 8061
Reputation: 84657
The @include
and @skip
directives are used for field selection, not filtering. GraphQL has no built-in way of doing filtering, sorting or pagination. It's up to each individual service to implement these features by exposing the appropriate arguments.
In this case, products
could expose an argument named filter
or isFeatured
to add the ability to filter the results by the featured
value. The field's resolver should then use that argument value to determine the correct value to return.
If you're writing client queries and consuming a schema you did not write, check your API's documentation to determine what arguments are available for the products
field. If the schema doesn't expose this capability and you don't have a way to change it, then as a client you don't have many options. At best, you can handle the filtering yourself after the result is fetched, but this is troublesome if you also use pagination.
Upvotes: 3