How to execute a conjunction of OR and AND in AEM GraphQL? | Community
Skip to main content
Level 4
November 7, 2022
Solved

How to execute a conjunction of OR and AND in AEM GraphQL?

  • November 7, 2022
  • 3 replies
  • 1160 views

I want to have a filter like this:

const value = { AND: [ { OR: [ { productVisibility_contains_none: ['authenticated', 'eap'] }, { AND: [ { productVisibility_contains_all: ['authenticated'] }, { productVisibility_contains_none: ['eap'] }, ], }, ], }, { title_exists: true }, { OR: [ { title_contains: $arg1 }, { author_contains: $arg1 }, { articleSource_contains: $arg1 }, { articleByline_contains: $arg1 }, { articleSubtitle_contains: $arg1 }, { primaryCategoryTag_contains_some: [$arg1, $arg1_cap, $arg1_low] }, { categoryTags_contains_some: [$arg1, $arg1_cap, $arg1_low] }, ], }, { primaryCategoryTag_contains: $categoryTitles }, ], };

as you can see there's conjunction of OR and AND operations, however in AEM GQL you only get either OR or AND for all the high level filters. I wanted to know if it would be possible to perform the above in AEM or not?

Best answer by aanchal-sikka

You would need to restructure your query. The samples for OR and And are as follows:

 

For an OR condition refer to: https://experienceleague.adobe.com/docs/experience-manager-cloud-service/content/headless/graphql-api/sample-queries.html?lang=en#sample-all-cities-d-ch-population

 

country: { _logOp: OR _expressions: [ { value: "Germany" }, { value: "Switzerland" } ] }

 

 

For and condition omit the "_logOp". Two adjacent conditions are tried via AND.

Example:
Multiple values in population is AND. And So is 'population' + 'country'

query { cityList(filter: { population: { _expressions: [ { value: 400000 _operator: GREATER_EQUAL }, { value: 1000000 _operator: LOWER } ] }, country: { _logOp: OR _expressions: [ { value: "Germany" }, { value: "Switzerland" } ] } }) { items { name population country } } }

 

3 replies

krati_garg
Adobe Employee
Adobe Employee
November 7, 2022
Level 4
November 7, 2022

Hello @krati_garg 

 

Thanks for the quick response, however if you see my question and compare the same with the documentation you would observe that this solution won't work for me as I need to perform a high level OR as well as AND filtering among multiple fields.

aanchal-sikka
Community Advisor
aanchal-sikkaCommunity AdvisorAccepted solution
Community Advisor
February 24, 2024

You would need to restructure your query. The samples for OR and And are as follows:

 

For an OR condition refer to: https://experienceleague.adobe.com/docs/experience-manager-cloud-service/content/headless/graphql-api/sample-queries.html?lang=en#sample-all-cities-d-ch-population

 

country: { _logOp: OR _expressions: [ { value: "Germany" }, { value: "Switzerland" } ] }

 

 

For and condition omit the "_logOp". Two adjacent conditions are tried via AND.

Example:
Multiple values in population is AND. And So is 'population' + 'country'

query { cityList(filter: { population: { _expressions: [ { value: 400000 _operator: GREATER_EQUAL }, { value: 1000000 _operator: LOWER } ] }, country: { _logOp: OR _expressions: [ { value: "Germany" }, { value: "Switzerland" } ] } }) { items { name population country } } }

 

Aanchal Sikka
Level 2
April 17, 2025

How is that the right answer?

kautuk_sahni
Community Manager
Community Manager
February 28, 2024

@spidey1405  Did you find the suggestions from users helpful? Please let us know if more information is required. Otherwise, please mark the answer as correct for posterity. If you have found out solution yourself, please share it with the community.

Kautuk Sahni