Skip to content

QueryComplexity does not work with ES Modules (ES2020, Typescript) #37

@tvvignesh

Description

@tvvignesh

Hi. I have my codebase in Typescript and had queryComplexity setup in my project some time ago. It worked well when my codebase was transpiled to commonjs and I was targetting ES2017

But recently I made the move to use ES2020 with Node.js modules (type:"module" in package.json`) and I get this error:

e1

This is how I am importing (as in the README):
import queryComplexity, { directiveEstimator, simpleEstimator } from 'graphql-query-complexity';

And my validationRules look like this (from the README):

validationRules: [
            queryComplexity({
              estimators: [
                directiveEstimator({
                  name: 'complexity'
                }),
                simpleEstimator({
                  defaultComplexity: 1
                })
              ],
              maximumComplexity: 500,
              variables: variables,
              onComplete: (complexity: number) => {
                console.log('Query Complexity:', complexity);
              },
              createError: (max: number, actual: number) => {
                return new GraphQLError(`Query is too complex: ${actual}. Maximum allowed complexity: ${max}`);
              }
            })
          ]

My old TSConfig (which worked):

{
  "compileOnSave": true,
  "compilerOptions": {
    "target": "ES2017",
    "module": "commonjs",
    "declaration": true,
    "experimentalDecorators": true,
    "emitDecoratorMetadata": true,
    "moduleResolution": "node",
    "esModuleInterop": true,
    "incremental": true,
    "tsBuildInfoFile": "./dist/tsbuildinfo",
    "alwaysStrict": true,
    "skipLibCheck": true,
    "noImplicitAny": false,
    "noUnusedLocals": true,
    "removeComments": true,
    "preserveConstEnums": true,
    "allowSyntheticDefaultImports": true,
    "outDir": "dist",
    "rootDir": "src",
    "declarationMap": true,
    "sourceMap": true,
    "strict": true,
    "noEmitOnError": true,
    "lib": ["ES2017"]
  },
  "include": ["./src/**/*"],
  "exclude": ["node_modules", "**/*.spec.ts", "**/*.d.ts"]
}

My new TSConfig which does not work:

{
	"compileOnSave": true,
	"compilerOptions": {
	  "target": "ES2020",
	  "module": "ESNext",
	  "declaration": true,
	  "experimentalDecorators": true,
	  "emitDecoratorMetadata": true,
	  "moduleResolution": "node",
	  "esModuleInterop": true,
	  "incremental": true,
	  "tsBuildInfoFile": "./dist/tsbuildinfo",
	  "alwaysStrict": true,
	  "skipLibCheck": true,
	  "noImplicitAny": false,
	  "noUnusedLocals": true,
	  "removeComments": true,
	  "preserveConstEnums": true,
	  "allowSyntheticDefaultImports": true,
	  "outDir": "dist",
	  "rootDir": "src",
	  "declarationMap": true,
	  "sourceMap": true,
	  "strict": true,
	  "noEmitOnError": true,
	  "lib": ["ES2020"]
	},
	"include": ["./src/**/*"],
	"exclude": ["node_modules", "**/*.spec.ts", "**/*.d.ts"]
  }

PS: All other libraries work except this since I have esModuleInterop set.

Any ideas what I might be doing wrong? Thanks.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions