Increased query complexity when using fragment colocation

if I have a query like

query Posts {
  posts {
    id
    title
    description
  }
}

and extract some fields to a fragment like this

query Posts {
  posts {
    id
    title
   ... Description
  }
}

fragment Description on Post {
   id
  description
}

I have my server setup with a maximum query complexity, so the request fails with the following error

 maximum query complexity exceeded x > y

my question is, is there a way to modify the query at request time to jam in the fragment fields into the main query, so the request query will be back to

query Posts {
  posts {
    id
    title
    description
  }
}

No, that’s not possible.

Apollo Client has no knowledge about your schema, and it cannot know if posts is of the actual type Post[] or if it is of e.g. an interface that could contain other array members than Post that might be lacking description or where description is of a different, incompatible type.

Blindly inlining could cause errors both on the server (query doesn’t match schema) and on the client (unexpected types are returned).

As alternative solutions, I would recommend you contact the server team and have a discussion about the query complexity calculation - or you use something like persisted queries that are whitelisted and thus not subject to complexity calculations in the first place.

1 Like