⬡ GraphQL Intermediate

What is query complexity analysis in GraphQL?

Answer

Query complexity analysis assigns a cost score to each field and rejects queries whose total complexity exceeds a threshold. This is more nuanced than depth limiting — a shallow-but-wide query (requesting 100 list items with 50 fields each) can be more expensive than a deep query. You define costs: a scalar field might cost 1, a list field might cost 10, and a database-joining field might cost 100. The total is computed before execution and compared against a configured maximum (e.g., 1000). Libraries like graphql-query-complexity implement this. Exposing the complexity limit and cost of a query in the response helps clients optimize their queries.