Tina automatically creates filters for collections defined in your schema.
To filter collection results, pass the filter
argument to the <collection>Connection
query, followed by any of the filter operator types for the fields on your collection.
The filter
object is a nested set of conditions where the keys correspond to the collection field and the value describes the condition.
Conditions can be either "binary" or "ternary". A binary condition is composed of a single operator and a single operand (i.e. {"eq":"foo"}
). A ternary condition is composed of two operators and two operands (.i.e. {"gt":0, "lte": 10}
).
Onlygt
,gte
,lt
,lte
,after
,before
may be used in ternary conditions.
Here we will query our post
collection with postConnection
and filter the results by the 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"}}]}}}
Here we will query our post
collection with postConnection
and filter the results so that only members of the specified category
are returned:
{postConnection(filter: {category: {in: ["politics"]}}) {edges {node {idtitlecategory}}}}
{"data": {"postConnection": {"edges": [{"node": {"id": "content/posts/voteForPedro.json","title": "Vote For Pedro","category": "politics"}}]}}}
Here we will query our post
collection with postConnection
and filter the results so that only posts with a date
between the specified range are returned:
{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"}}]}}}
It is possible to filter on multiple fields. Multiple conditions are currently treated as a boolean AND
operation. Here we will query our post
collection with postConnection
and filter the results by category
and 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"}}]}}}
Here we will query our post
collection with postConnection
, and filtering on the referenced author
's name:
{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"}}]}}}
Last Edited: August 15, 2024
© TinaCMS 2019–2024