Skip to content

Commit

Permalink
docs(browser): update instructions for browser shim usage
Browse files Browse the repository at this point in the history
  • Loading branch information
MichalLytek committed Feb 21, 2019
1 parent 18eab5d commit 5f8fc2d
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 11 deletions.
13 changes: 7 additions & 6 deletions docs/browser-usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,18 @@ title: Browser usage
---

## Using classes in client app

Sometimes you might want to use the classes, that you've created and annotated with TypeGraphQL decorators, in your client app that works in browser. For example, you may want to reuse the args or input classes with `class-validator` decorators or the object type classes with some helpful custom methods.

As TypeGraphQL is a Node.js framework, it doesn't work in browser environment, so you may quickly got an error, e.g. `ERROR in ./node_modules/fs.realpath/index.js`, while trying to build your app with Webpack. To fix that, you have to configure Webpack to use the decorators shim instead of normal module. All you need is to add this plugin code to your webpack config:

```js
plugins: [
..., // here any other existing plugins that you already have
new webpack.NormalModuleReplacementPlugin(/type-graphql$/, function (result) {
result.request = result.request.replace(/type-graphql/, "type-graphql/browser-shim");
}),
]
// ...here are any other existing plugins that you already have
new webpack.NormalModuleReplacementPlugin(/type-graphql$/, resource => {
resource.request = resource.request.replace(/type-graphql/, "type-graphql/dist/browser-shim");
}),
];
```

Also, thanks to this your bundle will be much lighter as you don't embedded the whole TypeGraphQL library code in your app.

10 changes: 5 additions & 5 deletions src/browser-shim.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
/*
This "shim" can be used on the frontend to prevent from errors on undefined decorators,
when you are sharing same classes across backend and frontend.
To use this shim, simply configure your Webpack configuration to use this file
instead of normal TypeGraphQL module.
To use this shim, simply set up your Webpack configuration
to use this file instead of a normal TypeGraphQL module.
plugins: [
..., // any existing plugins that you already have
new webpack.NormalModuleReplacementPlugin(/type-graphql$/, function (result) {
result.request = result.request.replace(/type-graphql/, "type-graphql/browser-shim");
// ...here are any other existing plugins that you already have
new webpack.NormalModuleReplacementPlugin(/type-graphql$/, resource => {
resource.request = resource.request.replace(/type-graphql/, "type-graphql/dist/browser-shim");
}),
]
*/
Expand Down

0 comments on commit 5f8fc2d

Please sign in to comment.