All Projects → slicknode → Graphql Query Complexity

slicknode / Graphql Query Complexity

Licence: mit
GraphQL query complexity analysis and validation for graphql-js

Programming Languages

typescript
32286 projects

Projects that are alternatives of or similar to Graphql Query Complexity

Framework
Strongly-typed JavaScript object with support for validation and error handling.
Stars: ✭ 136 (-67.92%)
Mutual labels:  graphql, validation
Typegql
Create GraphQL schema with TypeScript classes.
Stars: ✭ 415 (-2.12%)
Mutual labels:  graphql, graphql-js
Hangzhou Graphql Party
杭州 GraphQLParty 往期记录(slide,照片,预告,视频等)
Stars: ✭ 142 (-66.51%)
Mutual labels:  graphql, graphql-js
The Road To Graphql Chinese
📓The Road to GraphQL: Your journey to master pragmatic GraphQL in JavaScript
Stars: ✭ 104 (-75.47%)
Mutual labels:  graphql, graphql-js
Graphql Js
A reference implementation of GraphQL for JavaScript
Stars: ✭ 18,251 (+4204.48%)
Mutual labels:  graphql, graphql-js
Kronky
Kronky bridges the gap between Ecto and Absinthe GraphQL by listing validation messages in a mutation payload.
Stars: ✭ 112 (-73.58%)
Mutual labels:  graphql, validation
Grial
A Node.js framework for creating GraphQL API servers easily and without a lot of boilerplate.
Stars: ✭ 194 (-54.25%)
Mutual labels:  graphql, graphql-js
Graphql Rxjs
fork of Graphql which adds Observable support
Stars: ✭ 78 (-81.6%)
Mutual labels:  graphql, graphql-js
Graphql To Mongodb
Allows for generic run-time generation of filter types for existing graphql types and parsing client requests to mongodb find queries
Stars: ✭ 261 (-38.44%)
Mutual labels:  graphql, graphql-js
The Road To Graphql
📓The Road to GraphQL: Your journey to master pragmatic GraphQL in JavaScript
Stars: ✭ 203 (-52.12%)
Mutual labels:  graphql, graphql-js
Cruddl
Create a GraphQL API for your database, using the GraphQL SDL to model your schema.
Stars: ✭ 98 (-76.89%)
Mutual labels:  graphql, graphql-js
Graphql Constraint Directive
Validate GraphQL fields
Stars: ✭ 401 (-5.42%)
Mutual labels:  graphql, validation
Graphql Go Tools
Tools to write high performance GraphQL applications using Go/Golang.
Stars: ✭ 96 (-77.36%)
Mutual labels:  graphql, validation
Postgraphile
GraphQL is a new way of communicating with your server. It eliminates the problems of over- and under-fetching, incorporates strong data types, has built-in introspection, documentation and deprecation capabilities, and is implemented in many programming languages. This all leads to gloriously low-latency user experiences, better developer experiences, and much increased productivity. Because of all this, GraphQL is typically used as a replacement for (or companion to) RESTful API services.
Stars: ✭ 10,967 (+2486.56%)
Mutual labels:  graphql, graphql-js
Graphql Log
Add logging to your GraphQL resolvers so you know what's going on in your app.
Stars: ✭ 94 (-77.83%)
Mutual labels:  graphql, graphql-js
Graphql2rest
GraphQL to REST converter: automatically generate a RESTful API from your existing GraphQL API
Stars: ✭ 181 (-57.31%)
Mutual labels:  graphql, graphql-js
Aws Mobile Appsync Sdk Js
JavaScript library files for Offline, Sync, Sigv4. includes support for React Native
Stars: ✭ 806 (+90.09%)
Mutual labels:  graphql, graphql-js
Graphql Scalars
A library of custom GraphQL Scalars for creating precise type-safe GraphQL schemas.
Stars: ✭ 1,023 (+141.27%)
Mutual labels:  graphql, graphql-js
Join Monster
A GraphQL to SQL query execution layer for query planning and batch data fetching.
Stars: ✭ 2,395 (+464.86%)
Mutual labels:  graphql, graphql-js
Graphql Core
A Python 3.6+ port of the GraphQL.js reference implementation of GraphQL.
Stars: ✭ 344 (-18.87%)
Mutual labels:  graphql, graphql-js

GraphQL Query Complexity Analysis for graphql-js

npm npm version CircleCI Twitter Follow

