Headless CMS > Basics
Using the GraphQL API Advanced Filtering
Learn how to use the Headless CMS's built-in GraphQL API advanced filter.
- how to use the Headless CMS GraphQL API advanced filtering
- how are
ANDandORnested queries working
Overview
In the 5.34.0 version of Webiny we introduced the AND and OR conditionals to help users filter the CMS entries.
Both AND and OR conditionals are arrays (lists) of the available filters, which depend on the model you are running the query for.
TheANDConditional
The AND conditional is the extension of our existing filtering. It works the same as our old GraphQL filtering, with the addition of nested filters.
This conditional requires all the filters sent to match.
Also, when sending filters in the root of the where object, there is no need for the AND keyword - but you can use it, of course.
SimpleANDExamples
Example #1
In this example we are searching for articles which:
- have a title with both
headlessandcmswords in it
This query will produce something like (title_contains = "headless" AND title_contains = "cms").
In our old GraphQL you could not do that, because you could send only one title_contains in the where object.
Example #2
Also, you can add a filter in the root level of the where object.
In this example we are searching for articles which:
- are in a category with ID
1 - have a title with both
headlessandcmswords in it.
This query will produce something like (categoryId = 1 AND title_contains = "headless" AND title_contains = "cms").
ComplexANDExamples
Example #1
In this example we are searching for articles which:
- are in a category with ID
1 - have a title with both
headlessandcmswords in it - are written by authors with IDs
5or6or7 - are created in year
2022
This query will produce something like (categoryId = 1 AND title_contains = "headless" AND title_contains = "cms" AND (author_in = [5, 6, 7] AND createdOn_gte = "2022-01-01" AND createdOn_lte = "2022-12-31")).
In this example, we’ve used the createdOn entry meta field. To learn more about date/time and identity meta fields, please refer to the Date/Time and Identity (On/By) Meta Fields reference article.
Example #2
All of these filters can be written in the root of the where object (except title_contains - because there are multiple):
The query above will produce the same result as the previous one, where we have nested AND conditional.
TheORConditional
The OR conditional brings completely new, and powerful, way of filtering the CMS entries.
This conditional requires only a single filter to match.
SimpleORExample
Example #1
In this example we are searching for articles which:
- have a title with
headlessorcmswords in it
This query will produce something like (title_contains = "headless" OR title_contains = "cms").
Example #2
In this example we are searching for articles which match any of the conditions:
- have a title with
cmsword in it - have a title with
headlessword in it and are in category with ID1
This query will produce something like ((title_contains = "headless" AND categoryId = 1) OR title_contains = "cms").
As you can notice, having more than one filter in an OR block produces the match all in that OR block.
We will cover the mixed AND and OR conditionals later in this article.
ComplexORExample
Example #1
In this example we are searching for articles which match any of the conditions:
- have a title with
headlessword in it - have a title with
cmsword in it - are in either category with ID
1or2
This query will produce something like (title_contains = "headless" OR title_contains = "cms" OR (categoryId = 1 OR categoryId = 2)).
The Mix ofANDandORConditionals
Root LevelORConditional With NestedANDandORConditionals
In this example we are searching for articles which match any of the conditions:
- have a title with
headlessword in it - have a title with
cmsword in it - have both
webinyandserverlessin the title and are created inJanuary of 2021orJanuary of 2022
This query will produce something like (title_contains = "headless" OR title_contains = "cms" OR (title_contains = "webiny" AND title_contains = "serverless" AND ((createdOn_gte = "2021-01-01" AND createdOn_lte = "2021-01-31") OR (createdOn_gte = "2022-01-01" AND createdOn_lte = "2022-01-31")))).
Root LevelANDConditional With NestedORandANDConditionals
In this example we are searching for articles which match all the conditions:
- have a title with
headlessword in it - have a title with
cmsword in it - have any of the
webinyorserverlessin the title or are created inJanuary of 2021orJanuary of 2022
This query will produce something like (title_contains = "headless" AND title_contains = "cms" AND (title_contains = "webiny" OR title_contains = "serverless" OR ((createdOn_gte = "2021-01-01" AND createdOn_lte = "2021-01-31") AND (createdOn_gte = "2022-01-01" AND createdOn_lte = "2022-01-31")))).
Root LevelORandANDConditionals
In this example we are searching for articles which:
- are written by author with ID
1OR are in category with ID2 - have a title with
headlessandcmswords in it
This query will produce something like ((author = 1 OR categoryId = 2) AND (title_contains = "headless" AND title_contains = "cms")).
While users can nest AND and OR conditionals indefinitely, they need to be aware that it might result in performance issues, especially in the DynamoDB only systems.