Collection filters
The GraphQL Content API allows users to specify filters on root collection queries.
Collections could be filtered by different fields or combination of fields that contain collection items. There are general and type specific filters:
For each content type the schema defines an input type to filter entries of that content type. For example, for the type FriendlyUser structured in the following way:
The schema defines the following filter input type:
Filter inputs can be passed to collection queries of their corresponding type to filter out mutations and the result set.
For example, to find all FriendlyUsers whose name is “Frank” or “Francine” and who are older than 30 years, write the following query:
Limitations
It is not possible to filter on fields of type Object or RichText. There’s an exemption in the case of the ContentfulMetadata type.
_contains filter is case insensitive and must be at least 2 characters long to work. The _contains filter is analogous to the [match] filter in the REST API content. Check the documentation of the [match] operator for more information about the details of full-text search in contentful.
For performance reasons it is not recommended to use the _contains filter when searching for slugs or text IDs. Please use the equality search instead.
Filter generation
Filter input types are derived from the content model, just like the output types. For each content type, one filter input type is derived. The user can pass it to the corresponding root collection query.
Each filter input type has the sys, AND, and OR fields as well as additional field type-specific filters for every field.
Name of the filter input type is derived from the output type by appending Filter to it.
Logical connectives
Each filter input type has two special fields AND and OR used to logically combine filters.
If multiple fields are specified on a filter, they get connected with an implicit AND:
And result in the following equivalent query:
Both queries return all the friendly users between the age of 30 to 40 and are named either Hans or Joe.
Filters by field type
For each field in a content type a set of filter fields is added to the content type’s filter input type. The type of filters is determined by the field type.
Symbol and Text
GraphQL Content API does not distinguish between Symbol and Text types and generates the same filters for both.
For example, if the content type FriendlyUser has a Symbol field name, the following types are generated:
Number and Integer
Filter names for Integer and Number types are the same. They only differ in the input types for values. For Integer fields the value type is Int, whereas for Number fields the type is Float.
For example, if the content type FriendlyUser has an Integer field age, the following types are generated:
Boolean
Boolean filter accepts values of type Boolean and can only be used on fields with type Boolean.
For example, if the content type FriendlyUser has a Boolean field employed, the following types are generated:
Date
For fields with type Date the value types are DateTime. The value for filter should be provided as a full DateTime value in ISO-8601 format (e.g. yyyy-mm-ddThh:mm:ss:sssZ).
For example, if the content type FriendlyUser has a DateTime field birthday, the following types are generated:
Location
For fields with type Location the value types are either Circle or Rectangle.
The Circle scalar type has the following format:
where lat and lon are coordinates of the center of the circle and radius its radius in kilometers.
The Rectangle scalar type has the following format:
where topLeftLat with topLeftLon are the coordinates of the top left corner of the rectangle, and bottomRightLat with bottomRightLon are the coordinates of the bottom right corner of the rectangle.
For example, if the content type FriendlyUser has a Location field place, the following types are generated:
Array
For Array fields with the value type String. The value for the filter should be an array of string values.
For example, if the content type FriendlyUser has an Array field nicknames, the following types are generated:
Link
For Link fields with a single linkContentType validation. Filtering depth is limited to one level of relationships.
The collection filter input type has a property corresponding to the field name. The type of this input filter property has filters for all the linked fields (without nested Link fields).
sys filters
Every filter input type has a sys property. The type of the sys filter property is the statically defined SysFilter type.
Similar to other field filters the SysFilter input type is generated from the Sys output type. For each field in the Sys type, a set of corresponding filters are added to SysFilter.
The following is an example of a query for a list of entries by IDs:
contentfulMetadata filters
Every filter input type has a contentfulMetadata property. The type of the contentfulMetadata filter property is the statically defined ContentfulMetadataFilter type.
The ContentfulMetadataFilter input type is generated from the tags field in the ContentfulMetadata type and its id subfield in the ContentfulTag type.
The following is an example of a query for a list of entries across content types by tag presence and tag IDs:
Nested collection filters
You can filter a multi reference field collection if the field contains a validation rule that makes it accept only specific content types.
If the reference field only accepts a single content type, then you can filter by any field on that content type.
On the other hand, if the reference field accepts multiple content types, then you can filter by any field that is common across all of those content types.
A field is considered common if it has the same apiName (field id) and type on all content types. Consider you have the following content types:
Cat- field Name:
Cat Name, field Id:name, type: text - field Name:
Legs, field Id:legs, type: number - field Name:
Lives Left, field id:livesLeftOfNine, type: number
- field Name:
Dog- field Name:
Dog Name, field Id:name, type: text - field Name:
Legs, field Id:legs, type: boolean - field Name:
Likes Walks, field id:likesWalks, type: boolean
- field Name:
Person- field Name:
Pets, field Id:pets, type: Reference, validations: Accept only specified entry types: Cat, Dog
- field Name:
On Person you will be able to query petsCollection by the fields that have the same field id and type on Cat and Dog. Per our content types definition above: the only common field is name (same field id name and type text on both collections). The field legs will not be a common field as its type differs across the content types.
The petsCollection can be filtered by the fields common to both Cat and Dog types, such as name. It cannot be filtered by fields specific to any one content type, such as livesLeftOfNine or likesGoingForWalks.
Note: When you filter a reference field which accepts more than one content type, the complexity of your query increases by the number of content types the field can accept.