简体   繁体   English

可以使用apollo-client跳过部分查询

[英]Is is possible to skip part of a query with apollo-client

I'm trying to perform 3 unique searches inside one query. 我正在尝试在一个查询中执行3个唯一搜索。 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. 如果在我的过滤器中提供了空值,那么我将得到graphql错误。

I want to skip searching for mainSearchData, firstComparisonSearchData or secondComparisonSearchData depending on whether the search filters contain data. 我想跳过搜索mainSearchData,firstComparisonSearchData或secondComparisonSearchData,具体取决于搜索过滤器是否包含数据。

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? 我知道我可以使用skip函数来忽略整个查询但是如何在查询的部分内部实现相同的功能呢? 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
    }
  }
}`;

You can use the skip function on the fields themselves. 您可以在字段本身上使用skip功能。

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 . 注意@skip(if: ...)语句上的别名调用后mainSearchDatafirstComparisonSearchDatasecondComparisonSearchData

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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