question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Feature Request: Graphql query filters AND / OR / NOT

See original GitHub issue

Summary

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

DGraph https://github.com/dgraph-io/dgraph/blob/8f53e376b7a07404ca26230b7f8d9ab4a0ee6dac/wiki/content/graphql/queries/and-or-not.md

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

Hasura https://hasura.io/docs/1.0/graphql/manual/queries/query-filters.html#using-multiple-filters-in-the-same-query-and-or

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:closed
  • Created 3 years ago
  • Reactions:3
  • Comments:14 (5 by maintainers)

github_iconTop GitHub Comments

12reactions
scottwojancommented, May 20, 2020

I have to agree this feels like baseline functionality for filtering capability. I just hit a wall due to not having OR capability.

4reactions
DevSidecommented, Apr 20, 2020
Read more comments on GitHub >

github_iconTop Results From Across the Web

GraphQL Search and Filter – How to search and filter results ...
Searching and filtering is a standard part of any GraphQL API. In this article, we'll learn how to add filtering capabilities to your...
Read more >
And, Or and Not Operators in GraphQL - Dgraph
Every GraphQL search filter can use AND, OR and NOT operators. ... The following example queries demonstrate the use of and , or...
Read more >
Graphql sort and filter - Feature Requests - Fauna Forums
I understand that currently sorting and filtering is not implemented in graphql. I want to ask then, why? Without these features Graphql is ......
Read more >
GraphQL Filtering, Pagination & Sorting Tutorial with JavaScript
The first step is to think about the filters you want to expose through your API. In your case, the feed query in...
Read more >
Queries and Mutations - GraphQL
In a system like REST, you can only pass a single set of arguments - the query parameters and URL segments in your...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found