Skip to content

Commit

Permalink
Merge pull request #18 from zhumeisongsong/feature/gateway-federation…
Browse files Browse the repository at this point in the history
…-logic

✨ Federated gateway
  • Loading branch information
zhumeisongsong authored Nov 8, 2024
2 parents 1af6fd8 + 7ceb7ea commit e3a6194
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 8 deletions.
3 changes: 2 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"cSpell.words": [
"nestjs"
"nestjs",
"supergraph"
]
}
15 changes: 14 additions & 1 deletion apps/gateway/src/app/app.module.ts
Original file line number Diff line number Diff line change
@@ -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<ApolloGatewayDriverConfig>({
driver: ApolloGatewayDriver,
gateway: {
supergraphSdl: new IntrospectAndCompose({
subgraphs: subgraphsConfig,
}),
},
}),
],
controllers: [AppController],
providers: [AppService],
})
Expand Down
4 changes: 1 addition & 3 deletions apps/gateway/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@ async function bootstrap() {
const port = gatewayConfig.port;

await app.listen(port);
Logger.log(
`🚀 Application is running on: http://${gatewayConfig.host}:${port}`
);
Logger.log(`🚀 Application is running on: ${gatewayConfig.host}:${port}`);
}

bootstrap();
4 changes: 1 addition & 3 deletions apps/users-application/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@ async function bootstrap() {
const port = userSubGraph.port;

await app.listen(port);
Logger.log(
`🚀 Application is running on: http://${userSubGraph.host}:${port}`
);
Logger.log(`🚀 Application is running on: ${userSubGraph.host}:${port}`);
}

bootstrap();
1 change: 1 addition & 0 deletions libs/application-config/src/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from './lib/applications-config';
export * from './lib/subgraphs-config';
6 changes: 6 additions & 0 deletions libs/application-config/src/lib/applications-config.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
});
});
Expand All @@ -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',
});
});
Expand Down
4 changes: 4 additions & 0 deletions libs/application-config/src/lib/applications-config.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
interface ApplicationConfig {
host: string;
port: string;
name: string;
}

const DEFAULT_HOST = 'localhost';
Expand All @@ -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',
};
10 changes: 10 additions & 0 deletions libs/application-config/src/lib/subgraphs-config.ts
Original file line number Diff line number Diff line change
@@ -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`,
},
];

0 comments on commit e3a6194

Please sign in to comment.