This library provides GraphQL query analysis to reject complex queries to your GraphQL server. This can be used to protect your GraphQL servers against resource exhaustion and DoS attacks.

Works with graphql-js reference implementation.

Installation

Install the package via npm

npm install -S graphql-query-complexity

Usage

Create the rule with a maximum query complexity:

import queryComplexity, {
  simpleEstimator
} from 'graphql-query-complexity';

const rule = queryComplexity({
  // The maximum allowed query complexity, queries above this threshold will be rejected
  maximumComplexity: 1000,
  
  // The query variables. This is needed because the variables are not available
  // in the visitor of the graphql-js library
  variables: {},

  // specify operation name only when pass multi-operation documents
  operationName?: string,
  
  // Optional callback function to retrieve the determined query complexity
  // Will be invoked whether the query is rejected or not
  // This can be used for logging or to implement rate limiting
  onComplete: (complexity: number) => {console.log('Determined query complexity: ', complexity)},
  
  // Optional function to create a custom error
  createError: (max: number, actual: number) => {
    return new GraphQLError(`Query is too complex: ${actual}. Maximum allowed complexity: ${max}`);
  },
  
  // Add any number of estimators. The estimators are invoked in order, the first
  // numeric value that is being returned by an estimator is used as the field complexity.
  // If no estimator returns a value, an exception is raised. 
  estimators: [
    // Add more estimators here...
    
    // This will assign each field a complexity of 1 if no other estimator
    // returned a value. 
    simpleEstimator({
      defaultComplexity: 1
    })
  ]
});

Configuration / Complexity Estimators

The complexity calculation of a GraphQL query can be customized with so called complexity estimators. A complexity estimator is a simple function that calculates the complexity for a field. You can add any number of complexity estimators to the rule, which are then executed one after another. The first estimator that returns a numeric complexity value determines the complexity for that field.

At least one estimator has to return a complexity value, otherwise an exception is raised. You can for example use the simpleEstimator as the last estimator in your chain to define a default value.

You can use any of the available estimators to calculate the complexity of a field or write your own:

  • simpleEstimator: The simple estimator returns a fixed complexity for each field. Can be used as last estimator in the chain for a default value.
  • directiveEstimator: Set the complexity via a directive in your schema definition (for example via GraphQL SDL)
  • fieldExtensionsEstimator: The field extensions estimator lets you set a numeric value or a custom estimator function in the field config extensions of your schema.
  • PRs welcome...

Consult the documentation of each estimator for information about how to use them.

Creating Custom Estimators

An estimator has the following function signature:

type ComplexityEstimatorArgs = {
  // The composite type (interface, object, union) that the evaluated field belongs to
  type: GraphQLCompositeType,
  
  // The GraphQLField that is being evaluated
  field: GraphQLField<any, any>,
  
  // The GraphQL node that is being evaluated
  node: FieldNode,
  
  // The input arguments of the field
  args: {[key: string]: any},
  
  // The complexity of all child selections for that field
  childComplexity: number
}

type ComplexityEstimator = (options: ComplexityEstimatorArgs) => number | void;

Usage with express-graphql

To use the query complexity analysis validation rule with express-graphql, use something like the following:

import queryComplexity, { simpleEstimator } from 'graphql-query-complexity';
import express from 'express';
import graphqlHTTP from 'express-graphql';
import schema from './schema';

const app = express();
app.use('/api', graphqlHTTP(async (request, response, {variables}) => ({
  schema,
  validationRules: [
    queryComplexity({
      estimators: [
        // Configure your estimators
        simpleEstimator({defaultComplexity: 1})
      ],
      maximumComplexity: 1000,
      variables,
      onComplete: (complexity: number) => {console.log('Query Complexity:', complexity);},
    })
  ]
})));

Calculate query complexity

If you want to calculate the complexity of a GraphQL query outside of the validation phase, for example to return the complexity value in a resolver, you can calculate the complexity via getComplexity:

import { getComplexity, simpleEstimator } from 'graphql-query-complexity';
import { parse } from 'graphql';

// Import your schema or get it form the info object in your resolver
import schema from './schema';

// You can also use gql template tag to get the parsed query
const query = parse(`
  query Q($count: Int) {
    some_value
    some_list(count: $count) {
      some_child_value
    }
  }
`);

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

console.log(complexity); // Output: 3

Prior Art

This project is inspired by the following prior projects:

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].