Skip to content

Commit 7a89f7d

Browse files
committed
docs(federation): add federation and gateway example
1 parent f47adbc commit 7a89f7d

35 files changed

Lines changed: 630 additions & 0 deletions

examples/4-federation/Readme.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
## Apollo Federation
2+
3+
Showcase of Apollo Federation approach, using the `TypeGraphQLFederationModule` and `GraphQLGatewayModule`.
4+
5+
### How to run?
6+
7+
This example is a bit more complex than the other ones.
8+
9+
Apart from running the main `index.ts` file by e.g. `npx ts-node ./examples/4-federation/index.ts`, you also need to run all the other services first:
10+
11+
- `npx ts-node ./examples/4-federation/accounts/index.ts`
12+
- `npx ts-node ./examples/4-federation/inventory/index.ts`
13+
- `npx ts-node ./examples/4-federation/products/index.ts`
14+
- `npx ts-node ./examples/4-federation/reviews/index.ts`
15+
16+
They will be listening on ports 3001-3004 and the main script on port 3000.
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { Module } from "@nestjs/common";
2+
import { TypeGraphQLFederationModule } from "../../../src";
3+
4+
import AccountsResolver from "./resolver";
5+
import User from "./user";
6+
import { resolveUserReference } from "./user-reference";
7+
8+
@Module({
9+
imports: [
10+
TypeGraphQLFederationModule.forFeature({
11+
orphanedTypes: [User],
12+
referenceResolvers: {
13+
User: {
14+
__resolveReference: resolveUserReference,
15+
},
16+
},
17+
}),
18+
],
19+
providers: [AccountsResolver],
20+
})
21+
export default class AccountModule {}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { Module } from "@nestjs/common";
2+
import { TypeGraphQLFederationModule } from "../../../src";
3+
4+
import AccountModule from "./account.module";
5+
6+
@Module({
7+
imports: [
8+
TypeGraphQLFederationModule.forRoot({
9+
validate: false,
10+
skipCheck: true,
11+
}),
12+
AccountModule,
13+
],
14+
})
15+
export default class AppModule {}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import User from "./user";
2+
3+
export const users: User[] = [
4+
createUser({
5+
id: "1",
6+
name: "Ada Lovelace",
7+
birthDate: "1815-12-10",
8+
username: "@ada",
9+
}),
10+
createUser({
11+
id: "2",
12+
name: "Alan Turing",
13+
birthDate: "1912-06-23",
14+
username: "@complete",
15+
}),
16+
];
17+
18+
function createUser(userData: Partial<User>) {
19+
return Object.assign(new User(), userData);
20+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import "reflect-metadata";
2+
import { NestFactory } from "@nestjs/core";
3+
import {
4+
NestFastifyApplication,
5+
FastifyAdapter,
6+
} from "@nestjs/platform-fastify";
7+
8+
import AppModule from "./app.module";
9+
10+
async function bootstrap() {
11+
const app = await NestFactory.create<NestFastifyApplication>(
12+
AppModule,
13+
new FastifyAdapter(),
14+
);
15+
16+
await app.listen(3001);
17+
}
18+
19+
bootstrap();
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { Resolver, Query } from "type-graphql";
2+
3+
import User from "./user";
4+
import { users } from "./data";
5+
6+
@Resolver(of => User)
7+
export default class AccountsResolver {
8+
@Query(returns => User)
9+
me(): User {
10+
return users[0];
11+
}
12+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { users } from "./data";
2+
import User from "./user";
3+
4+
export async function resolveUserReference(reference: Pick<User, "id">): Promise<User> {
5+
return users.find(u => u.id === reference.id)!;
6+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { Field, ObjectType, Directive, ID } from "type-graphql";
2+
3+
@Directive(`@key(fields: "id")`)
4+
@ObjectType()
5+
export default class User {
6+
@Field(type => ID)
7+
id!: string;
8+
9+
@Field()
10+
username!: string;
11+
12+
@Field()
13+
name!: string;
14+
15+
@Field()
16+
birthDate!: string;
17+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import "reflect-metadata";
2+
3+
import { Module } from "@nestjs/common";
4+
import { GraphQLGatewayModule } from "@nestjs/graphql";
5+
6+
@Module({
7+
imports: [
8+
GraphQLGatewayModule.forRoot({
9+
server: {
10+
tracing: true,
11+
playground: true,
12+
},
13+
gateway: {
14+
serviceList: [
15+
{ name: "accounts", url: "http://localhost:3001/graphql" },
16+
{ name: "inventory", url: "http://localhost:3002/graphql" },
17+
{ name: "products", url: "http://localhost:3003/graphql" },
18+
{ name: "reviews", url: "http://localhost:3004/graphql" },
19+
],
20+
},
21+
}),
22+
],
23+
})
24+
export default class AppModule {}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
query {
2+
topProducts {
3+
name
4+
price
5+
shippingEstimate
6+
inStock
7+
reviews {
8+
body
9+
author {
10+
name
11+
birthDate
12+
reviews {
13+
product {
14+
name
15+
}
16+
}
17+
}
18+
}
19+
}
20+
}

0 commit comments

Comments
 (0)