Skip to content

Commit 2b94de6

Browse files
committed
docs(graphql): added docs for graphql model shim
1 parent 58e409b commit 2b94de6

File tree

4 files changed

+41
-0
lines changed

4 files changed

+41
-0
lines changed

content/graphql/sharing-models.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
### Sharing Models
2+
3+
> warning **Warning** This chapter applies only to the code first approach.
4+
5+
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.
6+
7+
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.
8+
9+
#### Using the model shim
10+
11+
To solve this issue, nest provides a "shim" which allows you to replace the original decorators with inert code by using a `webpack` (or similar) configuration.
12+
To use this shim, configure an alias between the `@nestjs/graphql` package and the shim.
13+
14+
For example, for webpack this is resolved this way:
15+
16+
```typescript
17+
resolve: { // see: https://webpack.js.org/configuration/resolve/
18+
alias: {
19+
"@nestjs/graphql": path.resolve(__dirname, "../node_modules/@nestjs/graphql/dist/extra/graphql-model-shim")
20+
}
21+
}
22+
```
23+
24+
> 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
@@ -115,6 +115,7 @@ export class MenuComponent implements OnInit {
115115
{ title: 'Unions', path: '/graphql/unions' },
116116
{ title: 'Enums', path: '/graphql/enums' },
117117
{ title: 'Mapped types', path: '/graphql/mapped-types' },
118+
{ title: 'Sharing Models', path: '/graphql/sharing-models' },
118119
{ title: 'Complexity', path: '/graphql/complexity' },
119120
{ title: 'Extensions', path: '/graphql/extensions' },
120121
{ title: 'CLI Plugin', path: '/graphql/cli-plugin' },

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

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

@@ -103,6 +104,11 @@ const routes: Routes = [
103104
component: InterfacesComponent,
104105
data: { title: 'GraphQL + TypeScript - Interfaces' },
105106
},
107+
{
108+
path: 'sharing-models',
109+
component: SharingModelsComponent,
110+
data: { title: "GraphQL + TypeScript - Sharing models"}
111+
},
106112
{
107113
path: 'mapped-types',
108114
component: MappedTypesComponent,
@@ -135,6 +141,7 @@ const routes: Routes = [
135141
ScalarsComponent,
136142
SchemaGeneratorComponent,
137143
MappedTypesComponent,
144+
SharingModelsComponent,
138145
CliPluginComponent,
139146
FederationComponent,
140147
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)