/
githubmirror
/
nest
Обзор
Документация
Войти
/
githubmirror
/
nest
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
sample/23-graphql-code-first/src/common/plugins/complexity.plugin.ts
39 строк
1 KB
Kamil Myśliwiec
chore(sample): update samples to use the latest apollo version
10 мар 2023, 16:12
Не верифицирован
10 мар 2023, 16:12
ef04478
Код
Авторство
О чём код?
import { ApolloServerPlugin, GraphQLRequestListener } from '@apollo/server'; import { Plugin } from '@nestjs/apollo'; import { GraphQLSchemaHost } from '@nestjs/graphql'; import { GraphQLError } from 'graphql'; import { fieldExtensionsEstimator, getComplexity, simpleEstimator, } from 'graphql-query-complexity'; @Plugin() export class ComplexityPlugin implements ApolloServerPlugin { constructor(private gqlSchemaHost: GraphQLSchemaHost) {} async requestDidStart(): Promise<GraphQLRequestListener<any>> { const { schema } = this.gqlSchemaHost; return { async 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); }, }; } }