Is it possible to skip part of the request using apollo-client

I am trying to do 3 unique searches within a single query. The problem is that my search type "filter" is required in the schema, but it is optional in the interface. If there is a null value in my filter then I 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 a function skip

to ignore the entire request, but how can I achieve the same for a portion of the request? Or, alternatively, how can I compose them as separate requests, but only execute 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
    }
  }
}`;

      

+3


source to share


1 answer


You can use the function skip

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
    }
  }
}`;

      

Note the instructions @skip(if: ...)

after calls to the alias mainSearchData

, firstComparisonSearchData

and secondComparisonSearchData

.

+6


source







All Articles