Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
G

graphql-query-complexity

> 后端框架
Open source

GraphQL query complexity analysis and validation for graphql-js

750 stars0 likes1 views
WebsiteGitHub

About

GraphQL query complexity analysis and validation for graphql-js

GraphQL Query Complexity Analysis for graphql-js

This library provides GraphQL query analysis to reject complex queries to your GraphQL server. It can be used to protect your GraphQL servers against resource exhaustion and DoS attacks.

This library was originally developed as part of the Slicknode GraphQL Framework + Headless CMS.

Works with graphql-js reference implementation.

Installation

Install the package via npm

bash
npm install -S graphql-query-complexity

Usage

Create the rule with a maximum query complexity:

…

Configuration / Complexity Estimators

The complexity calculation of a GraphQL query can be customized with so called complexity estimators. A complexity estimator is a simple function that calculates the complexity for a field. You can add any number of complexity estimators to the rule, which are then executed one after another. The first estimator that returns a numeric complexity value determines the complexity for that field.

At least one estimator has to return a complexity value, otherwise an exception is raised. You can for example use the simpleEstimator as the last estimator in your chain to define a default value.

You can use any of the available estimators to calculate the complexity of a field or write your own:

  • simpleEstimator: The simple estimator returns a fixed complexity for each field. Can be used as last estimator in the chain for a default value.
  • directiveEstimator: Set the complexity via a directive in your schema definition (for example via GraphQL SDL)
  • fieldExtensionsEstimator: The field extensions estimator lets you set a numeric value or a custom estimator function in the field config extensions of your schema.
  • PRs welcome...

Consult the documentation of each estimator for information about how to use them.

Creating Custom Estimators

An estimator has the following function signature:

…

Usage with express-graphql

To use the query complexity analysis validation rule with express-graphql, use something like the following:

…

Calculate query complexity

If you want to calculate the complexity of a GraphQL query outside of the validation phase, for example to return the complexity value in a resolver, you can calculate the complexity via getComplexity:

…

Prior Art

This project is inspired by the following prior projects:

  • Query complexity analysis in the Sangria GraphQL implementation.
  • graphql-cost-analysis - Multipliers and directiveEstimator

Issues· 0 open

View all issuesOpen on GitHub

No open issues yet, or sync has not completed.

> Tags

TypeScriptanalysiscomplexitydos-attackgraphql

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 17, 2026
Category后端框架
PricingOpen source

> Related tools

N
Node.js
基于 V8 的 JavaScript 运行时
D
Django
Python 高级 Web 框架
S
Spring Boot
Java 生态主流微服务框架