Skip to content

Commit 3fc0038

Browse files
committed
feat(nestjs): update to NestJS v9/10 and Federation v2
1 parent d28a95c commit 3fc0038

File tree

109 files changed

+4698
-10814
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

109 files changed

+4698
-10814
lines changed

.prettierrc

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
{
22
"arrowParens": "avoid",
3-
"trailingComma": "all"
3+
"trailingComma": "all",
4+
"bracketSameLine": false,
5+
"bracketSpacing": true,
6+
"semi": true
47
}

README.md

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ import { authChecker } from "./auth";
4343
@Module({
4444
imports: [
4545
TypeGraphQLModule.forRoot({
46+
driver: ApolloDriver,
4647
emitSchemaFile: true,
4748
validate: false,
4849
authChecker,
@@ -112,6 +113,7 @@ Example of using the config service to generate `TypeGraphQLModule` options:
112113
TypeGraphQLModule.forRootAsync({
113114
inject: [ConfigService],
114115
useFactory: async (config: ConfigService) => ({
116+
driver: ApolloDriver,
115117
cors: true,
116118
debug: config.isDevelopmentMode,
117119
playground: !config.isDevelopmentMode,
@@ -126,13 +128,13 @@ Example of using the config service to generate `TypeGraphQLModule` options:
126128
export default class AppModule {}
127129
```
128130

129-
### `TypeGraphQLFederationModule`
131+
### Apollo Federation
130132

131133
`typegraphql-nestjs` has also support for [Apollo Federation](https://www.apollographql.com/docs/federation/).
132134

133-
However, Apollo Federation requires building a federated GraphQL schema, hence you need to use the `TypeGraphQLFederationModule` module, designed specially for that case.
135+
However, Apollo Federation requires building a federated GraphQL schema, hence you need to adjust your code a bit.
134136

135-
The usage is really similar to the basic `TypeGraphQLModule` - the only different is that `.forFeature()` method has an option to provide `referenceResolvers` object which is needed in some cases of Apollo Federation:
137+
The usage is really similar to the basic case - the only difference is that in `TypeGraphQLModule.forFeature()` method you can provide a `referenceResolvers` option object, which is needed in some cases of Apollo Federation:
136138

137139
```ts
138140
function resolveUserReference(
@@ -143,7 +145,7 @@ function resolveUserReference(
143145

144146
@Module({
145147
imports: [
146-
TypeGraphQLFederationModule.forFeature({
148+
TypeGraphQLModule.forFeature({
147149
orphanedTypes: [User],
148150
referenceResolvers: {
149151
User: {
@@ -157,25 +159,22 @@ function resolveUserReference(
157159
export default class AccountModule {}
158160
```
159161

160-
The `.forRoot()` method has no differences but you should provide the `skipCheck: true` option as federated schema can violate the standard GraphQL schema rules like at least one query defined:
162+
For the `.forRoot()` method there's no differences - just need to provide `driver: ApolloFederationDriver` option in order to build a subgraph schema, same as with `GraphQLModule` from `@nestjs/graphql` described in the [NestJS docs](https://docs.nestjs.com/graphql/federation). However, you also need to explicitly setup federation version, by using `federationVersion` option:
161163

162164
```ts
163165
@Module({
164166
imports: [
165-
TypeGraphQLFederationModule.forRoot({
166-
validate: false,
167-
skipCheck: true,
167+
TypeGraphQLModule.forRoot({
168+
driver: ApolloFederationDriver,
169+
federationVersion: 2,
168170
}),
169171
AccountModule,
170172
],
171173
})
172174
export default class AppModule {}
173175
```
174176

175-
> Be aware that you cannot mix `TypeGraphQLFederationModule.forRoot()` with the base `TypeGraphQLModule.forFeature()` one.
176-
> You need to consistently use only `TypeGraphQLFederationModule` across all modules.
177-
178-
Then, for exposing the federated schema using Apollo Gateway, you should use the standard NestJS [GraphQLGatewayModule](https://docs.nestjs.com/graphql/federation#federated-example-gateway).
177+
Then, for exposing the federated schema using Apollo Gateway, you should use the standard [NestJS `ApolloGatewayDriver` solution](https://docs.nestjs.com/graphql/federation#federated-example-gateway).
179178

180179
## Caveats
181180

@@ -201,13 +200,13 @@ You can see some examples of the integration in this repo:
201200

202201
Usage of request scoped dependencies - retrieving fresh instances of resolver and service classes on every request (query/mutation)
203202

204-
1. [Apollo Federation](https://github.com/MichalLytek/typegraphql-nestjs/tree/master/examples/4-federation)
203+
1. [Middlewares](https://github.com/MichalLytek/typegraphql-nestjs/tree/master/examples/4-middlewares)
205204

206-
Showcase of Apollo Federation approach, using the `TypeGraphQLFederationModule` and `GraphQLGatewayModule`.
205+
Usage of class-based middlewares - modules, providers and schema options
207206

208-
1. [Middlewares](https://github.com/MichalLytek/typegraphql-nestjs/tree/master/examples/5-middlewares)
207+
1. [Apollo Federation](https://github.com/MichalLytek/typegraphql-nestjs/tree/master/examples/5-federation)
209208

210-
Usage of class-based middlewares - modules, providers and schema options
209+
Showcase of Apollo Federation approach
211210

212211
You can run them by using `ts-node`, like `npx ts-node ./examples/1-basics/index.ts`.
213212

examples/1-basics/app.module.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
1+
import { ApolloDriver, ApolloDriverConfig } from "@nestjs/apollo";
12
import { Module } from "@nestjs/common";
23
import { TypeGraphQLModule } from "../../src";
34

45
import RecipeModule from "./recipe/module";
56

67
@Module({
78
imports: [
9+
// TypeGraphQLModule.forRoot<ApolloDriverConfig>({
810
TypeGraphQLModule.forRoot({
11+
driver: ApolloDriver,
912
emitSchemaFile: true,
1013
validate: false,
1114
}),
1215
RecipeModule,
1316
],
1417
})
15-
export default class AppModule {}
18+
export default class AppModule { }

examples/1-basics/index.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
import "reflect-metadata";
22
import { NestFactory } from "@nestjs/core";
33
import {
4-
NestFastifyApplication,
5-
FastifyAdapter,
6-
} from "@nestjs/platform-fastify";
4+
NestExpressApplication,
5+
ExpressAdapter,
6+
} from "@nestjs/platform-express";
77

88
import AppModule from "./app.module";
99

1010
async function bootstrap() {
11-
const app = await NestFactory.create<NestFastifyApplication>(
11+
const app = await NestFactory.create<NestExpressApplication>(
1212
AppModule,
13-
new FastifyAdapter(),
13+
new ExpressAdapter(),
1414
);
1515

1616
await app.listen(3000);

examples/1-basics/schema.gql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,4 @@ type Recipe {
1919
input RecipeInput {
2020
description: String
2121
title: String!
22-
}
22+
}

examples/2-multiple-servers/first-schema.graphql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,4 @@ type SuperRecipe {
2525
description: String
2626
isSuperRecipe: Boolean!
2727
title: String!
28-
}
28+
}

examples/2-multiple-servers/first.module.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { ApolloDriver } from "@nestjs/apollo";
12
import { Module } from "@nestjs/common";
23
import path from "path";
34
import { TypeGraphQLModule } from "../../src";
@@ -7,10 +8,11 @@ import RecipeModule from "./recipe/module";
78
@Module({
89
imports: [
910
TypeGraphQLModule.forRoot({
11+
driver: ApolloDriver,
1012
emitSchemaFile: path.resolve(__dirname, "first-schema.graphql"),
1113
validate: false,
1214
}),
1315
RecipeModule,
1416
],
1517
})
16-
export default class FirstAppModule {}
18+
export default class FirstAppModule { }

examples/2-multiple-servers/index.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
11
import "reflect-metadata";
22
import { NestFactory } from "@nestjs/core";
33
import {
4-
NestFastifyApplication,
5-
FastifyAdapter,
6-
} from "@nestjs/platform-fastify";
4+
NestExpressApplication,
5+
ExpressAdapter,
6+
} from "@nestjs/platform-express";
77

88
import FirstAppModule from "./first.module";
99
import SecondAppModule from "./second.module";
1010

1111
async function bootstrap() {
12-
const firstApp = await NestFactory.create<NestFastifyApplication>(
12+
const firstApp = await NestFactory.create<NestExpressApplication>(
1313
FirstAppModule,
14-
new FastifyAdapter(),
14+
new ExpressAdapter(),
1515
);
16-
const secondApp = await NestFactory.create<NestFastifyApplication>(
16+
const secondApp = await NestFactory.create<NestExpressApplication>(
1717
SecondAppModule,
18-
new FastifyAdapter(),
18+
new ExpressAdapter(),
1919
);
2020

2121
await firstApp.listen(3001);

examples/2-multiple-servers/second-schema.graphql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,4 @@ type SuperAnimal {
2525
isSuperHero: Boolean!
2626
name: String!
2727
weight: Int!
28-
}
28+
}

examples/2-multiple-servers/second.module.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { ApolloDriver } from "@nestjs/apollo";
12
import { Module } from "@nestjs/common";
23
import path from "path";
34
import { TypeGraphQLModule } from "../../src";
@@ -7,10 +8,11 @@ import AnimalModule from "./animal/module";
78
@Module({
89
imports: [
910
TypeGraphQLModule.forRoot({
11+
driver: ApolloDriver,
1012
emitSchemaFile: path.resolve(__dirname, "second-schema.graphql"),
1113
validate: false,
1214
}),
1315
AnimalModule,
1416
],
1517
})
16-
export default class SecondAppModule {}
18+
export default class SecondAppModule { }

0 commit comments

Comments
 (0)