Filters
The examples on this page use the db.user and db.post tables 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: async (_parent, _args, context, info) => {
return await graph.resolve(db.post, { context, info, filters: true })
},
},
}The client passes conditions as an object:
query {
posts(filter: { author_id__in: [1, 2], text__contains: "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.table(db.user, {
fields: {
id: true,
name: true,
posts: r.relation({ filters: true }),
},
}),
Post: r.table(db.post),
})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.table(db.user),
Post: r.table(db.post),
},
{ 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 of Orchid ORM's where:
column: valuematches the value, andcolumn: nullmatchesNULL;column__notmatches any other value;column__lt,column__lte,column__gt,column__gtecompare with the value;column__inmatches any value of an array;column__betweenmatches a value between the two of an array, inclusive;column__containsmatches text that contains the value, case-insensitively.
The conditions of a filter combine with AND.
Currently broken
Operators whose Orchid ORM name has a capital letter inside, such as column__notIn, column__startsWith, or column__containsSensitive, fail. Until they work, call such an operator in a named filter, such as text_starts_with: (query, value: string) => query.where({ text: { startsWith: value } }).
Named filters
To let clients filter by a condition of your own, such as a search, pass modifiers to the type:
const graph = r.graph({
Post: r.table(db.post, {
modifiers: {
search: (query, term: string) => query.where({ text: { contains: term } }),
},
}),
})The client passes the value of the filter to the modifier:
query {
posts(filter: { author_id: 1, search: "hello" }) {
id
text
}
}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 "orchid-graphql"
const resolvers = {
Query: {
posts_count: async (_parent, { filter }) => {
return await filterQuery(db.post, filter).count()
},
},
}To apply named filters, pass them as modifiers in the third argument of filterQuery, along with context for them. It does not apply modify of the type, so add the same restriction of rows to the query yourself.