Allows you to use graphql-query-complexity with apollo server 3 as a plugin
npm install graphql-query-complexity-apollo-plugin
This is a plugin for Apollo Server 3 that throws if a query is too complex.
``ts
import { ApolloServer } from 'apollo-server-lambda'
import { schema } from './schema'
import { context } from './context'
import { SystemConfigOptions } from '../lib/SystemConfig'
import { fieldExtensionsEstimator, simpleEstimator } from 'graphql-query-complexity'
import { createComplexityPlugin } from './createComplexityPlugin'
return new ApolloServer({
schema,
context,
plugins: [
createComplexityPlugin({
schema,
estimators: [
fieldExtensionsEstimator(),
simpleEstimator({ defaultComplexity: 1 }),
],
maximumComplexity: 1000,
onComplete: (complexity) => {
console.log('Query Complexity:', complexity)
},
}),
],
})
`
`ts`
export const createComplexityPlugin: ({ schema, maximumComplexity, estimators, onComplete, createError }: {
schema: GraphQLSchema
maximumComplexity: number
estimators: Array
onComplete?: ((complexity: number) => Promise
createError?: ((max: number, actual: number) => Promise
}) => PluginDefinition
- createError` should return an error to be thrown if the actual complexity is more than the maximum complexity.