I'm trying to perform 3 unique searches inside one query. The problem is that my search "filter" type is mandatory in the schema but in the front-end it's optional. If a null value is provided inside my filter then I'll get a graphql error.
I want to skip searching for mainSearchData, firstComparisonSearchData or secondComparisonSearchData depending on whether the search filters contain data.
I know that I can use the skip
function to ignore the whole query but how can I achieve the same for part of the query? Or alternatively, how can I compose these as separate queries but perform just one request?
const GROWTH_QUERY = gql`query aggregateQuery($mainFilter: filter!, $firstComparisonFilter: filter!, $secondComparisonFilter: filter! $interval: interval!) {
mainSearchData: groupBy(filter: $mainFilter, first: 20, after: 0) {
items: publicationDate(interval: $interval, minDocCount: 1, sort: DESC) {
date
count
}
}
firstComparisonSearchData: groupBy(filter: $firstComparisonFilter, first: 20, after: 0) {
items: publicationDate(interval: $interval, minDocCount: 1, sort: DESC) {
date
count
}
}
secondComparisonSearchData: groupBy(filter: $secondComparisonFilter, first: 20, after: 0) {
items: publicationDate(interval: $interval, minDocCount: 1, sort: DESC) {
date
count
}
}
}`;
@skip and @include directives can be applied to query fields. They allow you to skip or include a field based on the value of the if argument that is passed to the directive.
You can use the skip
function on the fields themselves.
For example:
const GROWTH_QUERY = gql`query aggregateQuery($mainFilter: filter!, $firstComparisonFilter: filter!, $secondComparisonFilter: filter! $interval: interval!) @skip(if: ...) {
mainSearchData: groupBy(filter: $mainFilter, first: 20, after: 0) {
items: publicationDate(interval: $interval, minDocCount: 1, sort: DESC) {
date
count
}
}
firstComparisonSearchData: groupBy(filter: $firstComparisonFilter, first: 20, after: 0) @skip(if: ...) {
items: publicationDate(interval: $interval, minDocCount: 1, sort: DESC) {
date
count
}
}
secondComparisonSearchData: groupBy(filter: $secondComparisonFilter, first: 20, after: 0) @skip(if: ...) {
items: publicationDate(interval: $interval, minDocCount: 1, sort: DESC) {
date
count
}
}
}`;
Notice the @skip(if: ...)
statements after the alias calls on mainSearchData
, firstComparisonSearchData
, and secondComparisonSearchData
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With