Issue workflow progress
Progress of the issue based on the
Contributor Workflow
Describe the bug
With esModule: true option, imported fragments are sometimes tree-shaken incorrectly. E.g. in my Webpack bundle I have
instead of expected
45049:function(e,n){"use strict";n.A={kind:"Document",definitions:[{kind:"FragmentDefinition",name:"MenuSectionFragment"},...]}}
I get with the fix below. This seems to happen because require(${importFile})
|
const parseDocument = `require(${importFile})`; |
doesn't return the document exported by
|
return `export default ${identifier}`; |
.
The symptoms are:
TypeError: The following error originated from your test code, not from Cypress.
> Cannot read properties of undefined (reading 'filter')
When Cypress detects uncaught errors originating from your test code it will automatically fail the current test.
Cypress could not associate this error to any specific test.
We dynamically generated a new test to display this failure.
at unique (webpack://myApp/./app/javascript/libs/gql/queries/myQuery.gql:9:0)
at ./app/javascript/libs/gql/queries/myQuery.gql (webpack://myApp/./app/javascript/libs/gql/queries/myQuery.gql:21:0)
at __webpack_require__ (http://localhost:8080/__cypress/src/main.js:2705:42)
at ./app/javascript/libs/gql/queries/myQuery.generated.tsx(http://localhost:8080/__cypress/src/spec-0.js:4333:79)
at __webpack_require__ (http://localhost:8080/__cypress/src/main.js:2705:42)
at ./app/javascript/myApp/useMenuItem.ts (http://localhost:8080/__cypress/src/spec-0.js:6226:115)
at __webpack_require__ (http://localhost:8080/__cypress/src/main.js:2705:42)
at ./app/javascript/myApp/index.ts (http://localhost:8080/__cypress/src/spec-0.js:6189:70)
at __webpack_require__ (http://localhost:8080/__cypress/src/main.js:2705:42)
at ./app/javascript/myApp/index.ts (http://localhost:8080/__cypress/src/spec-0.js:5800:70)
- If this is fixed by patching
@graphql-tools/webpack-loader-runtime to replace defs.filter by (defs || []).filter, I see
No fragment named MenuSectionFragment
instead.
To Reproduce Steps to reproduce the behavior:
Use @graphql-tools/webpack-loader with esModule: true and import a fragment from a different file. It may not always happen; I'll try to add a codesandbox and update the issue.
Expected behavior
Using esModule: true should not break the app.
Expected fix
Update
|
const parseDocument = `require(${importFile})`; |
to
const parseDocument = options.esModule ? `require(${importFile}).default` : `require(${importFile})`;
(or use an import statement instead but that would be more complicated).
Also update
|
return defs.filter(function (def) { |
and
|
return defs.filter(def => { |
to use
(defs || []).filter to correctly handle the case where an imported file is not actually used.
Environment:
- OS: Ubuntu
@graphql-tools/webpack-loader: 7.0.0
@graphql-tools/webpack-loader-runtime: 7.0.0
- NodeJS: 20.16.0
Additional context
Issue workflow progress
Progress of the issue based on the
Contributor Workflow
Describe the bug
With
esModule: trueoption, imported fragments are sometimes tree-shaken incorrectly. E.g. in my Webpack bundle I haveinstead of expected
I get with the fix below. This seems to happen because
require(${importFile})graphql-tools/packages/webpack-loader/src/index.ts
Line 40 in 9eccdf9
graphql-tools/packages/webpack-loader/src/index.ts
Line 101 in 9eccdf9
The symptoms are:
@graphql-tools/webpack-loader-runtimeto replacedefs.filterby(defs || []).filter, I seeinstead.
To Reproduce Steps to reproduce the behavior:
Use
@graphql-tools/webpack-loaderwithesModule: trueand import a fragment from a different file. It may not always happen; I'll try to add a codesandbox and update the issue.Expected behavior
Using
esModule: trueshould not break the app.Expected fix
Update
graphql-tools/packages/webpack-loader/src/index.ts
Line 40 in 9eccdf9
(or use an
importstatement instead but that would be more complicated).Also update
graphql-tools/packages/webpack-loader-runtime/src/index.ts
Line 6 in 9eccdf9
graphql-tools/packages/webpack-loader-runtime/src/index.ts
Line 22 in 9eccdf9
(defs || []).filterto correctly handle the case where an imported file is not actually used.Environment:
@graphql-tools/webpack-loader: 7.0.0@graphql-tools/webpack-loader-runtime: 7.0.0Additional context