Filters
The examples on this page use the UserModel and PostModel models from Relations.
Filter a list
Declare a Filter scalar and a filter argument on the list field:
scalar Filter
type Query {
posts(filter: Filter): [Post!]!
}Filters are disabled by default; enable them in the options of graph.resolve:
const resolvers = {
Query: {
posts: (_parent, _args, context, info) => {
return graph.resolve(PostModel.query(), { context, info, filters: true })
},
},
}The client passes conditions as an object:
query {
posts(filter: { author_id__in: [1, 2], text__like: "%news%" }) {
id
text
}
}Filter a relation
To let clients filter the rows of one relation, pass filters: true to its r.relation, and add a filter argument to the relation field:
type User {
id: Int!
name: String!
posts(filter: Filter): [Post!]!
}const graph = r.graph({
User: r.model(UserModel, {
fields: {
id: true,
name: true,
posts: r.relation({ filters: true }),
},
}),
Post: r.model(PostModel),
})To enable filters wherever the graph loads a type, set allowAllFilters: true on the type: on Post, it enables filters on the root lists of posts and on every relation that loads posts, such as user { posts }. To enable them for every type, set allowAllFilters: true on the graph:
const graph = r.graph(
{
User: r.model(UserModel),
Post: r.model(PostModel),
},
{ allowAllFilters: true },
)To disable filters for one type under allowAllFilters: true of the graph, set allowAllFilters: false on the type and do not pass filters: true to its root queries and relations. For paginated fields, see Pagination.
Conditions
Each key of a filter is a column, optionally followed by __ and an operator:
column: valuematches the value, andcolumn: nullmatchesNULL;column__lt,column__lte,column__gt,column__gtecompare with the value;column__inmatches any value of an array;column__likematches an SQLLIKEpattern, andcolumn__ilikematches it case-insensitively on PostgreSQL.
The conditions of a filter combine with AND.
Named filters
To let clients filter by a condition of your own, such as a search, define a modifier on the model:
import type { AnyQueryBuilder } from "objection"
class PostModel extends Model {
// tableName and relationMappings as in Relations.
static modifiers = {
search: (query: AnyQueryBuilder, term: string) => query.where("text", "like", `%${term}%`),
}
}The client passes the value of the filter to the modifier:
query {
posts(filter: { author_id: 1, search: "hello" }) {
id
text
}
}To define modifiers for the graph only, pass them as modifiers of r.model:
const graph = r.graph({
Post: r.model(PostModel, {
modifiers: {
search: (query, term) => query.where("text", "like", `%${term}%`),
},
}),
})Name a modifier differently from the columns: a modifier replaces the column filter of the same name.
Reuse a filter outside GraphQL
To apply a filter to a query of your own, such as to count the rows that a filtered list holds, use filterQuery:
import { filterQuery } from "objection-graphql-resolver"
const resolvers = {
Query: {
posts_count: (_parent, { filter }) => {
return filterQuery(PostModel.query(), filter).resultSize()
},
},
}filterQuery applies the modifiers of the model; pass others as modifiers in its third argument, along with context for them. It does not apply modify of the type, so add the same restriction of rows to the query yourself.