Loving Tina? us on GitHub0.0k

文档

学习

v.Latest
Documentation
过滤查询结果
目录

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

只有gtgteltlteafterbefore可以用于三元条件。

示例

在字段上进行过滤

这里我们将使用postConnection查询我们的post集合,并通过帖子title过滤结果:

{
postConnection(filter: {title: {startsWith: "Vote"}}) {
edges {
node {
id
title
category
}
}
}
}
{
"data": {
"postConnection": {
"edges": [
{
"node": {
"id": "content/posts/voteForPedro.json",
"title": "Vote For Pedro",
"category": "politics"
}
}
]
}
}
}

使用IN操作符在字段上进行过滤

这里我们将使用postConnection查询我们的post集合,并过滤结果以便仅返回指定category的成员:

{
postConnection(filter: {category: {in: ["politics"]}}) {
edges {
node {
id
title
category
}
}
}
}
{
"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 {
id
title
category
}
}
}
}
{
"data": {
"postConnection": {
"edges": [
{
"node": {
"id": "content/posts/voteForPedro.json",
"title": "Vote For Pedro",
"category": "politics"
}
}
]
}
}
}

在多个字段上进行过滤

可以在多个字段上进行过滤。多个条件目前被视为布尔AND操作。这里我们将使用postConnection查询我们的post集合,并通过categorytitle过滤结果:

{
postConnection(filter: {category: {in: ["politics"]}, title: {startsWith: "Vote"}}) {
edges {
node {
id
title
category
}
}
}
}
{
"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 {
id
title
category
}
}
}
}
{
"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"
}
}
]
}
}
}
上次编辑: March 26, 2025