Filtering Query Results

You can filter the query results in GraphQLize using the where argument.

Comparison Operators

Equal To, Not Equal To

query {
cities(where: {cityId: {eq: 3}}) {
city
}
}
query {
languages(where: {name: {neq: "English"}}) {
name
}
}

Greater Than, Greater Than Or Equal To

query {
payments(where: {amount: {gt: 11.99}}) {
rentalId
amount
}
}
query {
payments(where: {amount: {gte: 11.99}}) {
rentalId
amount
}
}

Less Than, Less Than Or Equal To

query {
payments(where: {amount: {lt: 6.99}}) {
rentalId
amount
}
}
query {
payments(where: {amount: {lte: 6.99}}) {
rentalId
amount
}
}

Between

query {
payments(where: {paymentDate: {between: {from: "2005-08-23T21:00:00", to: "2005-08-23T21:03:00"}}}) {
rentalId
}
}

Is Null, Is Not Null

query {
employees(where: {employeeReportsToId: {isNull: true}}) {
firstName
lastName
}
}
query {
employees(where: {employeeReportsToId: {isNotNull: true}}) {
firstName
lastName
}
}

IN, NOT IN

query {
payments(where: {customerId: {in: [1, 2]}}) {
paymentId
amount
}
}
query {
payments(where: {customerId: {notIn: [1, 2]}}) {
paymentId
amount
}
}

String Comparison

Like

query {
countries(where: {country: {like: "A%"}}) {
country
}
}

Not Like

query {
countries(where: {country: {notLike: "A%"}}) {
country
}
}

Logical Operators

AND

query {
# payments of a customer with the id `1`
# and amount greater than `5.99`
payments(where: {and: {customerId: {eq: 1}, amount: {gt: 5.99}}}) {
paymentId
amount
}
}

and also supports list of conditions.

The above query can be rewritten as

query {
payments(where: {and: [{customerId: {eq: 1}, amount: {gt: 5.99}}]}) {
paymentId
amount
}
}

OR

or is similar to and in syntax. It accepts both a single condition or a list of conditions.

query {
# languages with the name either "English" or "French"
languages(where: {or: [{name: {eq: "English"}}, {name: {eq: "French"}}]}) {
languageId
name
}
}

NOT

Using the not operator we can invert the filter condition.

query {
# languages with the name except "English" or "French"
languages(where: {not: {or: [{name: {eq: "English"}}, {name: {eq: "French"}}]}}) {
languageId
name
}
}

Filter by nested object(s)

We can filter the resulting objects based on the fields of a nested object(s).

One to One

For example, to get all the cities of a county using the country' name in the above schema, We can query it as

query {
cities(where: {country: {country: {eq : "Algeria"}}}) {
cityId
city
}
}

One to Many

If the nested object has one-to-many relationship with its parent, the filter condition yield the results if at-least one of the nested object's field(s) satisfy the condition.

For the above schema, we can get a list of countries which has at-least one city that starts with Ab using the below query.

query {
# returns countries which has at-least one city
# with the name like 'AB%'
countries(where: {cities: {city: {like: "Ab%"}}}) {
countryId
country
}
}

Many to Many

A filter based on a nested object with the many-to-many relationship behaves the same as that of one-to-many.

For the above schema, to get the actors who are part of at-lease one film which has the word LIFE in its title.

query {
actors(where: {films: {title: {:like "%LIFE%"}}}) {
firstName
lastName
}
}

Let's assume that we have a schema like below

To filter authors who has at-least one course with the rating 5, we can achieve it using the following query.

query {
authors(where: {courses : {rating: {eq: 5}}}) {
firstName
lastName
}
}

If we want to filter only the authors who has got the rating 5 in all their courses, we can achieve it by

query {
authors(where: {not: {courses : {rating: {neq: 5}}}}) {
firstName
lastName
}
}

We are making use of SQL anti-join here by inverting the whole condition using not and complementing the inner condition using its reverse (neq in the place of eq).

Existence Check

GraphQLize provides a special filter parameter have to filter based on the existence of child objects. For example, in the above schema, if we'd like to filter only the authors who have courses, it can be achieved using the following query.

query {
authors(where: {have: courses}) {
firstName
lastName
}
}

Suppose, if we want to filter authors who don't have courses, we can make use of the not in addition to have in the filter.

query {
authors(where: {not: {have:courses}}) {
firstName
lastName
}
}

The have parameter is available only in the one-to-many and many-to-many relationship fields

Filtering nested objects

GraphQLize also supports filtering nested objects.

query {
countryByCountryId(countryId: 2) {
country
# filter one-to-many relationship
cities(where: {city: {like: "B%"}}) {
cityId
city
}
}
}
query {
actorByActorId(actorId: 148) {
firstName
# filter many-to-many relationship
films(where: {title: {like: "S%"}}) {
title
}
}
}