dotansimha / dotansimha/graphql-code-generator

Local ESM typescript plugin fails to load when using ts-node/esm

Open
#9,605 1 comment 1 reaction 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

### Describe the bug

When trying to use a local ESM typescript plugin, the plugin fails to load.
The issue happens because `ts-node/esm` reports error differently than node and causes an edge case on a condition during load.

There is actually two issues affecting the use of ESM typescript plugin:

## 1) Relative path resolving doesn't work in ESM mode

[This seems to be already known](https://github.com/dotansimha/graphql-code-generator/blob/master/packages/graphql-codegen-cli/src/codegen.ts#L38-L45).

As a workaround for now, the plugin's path needs to be absolute:
```ts
import path from 'path';

import { Types } from '@graphql-codegen/plugin-helpers';

const config: Types.Config = {
schema: 'http://localhost:3000/graphql',
emitLegacyCommonJSImports: false,
generates: {
'src/types/generated.ts': {
plugins: [
path.resolve(__dirname, './my-plugin.ts') // <----
],
config: {
...
},
},
},
};

export default config;
```

## 2) Module loading always crashes because of incompatible condition based on error's code

Inside the file `plugin.ts`, it crashes when the module cannot be loaded based on the error's `code`. However, `ts-node` doesn't report a code when crashing which makes this condition always `true` and it never gets to the point of trying to load the plugin with its full path.

The following quick patch fixes the issue: https://gist.github.com/leonchabbey/cbabda67f94d13a28697a7381927b18c

### Your Example Website or App

-

### Steps to Reproduce the Bug or Issue

-

### Expected behavior

To not have to apply a patch and to be able to use relative path.

For now, here is a full working setup:

```js
// package.json (force loader to be ts-node/esm)

{
"scripts": {
"codegen": "VERBOSE=1 NODE_OPTIONS=\"--loader ts-node/esm\" graphql-codegen-esm --config codegen.ts",
}

// codegen.ts (reference your plugin with an absolute path)

import path from 'path';
import { Types } from '@graphql-codegen/plugin-helpers';

const config: Types.Config = {
...,
generates: {
'src/types/generated.ts': {
plugins: [
path.resolve(__dirname, './my-plugin.ts')
],
...,
},
},
};

export default config;

// @graphql-codegen/cli (apply patch)
https://gist.github.com/leonchabbey/cbabda67f94d13a28697a7381927b18c

```

### Screenshots or Videos

_No response_

### Platform

- OS: macOS
- NodeJS: 16
- `graphql` version: 15
- `@graphql-codegen/*` version(s): 5.0.0

### Codegen Config File

_No response_

### Additional context

_No response_

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.