🚀 Big News: Socket Acquires Coana to Bring Reachability Analysis to Every Appsec Team.Learn more

graphql-query-complexity

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

graphql-query-complexity

Validation rule for GraphQL query complexity analysis

1.1.0-beta.0
Version published
Weekly downloads
0
Maintainers
1
Weekly downloads
 
Created

What is graphql-query-complexity?

The graphql-query-complexity package is a tool for analyzing and limiting the complexity of GraphQL queries. It helps in preventing abuse by ensuring that queries do not exceed a specified complexity threshold, which can be crucial for maintaining the performance and security of a GraphQL API.

What are graphql-query-complexity's main functionalities?

Complexity Analysis

This feature allows you to calculate the complexity of a given GraphQL query. The code sample demonstrates how to use the `getComplexity` function along with a simple estimator to determine the complexity of a basic query.

const { getComplexity, simpleEstimator } = require('graphql-query-complexity');
const { graphql, buildSchema } = require('graphql');

const schema = buildSchema(`
  type Query {
    hello: String
  }
`);

const query = '{ hello }';

const complexity = getComplexity({
  schema,
  query,
  estimators: [
    simpleEstimator({ defaultComplexity: 1 })
  ]
});

console.log('Query Complexity:', complexity);

Complexity Limiting

This feature allows you to enforce a maximum complexity limit on GraphQL queries. The code sample shows how to calculate the complexity of a query and throw an error if it exceeds a predefined maximum complexity.

const { getComplexity, simpleEstimator } = require('graphql-query-complexity');
const { graphql, buildSchema } = require('graphql');

const schema = buildSchema(`
  type Query {
    hello: String
  }
`);

const query = '{ hello }';

const complexity = getComplexity({
  schema,
  query,
  estimators: [
    simpleEstimator({ defaultComplexity: 1 })
  ]
});

const maxComplexity = 10;
if (complexity > maxComplexity) {
  throw new Error(`Query is too complex: ${complexity}. Maximum allowed complexity: ${maxComplexity}`);
}

console.log('Query is within the allowed complexity range.');

Other packages similar to graphql-query-complexity

FAQs

Package last updated on 08 Apr 2025

Did you know?

Socket

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.

Install

Related posts