Queries Utils
When building Compass queries, it might be useful to have some little utilities to not repeat the same code many times. There two types of utils for queries, building orders and building filters.
Order By
When you have a query that accepts an $orderBy
variable that let you input the field name and the
sort order, for example:
query MyDrops(
$orderBy: [drops_order_by!]
) {
drops(
order_by: $orderBy
where: {
drop_id: { _in: [14] }
}
) {
id
name
}
}
And an enum that let you choose what fields you want to order by:
enum MyDropSortFields {
ID = 'id',
NAME = 'name',
}
You can then build the variable $orderBy
with createOrderBy
:
import { Order, OrderByVariables, createOrderBy } from '@poap-xyz/utils';
const variables: OrderByVariables = {
orderBy: createOrderBy<MyDropSortFields>(MyDropSortFields.NAME, Order.ASC),
};
Filters
There are many filters with different types and for different results, normally you will have a
query that accepts a $where
variable and then you can construct the variables with the use of the
filters utils.
For example:
import {
FilterVariables,
createLikeFilter,
createBetweenFilter,
} from '@poap-xyz/utils';
const variables: FilterVariables = {
where: {
...createLikeFilter('name', 'My Awesome POAP'),
...createBetweenFilter('created_date', '2023-12-23', '2024-02-28'),
},
};
The possible filters to create are:
createLikeFilter
: searches for the value to be included case-insensitive in the field.createEqFilter
: match exact field value.createNeqFilter
: match not equal field value.createAddressFilter
: given the address match it case-insensitive.createNotNullAddressFilter
: excludes the zero and/or dead addresses.createInFilter
: if the field is contained in any of the given values.createNinFilter
: not contained in the given values.createBetweenFilter
: from and to values, mostly used for dates.createIsNullFilter
: to check if the field is null.
Use of DOT notation
All keys can be given as dot separated of nested entries when the field to filter is located inside another entity. For example, when you have the query:
query Collectors($where: collectors_bool_exp) {
collectors(where: $where) {
address
}
}
And you would like to retrieve the list of collector's addresses of a certain drop, then you can build the variables as:
const variables: FilterVariables = {
where: {
...createEqFilter('poaps.drop_id', 14),
...createNotNullAddressFilter('address'),
},
};