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

ESM support #6102

Merged
merged 23 commits into from
Aug 9, 2021
Merged

ESM support #6102

merged 23 commits into from
Aug 9, 2021

Conversation

PabloSzx
Copy link
Contributor

@PabloSzx PabloSzx commented Jun 8, 2021

Description

ESM Support

Type of change

  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)

How Has This Been Tested?

Test Environment:

  • OS: Windows & macOS
  • @graphql-codegen/...: latest
  • NodeJS: v14.17.0 & v16.3.0

Checklist:

  • I have followed the CONTRIBUTING doc and the style guidelines of this project
  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes
  • Any dependent changes have been merged and published in downstream modules

@changeset-bot
Copy link

changeset-bot bot commented Jun 8, 2021

🦋 Changeset detected

Latest commit: 07d3a5b

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 53 packages
Name Type
@graphql-codegen/typescript-resolvers Minor
@graphql-codegen/typed-document-node Minor
@graphql-cli/codegen Minor
@graphql-codegen/cli Minor
@graphql-codegen/core Minor
@graphql-codegen/c-sharp Minor
@graphql-codegen/c-sharp-operations Minor
@graphql-codegen/flow Minor
@graphql-codegen/flow-operations Minor
@graphql-codegen/flow-resolvers Minor
@graphql-codegen/java-apollo-android Minor
@graphql-codegen/java-common Minor
@graphql-codegen/java Minor
@graphql-codegen/kotlin Minor
@graphql-codegen/java-resolvers Minor
@graphql-codegen/add Minor
@graphql-codegen/fragment-matcher Minor
@graphql-codegen/introspection Minor
@graphql-codegen/jsdoc Minor
@graphql-codegen/schema-ast Minor
@graphql-codegen/time Minor
@graphql-codegen/urql-introspection Minor
@graphql-codegen/visitor-plugin-common Minor
@graphql-codegen/typescript-apollo-angular Minor
@graphql-codegen/typescript-apollo-client-helpers Minor
@graphql-codegen/typescript-document-nodes Minor
@graphql-codegen/typescript-enum-array Minor
@graphql-codegen/typescript-generic-sdk Minor
@graphql-codegen/gql-tag-operations Minor
@graphql-codegen/typescript-graphql-files-modules Minor
@graphql-codegen/typescript-graphql-request Minor
@graphql-codegen/typescript-mongodb Minor
@graphql-codegen/named-operations-object Minor
@graphql-codegen/typescript-oclif Minor
@graphql-codegen/typescript-operations Minor
@graphql-codegen/typescript-react-apollo Minor
@graphql-codegen/typescript-react-offix Minor
@graphql-codegen/typescript-react-query Minor
@graphql-codegen/typescript-rtk-query Minor
@graphql-codegen/typescript-stencil-apollo Minor
@graphql-codegen/typescript-type-graphql Minor
@graphql-codegen/typescript Minor
@graphql-codegen/typescript-urql Minor
@graphql-codegen/typescript-urql-graphcache Minor
@graphql-codegen/urql-svelte-operations-store Minor
@graphql-codegen/typescript-vue-apollo Minor
@graphql-codegen/typescript-vue-apollo-smart-ops Minor
@graphql-codegen/typescript-vue-urql Minor
@graphql-codegen/gql-tag-operations-preset Minor
@graphql-codegen/graphql-modules-preset Minor
@graphql-codegen/import-types-preset Minor
@graphql-codegen/near-operation-file-preset Minor
@graphql-codegen/plugin-helpers Minor

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@PabloSzx
Copy link
Contributor Author

PabloSzx commented Jun 8, 2021

I found an issue in CLI, and it's the usage of "require.resolve", which is not supported in ESM https://github.com/dotansimha/graphql-code-generator/blob/master/packages/graphql-codegen-cli/src/config.ts#L183

there isn't a good isomorphic solution to this issue, for more info check this: https://stackoverflow.com/questions/54977743/do-require-resolve-for-es-modules and https://nodejs.org/api/esm.html#esm_import_meta_resolve_specifier_parent

@PabloSzx
Copy link
Contributor Author

PabloSzx commented Jun 9, 2021

Another issue found in @graphql-codegen/plugin-helpers https://github.com/dotansimha/graphql-code-generator/blob/master/packages/utils/plugins-helpers/src/resolve-external-module-and-fn.ts#L7

That will most definitely break in ESM

@PabloSzx PabloSzx marked this pull request as draft June 9, 2021 20:18
@dotansimha
Copy link
Owner

