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-safe-guards

v1.2.3

Published

Protect GraphQL APIs from deep and expensive queries using depth and complexity limits

Readme

CI npm downloads license typescript

graphql-safe-guards 🛡️

Protect your GraphQL API from deep, expensive, and abusive queries by enforcing query depth and query complexity limits before execution.

A tiny, framework-agnostic utility that combines depth limiting and query complexity validation using native GraphQL validation rules.

Designed for production GraphQL APIs, public endpoints, and high-traffic systems.


🚨 The Problem

GraphQL gives clients a lot of power — sometimes too much.

Without safeguards, a single query can:

  • Exhaust database connections
  • Cause CPU spikes
  • Trigger accidental or malicious DoS attacks
  • Degrade performance for all users

This is especially dangerous in public APIs or high-traffic applications.


✅ The Solution

graphql-safe-guards acts as a logical firewall for GraphQL queries.

Before any resolver executes, it:

  • Validates query depth
  • Calculates query complexity
  • Rejects unsafe operations early

No resolvers are executed.
No database calls are made.


✨ Features

  • ✅ Limit query depth
  • ✅ Limit query complexity
  • ✅ Blocks abusive or accidental DoS-style queries
  • ✅ Uses native GraphQL validation (AST-based)
  • ✅ No schema directives
  • ✅ No runtime execution cost
  • ✅ Framework-agnostic
  • ✅ Production-ready presets

Installation

npm install graphql-safe-guards
# or
yarn add graphql-safe-guards

🚀 Quick Start (Apollo Server)

import { ApolloServer } from "@apollo/server";
import { createSafeGuards } from "graphql-safe-guards";

const server = new ApolloServer({
  schema,
  validationRules: createSafeGuards({
    depth: 3,
    complexity: 10,
  }),
});

Any query exceeding the limits will be rejected before execution.

Presets (Recommended)

graphql-safe-guards includes opinionated, production-ready presets for common use cases.

createSafeGuards({ preset: "strict" });
createSafeGuards({ preset: "balanced" });
createSafeGuards({ preset: "relaxed" });

| Preset | Depth | Complexity | Use case | | -------- | ----- | ---------- | --------------------------- | | strict | 3 | 50 | Public APIs, read-only APIs | | balanced | 4 | 100 | Private frontends | | relaxed | 6 | 200 | Admin / internal tools |

Environment-based setup

createSafeGuards({
  preset: process.env.NODE_ENV === "production" ? "strict" : "balanced",
});

Configuration

createSafeGuards({
  depth?: number;       // default: 3 (strict preset)
  complexity?: number;  // default: 50 (strict preset)

  /**
   * If true, GraphQL introspection queries are ignored
   * by depth and complexity validation.
   *
   * Useful for GraphQL Playground / Apollo Sandbox.
   */
  ignoreIntrospection?: boolean; // default: false
});

🔐 Security Note — Introspection⚠️

  • This library does not enable or disable GraphQL introspection

  • Introspection is controlled by your GraphQL server (e.g. Apollo Server) For private APIs with documentation enabled, the recommended setup is:

const server = new ApolloServer({
  schema,
  introspection: true,
  validationRules: createSafeGuards({
    depth: 3,
    complexity: 10,
    ignoreIntrospection: true,
  }),
});

🔥 What It Protects Against

Deeply nested queries

a {
  b {
    c {
      d {
        e {
          f {
            g
          }
        }
      }
    }
  }
}

High-cost queries

posts(limit: 100) {
  comments(limit: 100) {
    author {
      posts(limit: 100)
    }
  }
}

🧠 How It Works

  • Parses the GraphQL AST
  • Computes depth and complexity
  • Runs during the validation phase
  • Blocks unsafe queries before execution

Fast, predictable, and scalable.


🧩 Internals

This package composes and unifies:

  • graphql-safe-depth
  • graphql-complexity-validation

Validated through integration tests using native graphql-js validation.


Supported Frameworks

  • Apollo Server
  • GraphQL Yoga
  • Envelop
  • NestJS GraphQL
  • Plain graphql-js

📊 Production Recommendations

  • For best results, combine this library with:
  • DataLoader (avoid N+1 queries)
  • Pagination on list fields
  • HTTP caching / CDN
  • Persisted queries (hash-based)

🗺️ Roadmap

v1.x (current)

  • ✅ Combine depth + complexity validation
  • ✅ Presets support (strict, balanced, relaxed)
  • ✅ Backward-compatible API
  • ✅ Integration tests with graphql-js
  • 🔜 Additional presets based on community feedback

Roadmap items may change based on feedback and real-world usage.

⭐ Support

If this library helped you secure your GraphQL API, please consider giving it a ⭐ on GitHub.

License

MIT © Mateo Diaz