GraphQL Filter Pipeline#
Strapi's GraphQL filter pipeline converts a client-supplied filters argument (e.g. { category: { in: ["pizza", "burger"] } }) into a SQL WHERE clause. The pipeline has four distinct stages:
- Operator registration ā GraphQL input types are built from a typed operator registry.
- Argument transformation ā GraphQL operator keys are renamed to Strapi
$-prefixed equivalents. - Sanitization ā the filter tree is traversed and illegal or empty nodes are removed.
- SQL generation ā the sanitized filter object is compiled into Knex
WHEREclauses.
1. Operator Registry#
Each filter operator is a small module with three properties: the GraphQL field name, the Strapi operator key, and a Nexus type definition:
in.tsāfieldName: 'in',strapiOperator: '$in', typed aslist(type).not-in.tsāfieldName: 'notIn',strapiOperator: '$notIn', also typed aslist(type).
All 21 operators are registered in a central index and instantiated with the Strapi instance at startup.
2. Argument Transformation (transformArgs ā graphQLFiltersToStrapiQuery)#
When a query resolver is invoked, transformArgs calls mappers.graphQLFiltersToStrapiQuery(filters, contentType) before passing arguments to the resolver.
graphQLFiltersToStrapiQuery walks the filter object and:
- For scalar attributes: calls
recursivelyReplaceScalarOperators, which renames every known operator key (e.g.inā$in,notInā$notIn). - For relation/media/component attributes: recurses into the related model's schema.
- For root-level logical operators (
and,or,not): maps them to$and,$or,$notand recurses.
The output is a plain Strapi query filter object (e.g. { category: { $in: ["pizza", "burger"] } }).
3. Sanitization (defaultSanitizeFilters)#
After transformArgs, the resolver calls strapi.contentAPI.sanitize.query, which internally runs defaultSanitizeFilters. This applies a pipeline of traverseQueryFilters passes:
- Remove keys that are neither schema attributes nor known operators .
- Remove dynamic zones, morph-to relations, passwords, and private fields .
- Remove empty plain objects and empty arrays .
The third pass is the source of a well-known behavioral quirk (see below).
traverseQueryFilters itself handles the traversal mechanics: it intercepts arrays (mapping over each element), skips nil values, and recurses into operator nodes ā treating plain-object operator values as opaque unless they look like nested filter subtrees .
4. SQL Generation (applyWhere / applyOperator)#
The sanitized filter object reaches processWhere in the database query helper, which resolves attribute metadata and dispatches each key to applyWhereToColumn ā applyOperator.
$in maps to Knex whereIn; $notIn maps to Knex whereNotIn.
The Empty-Operand Quirk#
Symptom: A query like filters: { id: { in: [] } } ā intended to return no rows ā instead returns all rows.
Cause: The sanitizer's final pass unconditionally removes any key whose value is an empty array . After that pass, { id: { $in: [] } } becomes {}, so no WHERE clause is emitted and the query matches every row.
The same applies to notIn: [] and to any other list-valued operator sent with an empty array.
What to watch for:
- Dynamically constructed filter lists that can be empty at runtime (e.g. filtering by a list of IDs that turns out to be empty after some upstream computation).
- The empty-object branch of the same check also applies: a nested filter object that becomes
{}after inner keys are sanitized away is itself removed.
Workaround: Guard against empty arrays before passing filters to GraphQL ā if the list is empty and the intended semantics is "match nothing", either skip the query entirely or substitute an impossible condition (e.g. { id: { eq: -1 } }).
Key Files#
| File | Role |
|---|---|
filters/operators/in.ts | in ā $in GraphQL operator definition |
filters/operators/not-in.ts | notIn ā $notIn GraphQL operator definition |
filters/operators/index.ts | Central operator registry (all 21 operators) |
builders/utils.ts | transformArgs ā calls graphQLFiltersToStrapiQuery |
mappers/graphql-filters-to-strapi-query.ts | GraphQL ā Strapi operator key rewriting |
resolvers/query.ts | Collection-type query resolvers; calls sanitize.query |
sanitize/sanitizers.ts | defaultSanitizeFilters ā including the empty-array removal |
traverse/query-filters.ts | traverseQueryFilters ā filter tree traversal engine |
database/.../where.ts | processWhere / applyOperator ā Knex SQL generation |