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-cost-guardian

v1.0.0

Published

Analyze and limit the cost of GraphQL queries using configurable field costs

Readme

graphql-cost-guardian

Analyze and limit the cost of GraphQL queries using configurable field costs

Install

npm install graphql-cost-guardian graphql

Usage

import analyzeCost from 'graphql-cost-guardian';

const result = analyzeCost(`
	query {
		users {
			name
			posts {
				title
				comments {
					body
				}
			}
		}
	}
`, {
	fieldCosts: {'Query.users': 10},
	maxCost: 100,
	maxDepth: 5,
});

console.log(result.cost);
// => 14

console.log(result.depth);
// => 4

Middleware

import {createCostMiddleware} from 'graphql-cost-guardian';

const analyze = createCostMiddleware({
	maxCost: 500,
	maxDepth: 10,
	defaultCost: 1,
	listCostFactor: 10,
});

// Use in your GraphQL server middleware
const result = analyze(incomingQuery);

API

analyzeCost(query, options?)

Analyze the cost of a GraphQL query.

query

Type: string

A GraphQL query string.

options

Type: object

fieldCosts

Type: Record<string, number>
Default: {}

Mapping of TypeName.fieldName to custom cost values.

defaultCost

Type: number
Default: 1

Default cost per field when not specified in fieldCosts.

depthCostFactor

Type: number
Default: 1

Multiplier applied per nesting depth level.

listCostFactor

Type: number
Default: 10

Assumed list size multiplier for fields with list-related arguments (first, last, limit).

maxCost

Type: number

Maximum allowed cost. Throws CostExceededError when exceeded.

maxDepth

Type: number

Maximum allowed query depth. Throws DepthExceededError when exceeded.

Return value

Type: {cost: number, depth: number, fields: Map<string, number>}

createCostMiddleware(options)

Returns a function (query) => CostAnalysis with the given options baked in.

CostExceededError

Thrown when query cost exceeds maxCost. Has cost and maxCost properties.

DepthExceededError

Thrown when query depth exceeds maxDepth. Has depth and maxDepth properties.

Related

  • graphql — The JavaScript reference implementation for GraphQL

License

MIT