Skip to content

About

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

Topics

Resources

Security policy

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

graphql-cost-guardian — Analyze and limit the cost of GraphQL queries using configurable field costs

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

CI License: MIT npm Ask DeepWiki Socket Node 20+


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 | DocumentNode

A GraphQL query string or a parsed DocumentNode from graphql. Both analyzeCost and the function returned by createCostMiddleware accept either form.

options

Type: object

Cost values and multipliers must be finite, non-negative numbers. Limits must be non-negative numbers; Infinity means unbounded. Invalid numeric options throw TypeError.

Named fragments are counted at every reachable spread. Fragment wrappers do not add field nesting depth, and unused fragments are ignored.

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

About

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

Topics

Resources

Security policy

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Used by

Contributors

Languages