Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
didResolveOperation({ request, document }: any) {
const complexity = getComplexity({
schema,
query: request.operationName
? separateOperations(document)[request.operationName]
: document,
variables: request.variables,
estimators: [fieldConfigEstimator(), simpleEstimator({ defaultComplexity: 1 })],
})
if (complexity >= config.server.maxQueryComplexity) {
throw new Error(
`Query is too complex: ${complexity}. Maximum allowed complexity: ${config.server.maxQueryComplexity}`
)
}
// This condition will prevent our console from being spammed by apollo playground introspection queries
if (request.operationName !== 'IntrospectionQuery') {
didResolveOperation({ request, document }) {
/**
* This provides GraphQL query analysis to be able to react on complex queries to your GraphQL server.
* This can be used to protect your GraphQL servers against resource exhaustion and DoS attacks.
* More documentation can be found at https://github.com/ivome/graphql-query-complexity.
*/
const complexity = getComplexity({
// Our built schema
schema,
// To calculate query complexity properly,
// we have to check if the document contains multiple operations
// and eventually extract it operation from the whole query document.
query: request.operationName
? separateOperations(document)[request.operationName]
: document,
// The variables for our GraphQL query
variables: request.variables,
// Add any number of estimators. The estimators are invoked in order, the first
// numeric value that is being returned by an estimator is used as the field complexity.
// If no estimator returns a value, an exception is raised.
estimators: [
// Using fieldExtensionsEstimator is mandatory to make it work with type-graphql.
fieldExtensionsEstimator(),
didResolveOperation({ request, document }) {
const complexity = getComplexity({
schema,
query: request.operationName
? separateOperations(document)[request.operationName]
: document,
variables: request.variables,
estimators: [
(args) => {
if (args.field.complexity !== undefined) {
if (isFunction(args.field.complexity)) {
return args.field.complexity(args);
} else {
return args.field.complexity;
}
}
},
simpleEstimator({ defaultComplexity: 1 }),