Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error using custom loaders after migrating my project to ESM #6656

Open
3 of 4 tasks
eirikur-nc opened this issue Nov 7, 2024 · 3 comments
Open
3 of 4 tasks

Error using custom loaders after migrating my project to ESM #6656

eirikur-nc opened this issue Nov 7, 2024 · 3 comments

Comments

@eirikur-nc
Copy link

eirikur-nc commented Nov 7, 2024

Issue workflow progress

Progress of the issue based on the
Contributor Workflow

  • 1. The issue provides a reproduction available on Github, Stackblitz or CodeSandbox

    Make sure to fork this template and run yarn generate in the terminal.

    Please make sure the GraphQL Tools package versions under package.json matches yours.

  • 2. A failing test has been provided
  • 3. A local solution has been provided
  • 4. A pull request is pending review

Describe the bug

graphql-codegen fails after having added "type": "module" to all packages in a monorepo

To Reproduce:

https://codesandbox.io/p/devbox/q5w2zq

Expected behavior

Being able to author custom data loaders in ESM format

Environment:

  • OS: macOs 15.0.1
  • @graphql-tools/load: 8.0.0
  • NodeJS: 18.18.0

Additional context

I'm working in a monorepo that uses graphql-codegen to generate type information from our GraphQL schema (which has served us well, thanks!)
Due to issues with having a mixture of CommonJS and ESM packages in the repo, we decided to switch entirely over to ESM.
We added "type": "module" to all our package.json files.
We modified our scripts to run graphql-codegen-esm
We have a config file in yml format that specifies custom loaders for two schemas. Example

schema:
  - '${PWD}/src/graphql/types.graphql'
  - ${PWD}/../graphqlSchema.json:
        loader: '${ROOT_PATH}/configs/codegen/fileLoader.js'
  - http://127.0.0.1:5000/api/graphql:
      loader: '${ROOT_PATH}/configs/codegen/urlLoader.js'

Those failed to load properly. I converted them from CommonJS to ESM format and changed their file extensions to *.mjs but that did not suffice.

I found this SO question which got me on the right track
https://stackoverflow.com/questions/75253732/codegen-config-cant-load-custom-schema-loader

In packages/load/src/utils/custom-loader.ts there's a function named getCustomLoaderByPath which uses createRequire which cannot be used to load ES modules. I've managed to work around this issue by patching the package to use a dynamic import instead. The purpose of this issue is to get that change applied upstream to the benefit of others. I will follow up with a PR.

@ardatan
Copy link
Owner

ardatan commented Nov 7, 2024

Thanks for creating the issue.
Could you please create a reproduction on CodeSandbox so we can also debug and help you better with it?

@eirikur-nc
Copy link
Author

Thanks for creating the issue. Could you please create a reproduction on CodeSandbox so we can also debug and help you better with it?

Sure: https://codesandbox.io/p/devbox/q5w2zq
I've updated the description with a link

@eirikur-nc
Copy link
Author

A PR with a fix is ready for review #6660

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants