【发布时间】:2019-06-22 04:30:22
【问题描述】:
我无法理解如何在 GatsbyJS 中为 GraphQL 查询编写过滤器。
这行得通:
filter: { contentType: { in: ["post", "page"] }
我基本上需要相反的,比如:
filter: { "post" in: { contentTypes } } // where contentTypes is array
这不起作用,因为“需要 NAME”(在我的示例中是“post”)。
通过 GatsbyJS docs 我发现了这个:
elemMatch: short for element match, this indicates that the field you are filtering will return an array of elements, on which you can apply a filter using the previous operators
filter:{
packageJson:{
dependencies:{
elemMatch:{
name:{
eq:"chokidar"
}
}
}
}
}
太棒了!这就是我需要的!所以我尝试了一下,我得到了:
error GraphQL Error Field "elemMatch" is not defined by type markdownRemarkConnectionFrontmatterTagsQueryList_2.
markdownRemarkConnectionFrontmatterTagsQueryList_2 中定义的关键字是:
- eq: 字符串 |空;
- ne: 字符串 |空;
- 正则表达式:字符串 |空;
- glob:字符串 |空;
- 在:数组 |空;
当文档中提到更多关键字(例如elemMatch)时,为什么我仅限于这些关键字?为什么我不允许使用过滤器结构“element in: { array }”?
如何创建此过滤器?
【问题讨论】: