maticzav/graphql-shield

7.6.4 ESM build broken

Closed this issue · 5 comments

Bug report

  • [✔] I have checked other issues to make sure this is not a duplicate.

Describe the bug

Generated ESM index.js contains the following (which is identical to typings/index.d.ts):

export { IRule } from './types';
export { allow, and, chain, deny, inputRule, not, or, race, rule } from './constructors';
export { shield } from './shield';

with types.js containing:

export {};

Problems here are:

  1. Empty types.js module - no IRule export.
  2. Reference to './types', './constructors', and './shield' - all are missing .js extensions that are required due to relative import rules.
  3. IRule is TypeScript-specific and should not be emitted in the JavaScript file to begin with...

To Reproduce

  1. "type": "module" in package.json file

  2. Import { IRules } from "graphql-shield". (Or pick your favorite export)

  3. This is my GraphQL Schema.

Not required to reproduce the error

  1. This is the invoked query

Not required to reproduce the error.

  1. I use these permissions

Not required to reproduce the error.

  1. This is the error I see
node:internal/errors:478
    ErrorCaptureStackTrace(err);
    ^

Error [ERR_MODULE_NOT_FOUND]: Cannot find module '.../node_modules/graphql-shield/esm/constructors' imported from .../node_modules/graphql-shield/esm/index.js
    at new NodeError (node:internal/errors:387:5)
    at finalizeResolution (node:internal/modules/esm/resolve:330:11)
    at moduleResolve (node:internal/modules/esm/resolve:907:10)
    at defaultResolve (node:internal/modules/esm/resolve:1115:11)
    at nextResolve (node:internal/modules/esm/loader:163:28)
    at ESMLoader.resolve (node:internal/modules/esm/loader:837:30)
    at ESMLoader.getModuleJob (node:internal/modules/esm/loader:424:18)
    at ModuleWrap.<anonymous> (node:internal/modules/esm/module_job:76:40)
    at link (node:internal/modules/esm/module_job:75:36) {
  code: 'ERR_MODULE_NOT_FOUND'
}```

If I add `--es-module-specifier-resolution=node` to the command line, I get:

```bash
(node:7897) ExperimentalWarning: The Node.js specifier resolution flag is experimental. It could change or be removed at any time.
(Use `node --trace-warnings ...` to show where the warning was created)
file://.../node_modules/graphql-shield/esm/index.js:3
export { IRule } from './types';
         ^^^^^
SyntaxError: The requested module './types' does not provide an export named 'IRule'
    at ModuleJob._instantiate (node:internal/modules/esm/module_job:123:21)
    at async ModuleJob.run (node:internal/modules/esm/module_job:189:5)
    at async Promise.all (index 0)
    at async ESMLoader.import (node:internal/modules/esm/loader:526:24)
    at async loadESM (node:internal/process/esm_loader:91:5)
    at async handleMainPromise (node:internal/modules/run_main:65:12)

When I keep the node resolution flag but comment out the first line of esm/index.js and run the application, it starts up fine. Without the flag, Node complains about the next module without a .js extension...

Expected behavior

The build process should produce ESM compliant code.

Actual behaviour

See the description above.

Surprised this isn't a more prominent issue. Any known work around for this ?

We had the same issue and we solve it this way:

graphql-shield: 7.5.0 (fixed dependency, without the ^)
graphql-middleware: ^4.0.1 (another suggestion is the one below, fixed 6.0.9)

think it's connected with maticzav/graphql-middleware#433 and you can downgrade graphql-middleware to =6.0.9 to make it work.

having the same problem, the 7.5.0 works , graphql-middleware can be the latest, just make sure to do the following when applying the middleware:

const _schema = makeSchema(...
export const schema = applyMiddleware(_schema, permissions.generate(_schema))

I was having the same issue, downgrade it to v7.6.2 works for me
it's the same issue logged in here

the fix was released in 7.6.5
please feel free to reopen if the issue still exists