import { GraphQLSchemaHost , Plugin } from '@nestjs/graphql' ; import { ApolloServerPlugin , GraphQLRequestListener } from 'apollo-server-plugin-base' ; import { GraphQLError } from 'graphql' ; import { fieldExtensionsEstimator , getComplexity , simpleEstimator } from 'graphql-query-complexity' ; @ Plugin ( ) export class ComplexityPlugin implements ApolloServerPlugin { constructor ( private gqlSchemaHost ) { } requestDidStart ( ) { const { schema } = this . gqlSchemaHost ; return { didResolveOperation ( { request , document } ) { const complexity = getComplexity ( { schema , operationName : request . operationName , query : document , variables : request . variables , estimators : [ fieldExtensionsEstimator ( ) , simpleEstimator ( { defaultComplexity : 1 } ) ] } ) ; if ( complexity >= 20 ) { throw new GraphQLError ( `Query is too complex: ${ complexity }. Maximum allowed complexity: 20` ) ; } console . log ( 'Query Complexity:' , complexity ) ; } } ; } }