graphql-query-complexity-apollo-plugin
v1.0.2
Published
Allows you to use graphql-query-complexity with apollo server 3 as a plugin
Downloads
258
Maintainers
Readme
Graphql Query Complexity Apollo Plugin
This is a plugin for Apollo Server 3 that throws if a query is too complex.
Example
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)
},
}),
],
})
API
export const createComplexityPlugin: ({ schema, maximumComplexity, estimators, onComplete, createError }: {
schema: GraphQLSchema
maximumComplexity: number
estimators: Array<ComplexityEstimator>
onComplete?: ((complexity: number) => Promise<void> | void)
createError?: ((max: number, actual: number) => Promise<GraphQLError> | GraphQLError)
}) => PluginDefinition
createError
should return an error to be thrown if the actual complexity is more than the maximum complexity.