Tina会自动为在你的模式中定义的集合创建过滤器。
要过滤集合结果,请将filter
参数传递给<collection>Connection
查询,然后为集合中的字段使用任何过滤操作符类型。
filter
对象是一组嵌套的条件,其中键对应于集合字段,值描述条件。
条件可以是“二元”或“三元”的。二元条件由一个操作符和一个操作数组成(例如{"eq":"foo"}
)。三元条件由两个操作符和两个操作数组成(例如{"gt":0, "lte": 10}
)。
键 | 行为 | 类型 |
---|---|---|
eq | 等于 | string, number, boolean |
in | 其中之一 | string[], number[], boolean[] |
gt | 大于 | string, number |
gte | 大于或等于 | string, number |
lt | 小于 | string, number |
lte | 小于或等于 | string, number |
startsWith | 以...开始 | string |
after | 之后 | datetime |
before | 之前 | datetime |
只有gt
、gte
、lt
、lte
、after
、before
可以用于三元条件。
这里我们将使用postConnection
查询我们的post
集合,并通过帖子title
过滤结果:
{postConnection(filter: {title: {startsWith: "Vote"}}) {edges {node {idtitlecategory}}}}
{"data": {"postConnection": {"edges": [{"node": {"id": "content/posts/voteForPedro.json","title": "Vote For Pedro","category": "politics"}}]}}}
这里我们将使用postConnection
查询我们的post
集合,并过滤结果以便仅返回指定category
的成员:
{postConnection(filter: {category: {in: ["politics"]}}) {edges {node {idtitlecategory}}}}
{"data": {"postConnection": {"edges": [{"node": {"id": "content/posts/voteForPedro.json","title": "Vote For Pedro","category": "politics"}}]}}}
这里我们将使用postConnection
查询我们的post
集合,并过滤结果以便仅返回在指定范围内具有date
的帖子:
{postConnection(filter: {date: {after: "2022-06-01T07:00:00.000Z", before: "2022-06-30T07:00:00.000Z"}}) {edges {node {idtitlecategory}}}}
{"data": {"postConnection": {"edges": [{"node": {"id": "content/posts/voteForPedro.json","title": "Vote For Pedro","category": "politics"}}]}}}
可以在多个字段上进行过滤。多个条件目前被视为布尔AND
操作。这里我们将使用postConnection
查询我们的post
集合,并通过category
和title
过滤结果:
{postConnection(filter: {category: {in: ["politics"]}, title: {startsWith: "Vote"}}) {edges {node {idtitlecategory}}}}
{"data": {"postConnection": {"edges": [{"node": {"id": "content/posts/voteForPedro.json","title": "Vote For Pedro","category": "politics"}}]}}}
这里我们将使用postConnection
查询我们的post
集合,并在引用的author
的名字上进行过滤:
{postConnection(filter: {author: {author: {name: {eq: "Napolean"}}}}) {edges {node {idtitlecategory}}}}
{"data": {"postConnection": {"edges": [{"node": {"id": "content/posts/anotherPost.json","title": "Just Another Blog Post","category": "lifestyle"}},{"node": {"id": "content/posts/nested/anotherPost.json","title": "Just Another Blog Post","category": "lifestyle"}},{"node": {"id": "content/posts/voteForPedro.json","title": "Vote For Pedro","category": "politics"}}]}}}