Current Solution for Filters in GraphQL
Currently, Dgraph GraphQL has the functionality to take input of filter
in query<type_name>
type queries. Nodes which satisfy the criteria specified in the filter
are only considered in the query and other nodes are filtered out. Example:
queryPost(filter: { text: { alloftext: "foobar" } } )
Boolean operators like and
, or
and not
are also supported inside filters.
Example:
queryPost(filter: { text: { alloftext: "foobar" } }, or: { score: { ge: 10 } } )
The input type <type_name>Filter
in output GraphQL schema contains fields on which filters can be applied. It currently contains the fields with search directive along with and
, or
, not
operators. Example:
type Author {
id: ID!
name: String!
}
The above input GraphQL schema generates the following AuthorFilter
input type in output GraphQL schema.
input AuthorFilter {
id: [ID!]
has: AuthorHasFilter
and: AuthorFilter
or: AuthorFilter
not: AuthorFilter
}
With the current functionality, it is possible to compound filters using boolean operators of the form A AND B
, A OR B
, NOT A
etc.
Problem with Nesting Filters
But, because and
and or
fields inside AuthorFilter
input type take only a single AuthorFilter
instead of an array, [AuthorFilter]
, it is impossible to have a filter query of the form, (A OR B) AND (C OR D)
Proposed Solution to handle Nested Filters
Boolean operators AND
and OR
are binary operators and operate on atleast two operands. If the generated output schema for AuthorFilter
is changed to have a list of AuthorFilter
or two operands of the type AuthorFilter
, it could then be possible to create any type of nested filters with AND
and OR
boolean operators. The changed input type of AuthorFilter
will then look like.
input AuthorFilter {
id: [ID!]
has: AuthorHasFilter
and: [AuthorFilter]
or: [AuthorFilter]
not: AuthorFilter
}
The proposed solution will change how AND
and OR
operators are used in filters. This will be a breaking change. This approach is also consistent with other GraphQL providers.