Skip to content

Commit

Permalink
Add info about ESM support in docs
Browse files Browse the repository at this point in the history
  • Loading branch information
MichalLytek committed Jul 19, 2023
1 parent 84dea54 commit 0f87872
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
- **Breaking Change**: exported `GraphQLISODateTime` scalar has now a name `DateTimeISO`
- **Breaking Change**: change `ValidatorFn` signature from `ValidatorFn<TArgs>` to `ValidatorFn<TContext>`
- support custom validation function getting resolver data on validate
- bring compatibility with the ESM ecosystem by exposing the double bundle of the `type-graphql` package (CJS and ESM versions)

### Fixes

Expand Down
46 changes: 46 additions & 0 deletions docs/esm.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
---
title: ECMAScript Modules
---

Since `v2.0.0` release, TypeGraphQL is compatible with ECMAScript modules.

Thanks to this, we can `import` the `type-graphql` package in the ESM projects without any hassle.

## TypeScript configuration

It's important to properly configure the project, so that it uses ESM correctly:

- the `module` options should be set to `ES2020/ES2022/ESNext`
- for the NodeJS apps, we should set `moduleResolution` to `"node16"`

All in all, the `tsconfig.json` file should looks like this:

```json title="tsconfig.json"
{
"compilerOptions": {
"target": "es2021",
"module": "es2020",
"moduleResolution": "node16",
"experimentalDecorators": true,
"emitDecoratorMetadata": true
}
}
```

## Package.json configuration

It is also important to set `type` option to `"module"` in your `package.json` file:

```json title="package.json"
{
"type": "module"
}
```

## Imports

Apart from using `import` syntax, your local imports have to use the `.js` suffix, e.g.:

```ts
import { MyResolver } from "./resolvers/MyResolver.js";
```
6 changes: 3 additions & 3 deletions docs/installation.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,11 @@ It's important to set these options in the `tsconfig.json` file of our project:
}
```

`TypeGraphQL` is designed to work with Node.js LTS and the latest stable releases. It uses features from ES2019 so we should set our `tsconfig.json` file appropriately:
`TypeGraphQL` is designed to work with Node.js LTS and the latest stable releases. It uses features from ES2021 so we should set our `tsconfig.json` file appropriately:

```js
{
"target": "es2019" // Or newer if Node.js version supports it
"target": "es2021" // Or newer if Node.js version supports it
}
```

Expand All @@ -52,7 +52,7 @@ All in all, the minimal `tsconfig.json` file example looks like this:
```json
{
"compilerOptions": {
"target": "es2019",
"target": "es2021",
"module": "commonjs",
"experimentalDecorators": true,
"emitDecoratorMetadata": true
Expand Down
3 changes: 2 additions & 1 deletion tsconfig.esm.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"module": "esnext",
"module": "ES2020",
"moduleResolution": "node16",
"outDir": "./build/esm"
}
}
3 changes: 3 additions & 0 deletions website/i18n/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@
"enums": {
"title": "Enums"
},
"esm": {
"title": "ECMAScript Modules"
},
"examples": {
"title": "Examples",
"sidebar_label": "List of examples"
Expand Down
3 changes: 2 additions & 1 deletion website/sidebars.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
"getting-started",
"types-and-fields",
"resolvers",
"bootstrap"
"bootstrap",
"esm"
],
"Advanced guides": [
"scalars",
Expand Down

0 comments on commit 0f87872

Please sign in to comment.