apollo-server-plugin-query-complexity

Apollo server plugin that limits query complexity.

Usage no npm install needed!

<script type="module">
  import apolloServerPluginQueryComplexity from 'https://cdn.skypack.dev/apollo-server-plugin-query-complexity';
</script>

README

apollo-server-plugin-query-complexity

Apollo Server plugin that limits query complexity.

Apollo Server does not provide request variables to validation rules such as graphql-query-complexity. This plugin serves the same purpose but is compatible with operation variables.

Installation

yarn add apollo-server-plugin-query-complexity graphql-query-complexity graphql

Usage

import ApolloServer from 'apollo-server';
import ApolloServerPluginQueryComplexity from 'apollo-server-plugin-query-complexity';
import { directiveEstimator, simpleEstimator } from 'graphql-query-complexity';

const server = new ApolloServer({
  typeDefs: gql`
    directive @complexity(
      value: Int!
      multipliers: [String!]
    ) on FIELD_DEFINITION

    type Query {
      a: String! # Complexity of 1
      b(n: Int!): String! @complexity(value: 1, multipliers: ["n"]) # Complexity of variable "n"
    }
  `,
  resolvers: {},
  plugins: [
    ApolloServerPluginQueryComplexity({
      estimators: [directiveEstimator(), simpleEstimator()],
      maximumComplexity: 1,
    }),
  ],
});

Credit

TypeGraphQL Example