Skip to content

Filter, Sort and Group Without a Query Language

Every app needs lists: the blue products, the orders of this customer, the reviews below three stars, this month's errors by message. Many APIs answer with a few fixed filters per endpoint, and the moment you need another, you're writing a new endpoint, or a raw database query that your API now has to defend against.

ObjectsGrid takes the other route: one typed filter language, the same for every type, in REST and in GraphQL. Its shape follows Prisma, so it may look familiar.

where

A where holds conditions on fields. All must hold:

curl -G https://apis.objectsgrid.com/products \
  -H 'X-ObjectsGrid-AccessPoint: EU' -H "Authorization: Bearer $TOKEN" \
  --data-urlencode 'where={"color": "Blue", "weight": {"value": {"lt": 300}}, "offers": {"isEmpty": false}}' \
  --data-urlencode 'orderBy={"meta": {"createdAt": "desc"}}' \
  -d first=20
On Conditions
A string equals, in, notIn, lt, gt, contains, startsWith, endsWith, not, and mode: insensitive
A number or a date-time equals, in, notIn, lt, lte, gt, gte, not
A list of values has, hasSome, hasEvery, isEmpty
An embedded object a where on its fields
A list of objects some, every, none, isEmpty

AND, OR and NOT combine wheres. In GraphQL the same where is typed, so your editor completes it and the server refuses a wrong field before it runs:

{
  products(where: {color: {equals: "Blue"}, weight: {value: {lt: 300}}},
           orderBy: [{meta: {createdAt: desc}}], first: 20) {
    items { id sku name { at_language at_value } }
    pageInfo { endCursor hasNextPage }
  }
}

Text in several languages

Translated text, such as name, is a list of values each tagged with its language. A condition on one element holds for that element, so this finds products named "Boot" in English, not those named "Boot" in German and something else in English:

{"name": {"some": {"at_value": "Boot", "at_language": "en"}}}

Through references

Objects point to each other by references. A reference takes conditions on its own fields, and in REST, is puts conditions on the object it points to:

{"brand": {"is": {"name": {"some": {"at_value": {"equals": "Northpeak"}}}}}}

That's "the products of the brands named Northpeak", without reading the brands first.

Pages that never skip

first takes 1 to 100 objects; the page's endCursor goes in after for the next one. Cursors beat offsets: an object created while you page doesn't shift everything by one, and the object's id always ends the sort order, so no object appears twice or not at all.

Count and group

Add count=true in REST, or select totalCount in GraphQL, to count the matches. To summarize, group:

curl -G https://apis.objectsgrid.com/ratings \
  -H 'X-ObjectsGrid-AccessPoint: EU' -H "Authorization: Bearer $TOKEN" \
  --data-urlencode 'groupBy={"by": ["reviewAspect"], "_count": true, "_avg": {"ratingValue": true},
                             "having": {"ratingValue": {"_avg": {"gte": 4}}}}'
{"groups": [{"reviewAspect": "comfort", "_count": 12, "_avg": {"ratingValue": 4.3}}]}

Group by up to 3 fields, with _count, _sum, _avg, _min and _max, and keep the groups you want with having. The same works on frontend log events: "which errors happen most this week" is one groupBy.

Safe by construction

A typed filter isn't only convenient:

  • No injection. There's no query string to build, so there's nothing to inject into. A wrong field or operator is a 400 naming it.
  • Bounded. A where holds at most 50 conditions, nested at most 5 levels, so no request can ask for an unbounded amount of work.
  • Policy-aware. Lists leave out the objects your access policy doesn't let the caller read, and a caller can't filter or sort on a field hidden from them, so a filter can't be used to guess a hidden value.

One request, one bill

A list is one request however many objects it returns, and a GraphQL request with several lists is one request too. A page that needs products, their brands and the top reviews is one call. Lists are seconds behind writes, as they come from search; read a single object by id when you need what was just written.

The lists guide has every operator and limit.