Feature Request: Graphql query filters AND / OR / NOT
See original GitHub issueSummary
For the time being, it is not possible to do OR / NOT conditions in query filters. By design, GraphQL won’t add this feature but many libraries create resolvers to provide it.
Motivation
Many graphql queries are located component side and the way they extract or filter data depends on UI rules. A lack of conditional filtering leads to intermediate transformations far from where the query is defined. It would be very useful to introduce AND/OR/NOT operators to build more complex logical expressions.
Basic example
Retrieve all entities where their fields match (a && !b) || (!a && b)
in a single filter.
The only way to do it component side is to create two separate queries and combine them.
allEntity (filter: { a: { eq: true }, b: { eq: false } }) {}
allEntity (filter: { a: { eq: false }, b: { eq: true } }) {}
What would it look like?
Top level AND/OR
allEntity (filter: { OR: [
{ a: { eq: true }, b: { eq: false } },
{ a: { eq: false }, b: { eq: true } }
] }) {}
GraphCool https://www.graph.cool/docs/reference/graphql-api/query-api-nia9nushae/#filtering-by-field
where
clause
allEntity (filter: { where: {
OR: [
{ a: { eq: true }, b: { eq: false } },
{ a: { eq: false }, b: { eq: true } }
]
} }) {}
ContentStack https://www.contentstack.com/docs/developers/apis/graphql-content-delivery-api/#or-operator
conjunction
and conditions
clauses
allEntity (filter: {
conjunction: OR,
conditions: [
{ a: { eq: true }, b: { eq: false } },
{ a: { eq: false }, b: { eq: true } }
]
} }) {}
Drupal https://drupal-graphql.gitbook.io/graphql/queries/filters
Issue Analytics
- State:
- Created 3 years ago
- Reactions:3
- Comments:14 (5 by maintainers)
I have to agree this feels like baseline functionality for filtering capability. I just hit a wall due to not having OR capability.
It’s interesting. I won’t use
createResolvers
where the logic is too far from the components and their queries.Here is some query apis which use OR / NOT operators:
graphcool https://www.graph.cool/docs/reference/graphql-api/query-api-nia9nushae/#filtering-by-field
hasura https://hasura.io/docs/1.0/graphql/manual/queries/query-filters.html#using-multiple-filters-in-the-same-query-and-or
drupal api https://drupal-graphql.gitbook.io/graphql/queries/filters
contentstack api https://www.contentstack.com/docs/developers/apis/graphql-content-delivery-api/#or-operator