GraphQL Compexity Formula

After some team shifting, I’ve inherited an early stage project using Gatsby and GraphQL for dynamic pages constructed by content authors. There is a page model with a content field that allows linking to multiple components, and each of those components can contain modular content blocks. The page query has fragments for each component type and those component fragments contain fragments for their supported blocks like what is explained here. Static pages are being built using switch statements on the __typename similar to the answer in this post.

Since this project is still in its infancy, all of that is working great. But as more components get built, I’m concerned about running into limit issues with query complexity. I’ve been experimenting with the model and block fragments I do have in the API Explorer, and it seems like the x-complexity value is calculated from the most complex fragment path rather than all fragments. Is that a correct understanding? For example, the query below scores at 110. If I only remove the HeroComponentRecord fragment, it still scores at 110. If I only remove the ListComponentRecord fragement the score drops to 97. If I add a 2nd field to NumberListItemRecord I still get 110, but if I add a 3rd field I get 111 because the NumberListItemRecord fragment now requests more fields than the StatisticListItemRecord fragment. If that understanding is correct, then it seems like I shouldn’t run into any limit issues if I continue down this path for component development.

{
  allPages {
    id
    pageTitle
    titleSuffix
    pageContent {
        __typename
        ... on ListComponentRecord {
          title
          subtitle
          theme
          listItems {
            __typename
            ... on NumberedListItemRecord {
              text
            }
            ... on StatisticListItemRecord {
              description
              source
            }
          }
        }
      	... on HeroComponentRecord {
          subtitle
          header
          createdAt
        }
    }
  }
}
1 Like

hello @erin.rasmussen from the query looks like you are not using our source plugin, is that intentional? If you use the plugin you’ll never encounter any complexity limit as it’s all run on the frontend side.

If you still want to run the GraphQL call on our API (it’s perfectly fine!) your reasoning looks good. The fragments don’t count as they are only used when performing the query, but you are right about taking into consideration only the most complex block in the complexity calculation.

1 Like

The project is using the gatsby-source-datocms plugin. I’ve been doing some experimentation with our project and reading the documentation because I took over this project from a developer who no longer works at my company. I saw the complexity documentation page and was trying to see the x-complexity header value in the GraphiQL explorer running locally. I didn’t see one there so I tried building a similar query in the API Explorer tab of our admin.datacms.com portal. I was more focused on the result and with auto-complete I didn’t really notice the query modifications I had to make to get the query working there. The query I pasted in the initial post is from the admin portal exploration. This makes sense to me now and it seems like I have nothing to worry about. Thanks!

1 Like