dotansimha / dotansimha/graphql-code-generator

invalid esm import file url under windows (`ERR_UNSUPPORTED_ESM_URL_SCHEME`)

Open
#9,477 2 comments 0 reactions 0 assignees View on GitHub
Dominant language
TypeScript
Stars
11.3k
Forks
1.4k
Avg merge
1d 1h
Merged PRs (30d)
23

Description

### Which packages are impacted by your issue?

`@graphql-codegen/cli`, `@graphql-codegen/typescript-resolvers`, `@graphql-codegen/typescript`

### Describe the bug

We are using codegen to generate types for our graphql schema with @graphql-codegen/cli, @graphql-codegen/typescript and @graphql-codegen/typescript-resolvers
Our codegen config references a relative schema file
```typescript
const config: CodegenConfig = {
...
schema: "dist/api/schema.js",
...
};
```

Furthermore we use the `graphql-codegen-esm` binary to execute our code, since we are using ESM in our project.
Executing codegen works fine under Linux, but under Windows we get the message

```bash
[FAILED] Unable to load from file "C:/Users/xxxxxx/dev_win/xxxxx/dist/api/schema.js":
Error [ERR_UNSUPPORTED_ESM_URL_SCHEME]: Only URLs with a scheme in: file and data are supported by the default ESM loader.
On Windows, absolute paths must be valid file:// URLs. Received protocol 'c:'
```

I do not know how and where the relative path to that file is parsed from the config into an absolute file. But on windows this results in a path beginning with "C:/...". This is not compatible with ESM imports though, it needs to be `file:///c:/x/y/z instead of c:/x/y/z` (see nodejs/node#31710). When I manipulate the code that imports this file in `node_modules\@graphql-tools\code-file-loader\esm\load-from-module.js` to append `file:///` to the path, it actually works fine under windows.

Thank you!

### Steps to Reproduce the Bug or Issue

1. Create a project with esm and codegen
2. reference as schema a relative file in the configuration
3. run codegen (under windows)

### Expected behavior

I expect the code generation to run, but instead I receive an error.

### Platform

- OS: Windows
- NodeJS: v18.16.0
- `graphql` version: 16.6.0
- "@graphql-codegen/add": 5.0.0
- "@graphql-codegen/cli": 4.0.0
- "@graphql-codegen/typescript": 4.0.0
- "@graphql-codegen/typescript-resolvers": 4.0.0

### Codegen Config File
```typescript
import { CodegenConfig } from "@graphql-codegen/cli";

const config: CodegenConfig = {
overwrite: true,
schema: "dist/api/schema.js",
generates: {
"src/api/generated/types.ts": {
plugins: [
"typescript",
"typescript-resolvers",
{ add: { content: "// @ts-nocheck" } },
],
},
},
hooks: {
beforeDone: "npm run format",
},
};

export default config;
```

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.