npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

graphql-complexity-validation

v1.0.4

Published

GraphQL validation rule to limit query complexity

Readme

graphql-complexity-validation

CI npm downloads license typescript

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)

Requirements

  • Node.js >= 14
  • graphql ^14 | ^15 | ^16

Installation

npm install graphql-complexity-validation

or

yarn add graphql-complexity-validation

Basic Usage

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.


How Complexity Is Calculated

  • Each field has a cost
  • Default field cost is 1
  • Nested fields add their cost recursively
  • Fragments and inline fragments are fully supported
  • Introspection fields (__schema, __type, etc.) are ignored by default
  • Complexity is calculated per operation during GraphQL's validation phase.
  • If a document contains multiple operations, each operation is validated independently.

Example:

query {
  user {
    posts {
      comments {
        id
      }
    }
  }
}

Complexity (default):

user(1)
└─ posts(1)
   └─ comments(1)
      └─ id(1)

Total = 4

Configuration Options

createComplexityLimitRule({
  maxComplexity: number;           // required
  defaultCost?: number;            // default: 1
  fieldCosts?: Record<string, number>;
  ignoreIntrospection?: boolean;   // default: true
  message?: (cost, max) => string; // custom error message
});

Custom Field Costs

createComplexityLimitRule({
  maxComplexity: 5,
  fieldCosts: {
    posts: 3,
    comments: 2,
  },
});

Custom Error Message

createComplexityLimitRule({
  maxComplexity: 10,
  message: (cost, max) =>
    `Query cost ${cost} exceeds the allowed maximum of ${max}`,
});

Apollo Server

import { ApolloServer } from "@apollo/server";
import { createComplexityLimitRule } from "graphql-complexity-validation";

const server = new ApolloServer({
  schema,
  validationRules: [
    createComplexityLimitRule({
      maxComplexity: 20,
    }),
  ],
});

GraphQL Yoga

import { createYoga } from "graphql-yoga";
import { createComplexityLimitRule } from "graphql-complexity-validation";

const yoga = createYoga({
  schema,
  validationRules: [
    createComplexityLimitRule({
      maxComplexity: 20,
    }),
  ],
});

Envelop

import { envelop, useValidationRules } from "@envelop/core";
import { createComplexityLimitRule } from "graphql-complexity-validation";

const getEnveloped = envelop({
  plugins: [
    useValidationRules([
      createComplexityLimitRule({
        maxComplexity: 20,
      }),
    ]),
  ],
});

NestJS (GraphQLModule)

import { GraphQLModule } from "@nestjs/graphql";
import { createComplexityLimitRule } from "graphql-complexity-validation";

GraphQLModule.forRoot({
  schema,
  validationRules: [
    createComplexityLimitRule({
      maxComplexity: 20,
    }),
  ],
});

Why This Library?

  • No schema traversal at runtime
  • No directive setup
  • No Apollo-specific plugins
  • Uses native GraphQL validation
  • Predictable and easy to reason about

Designed for performance, clarity, and portability.


Comparison

Unlike other GraphQL complexity libraries, this package:

  • Does not require schema traversal
  • Does not rely on directives
  • Does not depend on Apollo internals
  • Works entirely at the GraphQL validation layer

License

MIT © Mateo Diaz