What is graphql-query-complexity?
The graphql-query-complexity package is a tool for analyzing and limiting the complexity of GraphQL queries. It helps in preventing abuse by ensuring that queries do not exceed a specified complexity threshold, which can be crucial for maintaining the performance and security of a GraphQL API.
What are graphql-query-complexity's main functionalities?
Complexity Analysis
This feature allows you to calculate the complexity of a given GraphQL query. The code sample demonstrates how to use the `getComplexity` function along with a simple estimator to determine the complexity of a basic query.
const { getComplexity, simpleEstimator } = require('graphql-query-complexity');
const { graphql, buildSchema } = require('graphql');
const schema = buildSchema(`
type Query {
hello: String
}
`);
const query = '{ hello }';
const complexity = getComplexity({
schema,
query,
estimators: [
simpleEstimator({ defaultComplexity: 1 })
]
});
console.log('Query Complexity:', complexity);
Complexity Limiting
This feature allows you to enforce a maximum complexity limit on GraphQL queries. The code sample shows how to calculate the complexity of a query and throw an error if it exceeds a predefined maximum complexity.
const { getComplexity, simpleEstimator } = require('graphql-query-complexity');
const { graphql, buildSchema } = require('graphql');
const schema = buildSchema(`
type Query {
hello: String
}
`);
const query = '{ hello }';
const complexity = getComplexity({
schema,
query,
estimators: [
simpleEstimator({ defaultComplexity: 1 })
]
});
const maxComplexity = 10;
if (complexity > maxComplexity) {
throw new Error(`Query is too complex: ${complexity}. Maximum allowed complexity: ${maxComplexity}`);
}
console.log('Query is within the allowed complexity range.');
Other packages similar to graphql-query-complexity
graphql-depth-limit
The graphql-depth-limit package provides a way to limit the depth of GraphQL queries. While graphql-query-complexity focuses on the overall complexity of a query, graphql-depth-limit specifically targets the depth of nested queries. This can be useful for preventing deeply nested queries that could potentially cause performance issues.
graphql-cost-analysis
The graphql-cost-analysis package is another tool for analyzing the cost of GraphQL queries. It allows you to define custom cost rules and calculate the cost of a query based on those rules. Compared to graphql-query-complexity, graphql-cost-analysis offers more flexibility in defining what constitutes the 'cost' of a query.
GraphQL Query Complexity Analysis for graphql-js
This library provides GraphQL query analysis to reject complex queries to your GraphQL server.
This can be used to protect your GraphQL servers against resource exhaustion and DoS attacks.
Works with graphql-js reference implementation.
Installation
Install the package via npm
npm install -S graphql-query-complexity
Usage
Create the rule with a maximum query complexity:
import queryComplexity, {
simpleEstimator
} from 'graphql-query-complexity';
const rule = queryComplexity({
maximumComplexity: 1000,
variables: {},
onComplete: (complexity: number) => {console.log('Determined query complexity: ', complexity)},
createError: (max: number, actual: number) => {
return new GraphQLError(`Query is too complex: ${actual}. Maximum allowed complexity: ${max}`);
},
estimators: [
simpleEstimator({
defaultComplexity: 1
})
]
});
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)fieldConfigEstimator
: The field config estimator lets you set a numeric value or a custom estimator
function in the field config of your schema.legacyEstimator
: The legacy estimator implements the logic of previous versions. Can be used
to gradually migrate your codebase to new estimators.- 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:
type ComplexityEstimatorArgs = {
type: GraphQLCompositeType,
field: GraphQLField<any, any>,
args: {[key: string]: any},
childComplexity: number
}
type ComplexityEstimator = (options: ComplexityEstimatorArgs) => number | void;
Usage with express-graphql
To use the query complexity analysis validation rule with express-graphql, use something like the
following:
import queryComplexity from 'graphql-query-complexity';
import express from 'express';
import graphqlHTTP from 'express-graphql';
import schema from './schema';
const app = express();
app.use('/api', graphqlHTTP(async (request, response, {variables}) => ({
schema,
validationRules: [ queryComplexity({
maximumComplexity: 1000,
variables,
onComplete: (complexity: number) => {console.log('Query Complexity:', complexity);},
}) ]
})));
Prior Art
This project is inspired by the following prior projects: