From dbea347e942179c46d306e90ec815a32d3ddacac Mon Sep 17 00:00:00 2001 From: zhumeisongsong Date: Fri, 8 Nov 2024 13:23:18 +0900 Subject: [PATCH 1/4] =?UTF-8?q?feat:=20=E2=9C=A8=20add=20subgraphsConfig?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- libs/application-config/src/lib/applications-config.ts | 4 ++++ libs/application-config/src/lib/subgraphs-config.ts | 10 ++++++++++ 2 files changed, 14 insertions(+) create mode 100644 libs/application-config/src/lib/subgraphs-config.ts diff --git a/libs/application-config/src/lib/applications-config.ts b/libs/application-config/src/lib/applications-config.ts index 4b6e66b..fce870a 100644 --- a/libs/application-config/src/lib/applications-config.ts +++ b/libs/application-config/src/lib/applications-config.ts @@ -1,6 +1,7 @@ interface ApplicationConfig { host: string; port: string; + name: string; } const DEFAULT_HOST = 'localhost'; @@ -15,15 +16,18 @@ const DEFAULT_PORT = { export const gatewayConfig: ApplicationConfig = { host: process.env['GATEWAY_HOST'] ?? DEFAULT_HOST, port: process.env['GATEWAY_PORT'] ?? DEFAULT_PORT.gateway, + name: 'gateway', }; // Graphql export const userSubGraph: ApplicationConfig = { host: process.env['USER_HOST'] ?? DEFAULT_HOST, port: process.env['USER_PORT'] ?? DEFAULT_PORT.user, + name: 'user', }; export const taskSubGraph: ApplicationConfig = { host: process.env['TASK_HOST'] ?? DEFAULT_HOST, port: process.env['TASK_PORT'] ?? DEFAULT_PORT.task, + name: 'task', }; diff --git a/libs/application-config/src/lib/subgraphs-config.ts b/libs/application-config/src/lib/subgraphs-config.ts new file mode 100644 index 0000000..4c59dd4 --- /dev/null +++ b/libs/application-config/src/lib/subgraphs-config.ts @@ -0,0 +1,10 @@ +import { ServiceEndpointDefinition } from '@apollo/gateway'; + +import { userSubGraph } from './applications-config'; + +export const subgraphsConfig: ServiceEndpointDefinition[] = [ + { + name: userSubGraph.name, + url: `${userSubGraph.host}:${userSubGraph.port}/graphql`, + }, +]; From 5a60da68677a3d178e124d1f002ec4ebf58cc85e Mon Sep 17 00:00:00 2001 From: zhumeisongsong Date: Fri, 8 Nov 2024 13:23:51 +0900 Subject: [PATCH 2/4] =?UTF-8?q?feat:=20=E2=9C=A8=20add=20ApolloGatewayDriv?= =?UTF-8?q?erConfig?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .vscode/settings.json | 3 ++- apps/gateway/src/app/app.module.ts | 15 ++++++++++++++- apps/gateway/src/main.ts | 2 +- apps/users-application/src/main.ts | 2 +- libs/application-config/src/index.ts | 1 + 5 files changed, 19 insertions(+), 4 deletions(-) diff --git a/.vscode/settings.json b/.vscode/settings.json index c1d4a56..7158154 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,5 +1,6 @@ { "cSpell.words": [ - "nestjs" + "nestjs", + "supergraph" ] } \ No newline at end of file diff --git a/apps/gateway/src/app/app.module.ts b/apps/gateway/src/app/app.module.ts index 6a9bc16..72200ac 100644 --- a/apps/gateway/src/app/app.module.ts +++ b/apps/gateway/src/app/app.module.ts @@ -1,10 +1,23 @@ +import { IntrospectAndCompose } from '@apollo/gateway'; +import { ApolloGatewayDriver, ApolloGatewayDriverConfig } from '@nestjs/apollo'; import { Module } from '@nestjs/common'; +import { GraphQLModule } from '@nestjs/graphql'; +import { subgraphsConfig } from '@graphql-federation-workspace/applications-config'; import { AppController } from './app.controller'; import { AppService } from './app.service'; @Module({ - imports: [], + imports: [ + GraphQLModule.forRoot({ + driver: ApolloGatewayDriver, + gateway: { + supergraphSdl: new IntrospectAndCompose({ + subgraphs: subgraphsConfig, + }), + }, + }), + ], controllers: [AppController], providers: [AppService], }) diff --git a/apps/gateway/src/main.ts b/apps/gateway/src/main.ts index b25b640..fcaa6c4 100644 --- a/apps/gateway/src/main.ts +++ b/apps/gateway/src/main.ts @@ -15,7 +15,7 @@ async function bootstrap() { await app.listen(port); Logger.log( - `🚀 Application is running on: http://${gatewayConfig.host}:${port}` + `🚀 Application is running on: ${gatewayConfig.host}:${port}` ); } diff --git a/apps/users-application/src/main.ts b/apps/users-application/src/main.ts index 73c159b..a74fbe1 100644 --- a/apps/users-application/src/main.ts +++ b/apps/users-application/src/main.ts @@ -15,7 +15,7 @@ async function bootstrap() { await app.listen(port); Logger.log( - `🚀 Application is running on: http://${userSubGraph.host}:${port}` + `🚀 Application is running on: ${userSubGraph.host}:${port}` ); } diff --git a/libs/application-config/src/index.ts b/libs/application-config/src/index.ts index f4ec158..1721aef 100644 --- a/libs/application-config/src/index.ts +++ b/libs/application-config/src/index.ts @@ -1 +1,2 @@ export * from './lib/applications-config'; +export * from './lib/subgraphs-config'; From 3c663c8132f72c7beee0d62eb934c065b6c0d212 Mon Sep 17 00:00:00 2001 From: zhumeisongsong Date: Fri, 8 Nov 2024 13:29:27 +0900 Subject: [PATCH 3/4] =?UTF-8?q?style:=20=F0=9F=92=84=20code=20format?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/gateway/src/main.ts | 4 +--- apps/users-application/src/main.ts | 4 +--- 2 files changed, 2 insertions(+), 6 deletions(-) diff --git a/apps/gateway/src/main.ts b/apps/gateway/src/main.ts index fcaa6c4..271affc 100644 --- a/apps/gateway/src/main.ts +++ b/apps/gateway/src/main.ts @@ -14,9 +14,7 @@ async function bootstrap() { const port = gatewayConfig.port; await app.listen(port); - Logger.log( - `🚀 Application is running on: ${gatewayConfig.host}:${port}` - ); + Logger.log(`🚀 Application is running on: ${gatewayConfig.host}:${port}`); } bootstrap(); diff --git a/apps/users-application/src/main.ts b/apps/users-application/src/main.ts index a74fbe1..720a641 100644 --- a/apps/users-application/src/main.ts +++ b/apps/users-application/src/main.ts @@ -14,9 +14,7 @@ async function bootstrap() { const port = userSubGraph.port; await app.listen(port); - Logger.log( - `🚀 Application is running on: ${userSubGraph.host}:${port}` - ); + Logger.log(`🚀 Application is running on: ${userSubGraph.host}:${port}`); } bootstrap(); From 7ceb7ea0e134142056b1a492017eab7a9c454e41 Mon Sep 17 00:00:00 2001 From: zhumeisongsong Date: Fri, 8 Nov 2024 13:37:15 +0900 Subject: [PATCH 4/4] =?UTF-8?q?test:=20=F0=9F=A7=AA=20pass=20error=20test?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- libs/application-config/src/lib/applications-config.spec.ts | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/libs/application-config/src/lib/applications-config.spec.ts b/libs/application-config/src/lib/applications-config.spec.ts index 72939b6..111b119 100644 --- a/libs/application-config/src/lib/applications-config.spec.ts +++ b/libs/application-config/src/lib/applications-config.spec.ts @@ -19,16 +19,19 @@ describe('Service Configurations', () => { expect(gatewayConfig).toEqual({ host: 'localhost', + name: 'gateway', port: '3333', }); expect(userSubGraph).toEqual({ host: 'localhost', + name: 'user', port: '15001', }); expect(taskSubGraph).toEqual({ host: 'localhost', + name: 'task', port: '15002', }); }); @@ -51,16 +54,19 @@ describe('Service Configurations', () => { expect(gatewayConfig).toEqual({ host: 'gateway.example.com', + name: 'gateway', port: '4000', }); expect(userSubGraph).toEqual({ host: 'user.example.com', + name: 'user', port: '5000', }); expect(taskSubGraph).toEqual({ host: 'task.example.com', + name: 'task', port: '6000', }); });