How to use the graphql-query-complexity.getComplexity function in graphql-query-complexity

To help you get started, we’ve selected a few graphql-query-complexity examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github developer239 / node-type-orm-graphql / src / plugins / complexityValidator.ts View on Github external
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') {
github MichalLytek / type-graphql / examples / query-complexity / index.ts View on Github external
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(),
github jorisvddonk / WelcomeToTheFuture / backend / index.ts View on Github external
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 }),

graphql-query-complexity

Validation rule for GraphQL query complexity analysis

MIT
Latest version published 5 months ago

Package Health Score

70 / 100
Full package analysis