You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -207,24 +230,33 @@ const result = await graphql({
207
230
console.log(result);
208
231
```
209
232
210
-
If the query exceeds the defined complexity limit, GraphQL.js will return a validation
211
-
error and skip execution.
233
+
<Callout type="info" emoji="ℹ️">
234
+
Only use complexity rules in validation if you're sure the analysis is cycle-safe.
235
+
Otherwise, run complexity checks after validation and before execution.
236
+
</Callout>
237
+
238
+
## Complexity in trusted environments
239
+
240
+
In environments that use persisted or precompiled operations, complexity analysis is still
241
+
useful, just in a different way. You can run it at build time to:
212
242
213
-
This approach is useful when you want to apply global complexity rules without needing
214
-
to modify resolver logic or add separate middleware.
243
+
- Warn engineers about expensive operations during development
244
+
- Track changes to operation cost across schema changes
245
+
- Define internal usage budgets by team, client, or role
215
246
216
247
## Best practices
217
248
218
-
- Set conservative complexity limits at first, and adjust them based on observed usage.
219
-
- Use field-level estimators to better reflect real backend cost.
220
-
- Log query complexity in development and production to identify inefficiencies.
221
-
- Apply stricter limits for public or unauthenticated clients.
222
-
- Combine complexity limits with depth limits, persisted queries, or operation
223
-
whitelisting for stronger control.
249
+
- Use trusted documents in production when possible.
250
+
- Use complexity analysis as a development-time safeguards.
251
+
- Avoid running untrusted operations without additional validation and cost checks.
252
+
- Account for list fields and abstract types, which can significantly increase cost.
253
+
- Avoid estimating complexity before validation unless you're confident in your tooling.
254
+
- Use complexity analysis as part of your layered security strategy, alongside depth limits,
255
+
field guards, and authentication.
224
256
225
257
## Additional resources
226
258
227
-
- [`graphql-query-complexity`](https://github.com/slicknode/graphql-query-complexity): A static analysis tool for measuring query cost in GraphQL.js servers
259
+
- [`graphql-query-complexity`](https://github.com/slicknode/graphql-query-complexity): A community-maintained static analysis tool
228
260
- [`graphql-depth-limit`](https://github.com/graphile/depth-limit): A lightweight tool to restrict the maximum query depth
229
261
- [GraphQL Specification: Operations and execution](https://spec.graphql.org/draft/#sec-Language.Operations)
230
262
- [GraphQL.org: Security best practices](https://graphql.org/learn/security/)
0 commit comments