How to filter CraftCMS GraphQL queries in GatsbyJS
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
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
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
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
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 email@example.com with any thoughts or feedback you may have.