
Security News
Axios Supply Chain Attack Reaches OpenAI macOS Signing Pipeline, Forces Certificate Rotation
OpenAI rotated macOS signing certificates after a malicious Axios package reached its CI pipeline in a broader software supply chain attack.
graphql-complexity-validation
Advanced tools
A lightweight, framework-agnostic GraphQL validation rule to limit query complexity and protect your server from expensive queries.
✅ Zero dependencies
✅ Compatible with graphql-js validation
✅ Works with Apollo Server, GraphQL Yoga, Envelop, NestJS
✅ Supports fragments, inline fragments, and introspection
✅ Fully typed (TypeScript)
graphql ^14 | ^15 | ^16npm install graphql-complexity-validation
or
yarn add graphql-complexity-validation
import { validate, parse } from "graphql";
import { createComplexityLimitRule } from "graphql-complexity-validation";
const errors = validate(schema, parse(query), [
createComplexityLimitRule({
maxComplexity: 10,
}),
]);
If the query exceeds the configured complexity, a validation error is returned.
1__schema, __type, etc.) are ignored by defaultExample:
query {
user {
posts {
comments {
id
}
}
}
}
Complexity (default):
user(1)
└─ posts(1)
└─ comments(1)
└─ id(1)
Total = 4
createComplexityLimitRule({
maxComplexity: number; // required
defaultCost?: number; // default: 1
fieldCosts?: Record<string, number>;
ignoreIntrospection?: boolean; // default: true
message?: (cost, max) => string; // custom error message
});
createComplexityLimitRule({
maxComplexity: 5,
fieldCosts: {
posts: 3,
comments: 2,
},
});
createComplexityLimitRule({
maxComplexity: 10,
message: (cost, max) =>
`Query cost ${cost} exceeds the allowed maximum of ${max}`,
});
import { ApolloServer } from "@apollo/server";
import { createComplexityLimitRule } from "graphql-complexity-validation";
const server = new ApolloServer({
schema,
validationRules: [
createComplexityLimitRule({
maxComplexity: 20,
}),
],
});
import { createYoga } from "graphql-yoga";
import { createComplexityLimitRule } from "graphql-complexity-validation";
const yoga = createYoga({
schema,
validationRules: [
createComplexityLimitRule({
maxComplexity: 20,
}),
],
});
import { envelop, useValidationRules } from "@envelop/core";
import { createComplexityLimitRule } from "graphql-complexity-validation";
const getEnveloped = envelop({
plugins: [
useValidationRules([
createComplexityLimitRule({
maxComplexity: 20,
}),
]),
],
});
import { GraphQLModule } from "@nestjs/graphql";
import { createComplexityLimitRule } from "graphql-complexity-validation";
GraphQLModule.forRoot({
schema,
validationRules: [
createComplexityLimitRule({
maxComplexity: 20,
}),
],
});
Designed for performance, clarity, and portability.
Unlike other GraphQL complexity libraries, this package:
MIT © Mateo Diaz
FAQs
GraphQL validation rule to limit query complexity
We found that graphql-complexity-validation demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
OpenAI rotated macOS signing certificates after a malicious Axios package reached its CI pipeline in a broader software supply chain attack.

Security News
Open source is under attack because of how much value it creates. It has been the foundation of every major software innovation for the last three decades. This is not the time to walk away from it.

Security News
Socket CEO Feross Aboukhadijeh breaks down how North Korea hijacked Axios and what it means for the future of software supply chain security.