How to filter CraftCMS GraphQL queries in GatsbyJS

Oscar de la Hera Gomez
Three flowers that represent CraftCMS, GraphQL & GatsbyJS side by side. Beneath them sits the text "Filter Queries".

A guide on recommendations for filtering CraftCMS GraphQL queries in GatsbyJS.

As part of optimizing your GraphQL queries, it's important to narrow down your requests to the smallest possible queries to guarantee performance and success. The following guide offers our lessons from the past two years in filtering down queries to the smallest possible size.

Please note we recommend that you use these in tandem with each other.

Filter by Slug

A sample of a GraphQL query filter by slug.

Filtering by slug is recommended for page queries or for gathering exact entries based on slug, or slugs if passed in as an array.

If used as a single entry, we recommend using it as part of an entry query, rather than an entries query which must be used if searching for multiple entries. We also recommend using the slug filter in tandem with Section and Type handles to guarantee that you find the right one, and in the event of searching for multiple entries, get rid of all the empty objects that come if you look for larger queries.

Filter by Section

A sample of a GraphQL query filtered by section handle.

Filtering by section is recommended when trying to fetch a collection of entries (for example, blog posts).

As this seeks to find multiple entries, you must make an entries query and pass in the section handle as part of an array. As demonstrated in the example above, it can be used to fetch multiple sections to guarantee not having to filter through empty objects.

Filter by Type

A sample of a GraphQL query filtered by Type.

Filtering by type is recommended when trying to fetch a collection of entries that fall under a section, but only limit it to that type of entry type.

Similarly to sections, you can filter for multiple types by passing strings into your query as part of an array.

Filter by Limit & Offset

A sample of a GraphQL query filtered by limit and offset.

As demonstrated in our pagination GraphQL tutorial, you can filter entries by limit and offset (i.e. limit of 50, offset 0 returns entries 0 to 50, limit of 50 offset 50 returns entries 50 to 100) to reduce the query size and speed up response time.

Any Questions ?

We are actively looking for feedback on how to improve this resource. Please send us a note to inquiries@delasign.com with any thoughts or feedback you may have.

delasign logo

Book a Free Consultation.

An icon of an email.

Click here to email us.

Fill in the details below to book a free consultation or to let us know about something else. Whatever it is, we are here to help.

How can we help you ?

Contact Details