dotansimha / dotansimha/graphql-code-generator

error loading preset when transpiling TS->ESM

Open
#8,358 1 comment 0 reactions 1 assignee Claimed by @charlypoly View on GitHub
core
Dominant language
TypeScript
Stars
11.3k
Forks
1.4k
Avg merge
1d 1h
Merged PRs (30d)
23

Description

### Describe the bug

In relation to 32c1560f1669f6726008226c0e467a7fcc90aa84 where the `err.code` was `ERR_MODULE_NOT_FOUND` in esm instead of `MODULE_NOT_FOUND` - if transpiling via `ts-node` and esm `err.code` is `undefined`. I'm guessing it's because [`ts-node` generates it's own errors](https://github.com/TypeStrong/ts-node/blob/main/dist-raw/node-internal-errors.js#L12) that don't have the `err.code` set.

There's an [upstream issue in `ts-node`](https://github.com/TypeStrong/ts-node/issues/1862) about this, but a temporary fix is to add `err.code !== undefined` [here](https://github.com/dotansimha/graphql-code-generator/blob/master/packages/graphql-codegen-cli/src/plugins.ts#L25)

### Your Example Website or App

...

### Steps to Reproduce the Bug or Issue

1. create config using `@graphql-codegen/near-operation-file`
1. configure typescript to use ESM
1. load schema from `.ts` file via `ts-node` using `NODE_OPTIONS='--loader ts-node/esm' graphql-codegen-esm`

### Expected behavior

`@graphql-codegen/near-operation-file` to be loaded

### Screenshots or Videos

_No response_

### Platform

- OS: Linux
- NodeJS: 18.8.0
- `graphql` version: 16.6.0
- `@graphql-codegen/*` version(s):
```
"@graphql-codegen/add": "^3.2.1",
"@graphql-codegen/cli": "^2.12.0",
"@graphql-codegen/near-operation-file-preset": "^2.4.1",
"@graphql-codegen/typed-document-node": "^2.3.3",
"@graphql-codegen/typescript": "^2.7.3",
"@graphql-codegen/typescript-operations": "^2.5.3",
```

### Codegen Config File

```yaml
projects:
operations:
schema: ./src/graphql/schema/index.ts
documents:
- ./src/components/**/*.graphql
extensions:
codegen:
emitLegacyCommonJSImports: false
generates:
./src/components/:
preset: near-operation-file
plugins:
- add:
content:
- '// this file was auto generated by graphql-codegen'
- typescript-operations
- typed-document-node
```

### Additional context

- `tsconfig.json`:
```json
{
"compilerOptions": {
"strict": true,
"declaration": true,
"module": "NodeNext",
"moduleResolution": "NodeNext",
"target": "ESNext",
"rootDir": "src",
"outDir": "dist"
}
}
```

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.