如何编写GraphQL查询来检查多个枚举是否具有相同的值?

时间:2020-07-14 13:58:01

标签: reactjs enums graphql

我正在GraphCMS上构建一个带有后端的react应用程序(使用GraphQL查询它的数据库)。本质上,我有一个页面需要查询3个不同表中的项目,但只有它们的类型与提供的类型相匹配。

每个表的类型指定为3个不同的枚举。

在这里我调用查询API的方法:

graphCMSClient
  .request(query, { articleType: params.extendedContentCategory, reportType: params.extendedContentCategory, videoType: params.extendedContentCategory })
  .then((data) => {
    this.setState({
      data: [...data.reports, ...data.articles, ...data.videos],
    })
  })

我的graphQL查询(由上面的query变量表示)如下:

query getData($articleType: ArticleType, $reportType: ReportType, $videoType: VideoType){
  reports(where: {reportType: $reportType, featured: true}, orderBy: releaseDate_DESC, first: 5) {
    id
    ... other fields
  }
  articles(where: {articleType: $articleType, featured: true}, orderBy: releaseDate_DESC, first: 5) {
    id
    ... other fields
  }
  videos(where: {videoType: $videoType, featured: true}, orderBy: releaseDate_DESC) {
    id
    ... other fields
  }
}

ArticleTypeReportTypeVideoType是3个不同的枚举。

将相同的param传递给所有3个变量的原因是因为此页面可以容纳所有这些数据类型-我也无法事先知道用户是否在请求文章,视频,报告或这些的任意组合。此外,所有这3种中都存在某些类型-例如,ArticleTypeReportTypeVideoType在其中都具有值Economy

但是,我不断收到此错误: Error: variable 'reportType' with 'Thoughts_Opinions' is not a valid 'ReportType'-这是有道理的,因为Thoughts_Opinions类型仅在ArticleType枚举下。

我是否可以编写此查询?例如,在此查询中,我可以检查该值是否存在于枚举中,然后有条件地将其作为变量传递吗?还是我需要一个单独的查询来检查代码中的内容并根据该内容更改查询?

0 个答案:

没有答案
相关问题