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

Generated queries in TypeScript do not compile with ESM #740

Open
4 tasks done
dpilch opened this issue Oct 12, 2023 · 2 comments
Open
4 tasks done

Generated queries in TypeScript do not compile with ESM #740

dpilch opened this issue Oct 12, 2023 · 2 comments
Labels
bug Something isn't working

Comments

@dpilch
Copy link
Contributor

dpilch commented Oct 12, 2023

Before opening, please confirm:

  • I have installed the latest version of the Amplify CLI (see above), and confirmed that the issue still persists.
  • I have searched for duplicate or closed issues.
  • I have read the guide for submitting bug reports.
  • I have done my best to include a minimal, self-contained set of instructions for consistently reproducing the issue.

How did you install the Amplify CLI?

npm

If applicable, what version of Node.js are you using?

v18.8.0

Amplify CLI Version

12.6.0

What operating system are you using?

Mac

Amplify Codegen Command

codegen statements

Describe the bug

When generating GraphQL queries with TypeScript the types import does not use correct module resolution for ESM.

src/graphql/mutations.ts:5:27 - error TS2835: Relative import paths need explicit file extensions in EcmaScript imports when '--moduleResolution' is 'node16' or 'nodenext'. Did you mean '../API.js'?

5 import * as APITypes from "../API";
                            ~~~~~~~~

src/graphql/queries.ts:5:27 - error TS2835: Relative import paths need explicit file extensions in EcmaScript imports when '--moduleResolution' is 'node16' or 'nodenext'. Did you mean '../API.js'?

5 import * as APITypes from "../API";
                            ~~~~~~~~

src/graphql/subscriptions.ts:5:27 - error TS2835: Relative import paths need explicit file extensions in EcmaScript imports when '--moduleResolution' is 'node16' or 'nodenext'. Did you mean '../API.js'?

5 import * as APITypes from "../API";
                            ~~~~~~~~

See for additional details: https://github.com/dpilch/codegen-esm-error

Expected behavior

The imports should have .js when using ESM.

Reproduction steps

See sample app here: https://github.com/dpilch/codegen-esm-error

GraphQL schema(s)

Any GraphQL schema will have this issue.

Log output

# Put your logs below this line
src/graphql/mutations.ts:5:27 - error TS2835: Relative import paths need explicit file extensions in EcmaScript imports when '--moduleResolution' is 'node16' or 'nodenext'. Did you mean '../API.js'?

5 import * as APITypes from "../API";
                            ~~~~~~~~

src/graphql/queries.ts:5:27 - error TS2835: Relative import paths need explicit file extensions in EcmaScript imports when '--moduleResolution' is 'node16' or 'nodenext'. Did you mean '../API.js'?

5 import * as APITypes from "../API";
                            ~~~~~~~~

src/graphql/subscriptions.ts:5:27 - error TS2835: Relative import paths need explicit file extensions in EcmaScript imports when '--moduleResolution' is 'node16' or 'nodenext'. Did you mean '../API.js'?

5 import * as APITypes from "../API";
                            ~~~~~~~~

Additional information

We will likely need to add a new codegen configuration for this option (such as moduleResolution).

@ErlendHer
Copy link

ErlendHer commented Nov 9, 2023

@dpilch Any update on this? It's somewhat frustrating having to manually add the .js extension to the imports in the generated code

Edit: For anybody having the same issue, and if you're using a Makefile, the below Makefile fixes it (run make update-imports)

GRAPHQL_SRC_DIR := packages/graphql/src

update-imports:
	@sed -i '' 's|import \* as APITypes from "./API";|import * as APITypes from "./API.js";|g' $(GRAPHQL_SRC_DIR)/mutations.ts
	@sed -i '' 's|import \* as APITypes from "./API";|import * as APITypes from "./API.js";|g' $(GRAPHQL_SRC_DIR)/queries.ts
	@sed -i '' 's|import \* as APITypes from "./API";|import * as APITypes from "./API.js";|g' $(GRAPHQL_SRC_DIR)/subscriptions.ts

graphql: amplify-codegen update-imports
graphql-force: amplify-codegen-force update-imports

amplify-codegen:
	amplify api gql-compile 
	amplify codegen --maxDepth 6
	amplify codegen models --maxDepth 6

amplify-codegen-force:
	amplify api gql-compile --allow-destructive-graphql-schema-updates
	amplify codegen --maxDepth 6
	amplify codegen models --maxDepth 6

@dpilch
Copy link
Contributor Author

dpilch commented Nov 15, 2023

Sorry, no progress on this so far.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants