Skip to content

Commit eb5a824

Browse files
Merge pull request #1505 from maayanbar13/feat/graphql-model-shim-doc
docs(graphql): added docs for graphql model shim
2 parents b0e9b90 + 1ae26f6 commit eb5a824

File tree

4 files changed

+43
-0
lines changed

4 files changed

+43
-0
lines changed

content/graphql/sharing-models.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
### Sharing models
2+
3+
4+
5+
> warning **Warning** This chapter applies only to the code first approach.
6+
7+
One of the biggest advantages of using Typescript for the backend of your project is the ability to reuse the same models in a Typescript-based frontend application, by using a common Typescript package.
8+
9+
But there's a problem: the models created using the code first approach are heavily decorated with GraphQL related decorators. Those decorators are irrelevant in the frontend, negatively impacting performance.
10+
11+
#### Using the model shim
12+
13+
To solve this issue, NestJS provides a "shim" which allows you to replace the original decorators with inert code by using a `webpack` (or similar) configuration.
14+
To use this shim, configure an alias between the `@nestjs/graphql` package and the shim.
15+
16+
For example, for webpack this is resolved this way:
17+
18+
```typescript
19+
resolve: { // see: https://webpack.js.org/configuration/resolve/
20+
alias: {
21+
"@nestjs/graphql": path.resolve(__dirname, "../node_modules/@nestjs/graphql/dist/extra/graphql-model-shim")
22+
}
23+
}
24+
```
25+
26+
> info **Hint** The [TypeORM](/techniques/database) package has a similar shim that can be found [here](https://github.com/typeorm/typeorm/blob/master/extra/typeorm-model-shim.js).

src/app/homepage/menu/menu.component.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@ export class MenuComponent implements OnInit {
138138
{ title: 'Unions and Enums', path: '/graphql/unions-and-enums' },
139139
{ title: 'Field middleware', path: '/graphql/field-middleware' },
140140
{ title: 'Mapped types', path: '/graphql/mapped-types' },
141+
{ title: 'Sharing Models', path: '/graphql/sharing-models' },
141142
{ title: 'Plugins', path: '/graphql/plugins' },
142143
{ title: 'Complexity', path: '/graphql/complexity' },
143144
{ title: 'Extensions', path: '/graphql/extensions' },

src/app/homepage/pages/graphql/graphql.module.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import { QuickStartComponent } from './quick-start/quick-start.component';
1818
import { ResolversMapComponent } from './resolvers-map/resolvers-map.component';
1919
import { ScalarsComponent } from './scalars/scalars.component';
2020
import { SchemaGeneratorComponent } from './schema-generator/schema-generator.component';
21+
import { SharingModelsComponent } from "./sharing-models/sharing-models.component";
2122
import { SubscriptionsComponent } from './subscriptions/subscriptions.component';
2223
import { UnionsAndEnumsComponent } from './unions-and-enums/unions.component';
2324

@@ -117,6 +118,11 @@ const routes: Routes = [
117118
component: InterfacesComponent,
118119
data: { title: 'GraphQL + TypeScript - Interfaces' },
119120
},
121+
{
122+
path: 'sharing-models',
123+
component: SharingModelsComponent,
124+
data: { title: "GraphQL + TypeScript - Sharing models"}
125+
},
120126
{
121127
path: 'mapped-types',
122128
component: MappedTypesComponent,
@@ -148,6 +154,7 @@ const routes: Routes = [
148154
ScalarsComponent,
149155
SchemaGeneratorComponent,
150156
MappedTypesComponent,
157+
SharingModelsComponent,
151158
CliPluginComponent,
152159
FederationComponent,
153160
ComplexityComponent,
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { ChangeDetectionStrategy, Component } from '@angular/core';
2+
import { BasePageComponent } from '../../page/page.component';
3+
4+
@Component({
5+
selector: 'app-sharing-models',
6+
templateUrl: './sharing-models.component.html',
7+
changeDetection: ChangeDetectionStrategy.OnPush,
8+
})
9+
export class SharingModelsComponent extends BasePageComponent {}

0 commit comments

Comments
 (0)