Skip to content

Filtering

Every list query takes a where argument. Fields inside it are combined with AND.

query UsedToyotas {
stocked_vehicles(
where: {
make: { _eq: "Toyota" }
condition: { _eq: used }
odometer: { _lt: 50000 }
}
limit: 10
) {
id
make
model
odometer
}
}
Operator Meaning Example
_eq, _neq equals, not equals make: { _eq: "Kia" }
_gt, _gte, _lt, _lte greater or less than production_year: { _gte: 2022 }
_in, _nin in a list, not in a list body_type: { _in: ["SUV", "Wagon"] }
_is_null is (or isn’t) empty available_from: { _is_null: true }
_ilike, _nilike case-insensitive match, % is a wildcard model: { _ilike: "%cross%" }
_like case-sensitive match stock_id: { _like: "NSW-%" }
_iregex case-insensitive regular expression vin: { _iregex: "^LGX" }

Text matching with _eq is exact and case-sensitive. Use _ilike without wildcards for a case-insensitive exact match: make: { _ilike: "toyota" }.

Operator On Meaning Example
_contains arrays, JSON contains all of these pricing_codes: { _contains: ["AU_VIC"] }
_contained_in arrays, JSON is a subset of
_has_key JSON object has this key utm_data: { _has_key: "utm_source" }
_has_keys_any, _has_keys_all JSON has any or all of these keys

Use _or for alternatives. It takes a list of conditions.

query ElectricOrHybrid {
stocked_vehicles(
where: {
_or: [
{ fuel_type: { _ilike: "%electric%" } }
{ fuel_type: { _ilike: "%hybrid%" } }
]
}
limit: 10
) {
id
fuel_type
}
}

Use _and when you need the same field twice, and _not to negate a whole condition.

query MidPriced {
stocked_vehicles(
where: {
_and: [{ min_price: { _gte: 3000000 } }, { min_price: { _lt: 5000000 } }]
_not: { make: { _in: ["Ferrari", "Lamborghini"] } }
}
limit: 10
) {
id
min_price
}
}

Nest a relationship’s fields to filter on them. This is how you scope stock to a showroom:

query ShowroomStock($showroomId: uuid!) {
stocked_vehicles(
where: { showroom_stocked_vehicles: { showroom_id: { _eq: $showroomId } } }
limit: 10
) {
id
}
}

The same works for one-to-one relationships, such as vehicles at a location in Victoria:

query VictorianStock {
stocked_vehicles(where: { location: { address: { _contains: { state: "VIC" } } } }, limit: 10) {
id
location {
name
}
}
}

Relationships that return a list take their own where, order_by and limit. This filters the images, not the vehicles:

query FeaturedImages {
stocked_vehicles(limit: 5) {
id
assets(where: { placement: { _eq: "featured" } }, limit: 1) {
src
}
}
}

A few fields are enums: condition (new, used, demo) and listing_type (retail, private). Written inline, enum values have no quotes. Passed as variables, they are ordinary strings.

# Inline: no quotes
stocked_vehicles(where: { condition: { _eq: used } }) { id }
# Variable: a normal string in the variables JSON, e.g. { "condition": "used" }
query ($condition: vehicle_conditions_enum!) {
stocked_vehicles(where: { condition: { _eq: $condition } }) { id }
}

The variable’s type must be the enum type, not String: vehicle_conditions_enum for condition, listing_types_enum for listing_type. Values are case-sensitive (used, not Used).

Put values in variables rather than building query strings. It avoids escaping bugs and injection, and you can reuse the query text.

{
"query": "query ($make: String!) { stocked_vehicles(where: { make: { _ilike: $make } }, limit: 10) { id model } }",
"variables": { "make": "toyota" }
}

Declare each variable with the column’s type:

Columns Variable type
id, showroom_id and other ids uuid!
make, model, code and other text String!
odometer, production_year, min_price and other decimals numeric!
limit, offset Int!
created_at, updated_at and other timestamps timestamptz!
available_from and other dates date!
condition, listing_type vehicle_conditions_enum!, listing_types_enum!

Send only the variables a query declares. An extra one fails with unexpected variables in variableValues, which is easy to hit if you share one variables object between queries.

You can also pass a whole where as a variable of type stocked_vehicles_bool_exp, which helps when your filters come from a search form.

query Search($where: stocked_vehicles_bool_exp!) {
stocked_vehicles(where: $where, limit: 24) {
id
}
}

search_stocked_vehicles takes a search phrase and accepts the same where, order_by, limit and offset as stocked_vehicles.

query Search($showroomId: uuid!) {
search_stocked_vehicles(
args: { search: "outlander phev" }
where: { showroom_stocked_vehicles: { showroom_id: { _eq: $showroomId } } }
limit: 12
) {
id
make
model
badge
}
}