ContentStack has a 8kb request size limit on GraphQL queries. This guide will help you optimise your queries to stay within the limit.
Title
, Description
, Links
across multiple Global Fields, you can’t create that once, create 1 fragment and then use that fragment in all your other Global Fields. Take a look at the ideal.graphql
and reality.graphql
examples below and ask yourself this question for each file - “What happens if I introduce a new Link type?”.ideal.graphql
you update 1 Global Field and 1 Fragment. For reality.graphql
you need to update x
number of Global Fields and x
number of Fragments. The ideal.graphql
example isn’t a radical idea either, Blocks/Global Fields are meant to be re-usable and create a single source of truth, ContentStack just doesn’t allow you to do this for some reason.
Page
content type with a Modular Block field called Flexible Content
. If you had 3 blocks that reference Global Fields, ContentStack would generate 3 new GraphQL types for each block.ideal.graphql
examples are how I’ve structured similar fields & fragments in other GraphQL-based CMSs.
fetch
based handler that I’ve written. You may need to adjust this for your own use case, for example when using Apollo Client or similar.body
for my fetch
request, I run the query through this package.
__typename
and append Fragment
to it. This has been fine in the past however, with ContentStack’s size limit, I needed to reduce the number of bytes my fragment names were taking up.
I am using the package nanoid to generate these random strings.
fragmentName
in my pigeon configuration, I would use the frgName
function. I am creating a lookup
table to ensure if I have the same __typename
I will always use the same fragment name. In the RARE case that I have a collision, I will continually generate a new fragment name until I find one that isn’t in the lookup
table.
With 53 possible characters, at a length of 4 characters per name, there are 7,890,481 possible fragment names.
Before this optimisation, my query size was:
DEBUG_QUERY_SIZE
environment variable that I can set to "true"
to enable this logging. I grab the query name if it is available for better logging, calculate the bytes of the query and then log the size percentage. If the percentage is over 80%, I log it in red, if it is over 50% I log it in yellow, otherwise it is green.
body
not just the query. ContentStack will limit you on the size of the body
you send to their API, the only thing we can really control is the query, so that is what I focused on to optimise.