I found an issue in CLI, and it's the usage of "require.resolve", which is not supported in ESM https://github.com/dotansimha/graphql-code-generator/blob/master/packages/graphql-codegen-cli/src/config.ts#L183

Yeah, seems to be an issue. And we do use this in codegen core to load plugins, configurations, and loaders. I guess we need a wrapper for that to make sure it's still supported in both setups.

Another issue found in @graphql-codegen/plugin-helpers https://github.com/dotansimha/graphql-code-generator/blob/master/packages/utils/plugins-helpers/src/resolve-external-module-and-fn.ts#L7

That will most definitely break in ESM

How do you think we should address this one?

cc @ardatan

@PabloSzx
Copy link
Contributor Author

PabloSzx commented Jun 20, 2021

How do you think we should address this one?

If that question is for me (maybe it wasn't), I feel like that is completely out of my scope since I'm not completely familiar with all the codegen internals, but adding support for esm I'd say is pretty much required for programmatic usage if for example GraphQL.js starts adding support for ESM with the exports field, that will trigger duplicated graphql module versions error, since if a commonjs module requires graphql, it will use the cjs version, and an esm module will use the esm version (this applies for every GraphQL library btw)

@dotansimha
Copy link
Owner

How do you think we should address this one?

If that question is for me (maybe it wasn't), I feel like that is completely out of my scope since I'm not completely familiar with all the codegen internals, but adding support for esm I'd say is pretty much required for programmatic usage if for example GraphQL.js starts adding support for ESM with the exports field, that will trigger duplicated graphql module versions error, since if a commonjs module requires graphql, it will use the cjs version, and an esm module will use the esm version (this applies for every GraphQL library btw)

Yeah, the question was for you :D
I see what you mean, so I think maybe we can wait a bit longer on this PR, and plan this for the next major version.

@PabloSzx
Copy link
Contributor Author

image

image

@dotansimha @ardatan

While working in a example using graphql-codegen programmatically I realized that the plugin-helpers .mjs file is bundling requires that come from bundling change-case-all, in ESM world bundling dependencies most of the time is forbidden, because it will bundle the requires in the esm files, which of course breaks.

Of course the solution is to add "change-case-all" as a dependency in this specific package, but this applies to every package that intends to support esm

@PabloSzx PabloSzx marked this pull request as ready for review June 25, 2021 01:23
@PabloSzx PabloSzx force-pushed the esm branch 2 times, most recently from 9b55a92 to 5b377b9 Compare June 28, 2021 19:43
@dotansimha dotansimha mentioned this pull request Jun 30, 2021
@PabloSzx PabloSzx force-pushed the esm branch 3 times, most recently from 9babdae to 87a16ec Compare August 2, 2021 04:55
@PabloSzx
Copy link
Contributor Author

PabloSzx commented Aug 2, 2021

After adding the test-esm.mjs script (and locally fixing the pending to be published fix of graphql-tools ardatan/graphql-tools#3289) these errors appear, this script is only able to catch errors in the imports, not the logic inside of the scripts

I4qPI3OXjD.mp4

I will take a look into fixing these issues

@PabloSzx PabloSzx force-pushed the esm branch 4 times, most recently from 2d0ab2e to 54b94dc Compare August 3, 2021 17:21
@PabloSzx
Copy link
Contributor Author

PabloSzx commented Aug 3, 2021

After ardatan/graphql-tools#3300 is merged and publish, this PR should be ready @dotansimha

@PabloSzx PabloSzx force-pushed the esm branch 2 times, most recently from 186c89c to 34d4c46 Compare August 4, 2021 02:23
@PabloSzx
Copy link
Contributor Author

PabloSzx commented Aug 4, 2021

image

createRequireFromPath is already removed in Node.js v16 (it was already deprecated in earlier versions), I will remove it and only use "createRequire", it should be fine since Node.js v10 support is already dropped

@@ -0,0 +1,52 @@
---
'@graphql-cli/codegen': major
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we really need a major release?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the changesets can be changed, I think this change is for minor, it was set as major since at first there was some changes that I thought were breaking, now everything should be fine

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ardatan done 👍

@PabloSzx PabloSzx force-pushed the esm branch 2 times, most recently from c0770d2 to 57ebac7 Compare August 6, 2021 06:23
'@graphql-codegen/import-types-preset': minor
'@graphql-codegen/near-operation-file-preset': minor
'@graphql-codegen/config-schema': minor
'@graphql-codegen/testing': minor
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't publish testing and website to the NPM so those would changeset fail at NPM release. Could you remove these two?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ardatan done 👍 this issue in changesets has been open since so much time... changesets/changesets#436

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

Successfully merging this pull request may close these issues.

5 participants