我正在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
}
}
ArticleType
,ReportType
和VideoType
是3个不同的枚举。
将相同的param
传递给所有3个变量的原因是因为此页面可以容纳所有这些数据类型-我也无法事先知道用户是否在请求文章,视频,报告或这些的任意组合。此外,所有这3种中都存在某些类型-例如,ArticleType
,ReportType
和VideoType
在其中都具有值Economy
。>
但是,我不断收到此错误:
Error: variable 'reportType' with 'Thoughts_Opinions' is not a valid 'ReportType'
-这是有道理的,因为Thoughts_Opinions
类型仅在ArticleType
枚举下。
我是否可以编写此查询?例如,在此查询中,我可以检查该值是否存在于枚举中,然后有条件地将其作为变量传递吗?还是我需要一个单独的查询来检查代码中的内容并根据该内容更改查询?