-
Notifications
You must be signed in to change notification settings - Fork 405
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(apollo-federation): added a multigraph use-case
- Loading branch information
Elies Lou
committed
Feb 10, 2022
1 parent
5b2fcd7
commit 376616a
Showing
11 changed files
with
398 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { PostModule } from './post/post.module'; | ||
import { UserModule } from './user/user.module'; | ||
|
||
@Module({ | ||
imports: [PostModule, UserModule], | ||
}) | ||
export class ApplicationModule {} |
18 changes: 18 additions & 0 deletions
18
packages/apollo/tests/code-first-multigraph/gateway.module.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { GqlModuleOptions, GraphQLModule } from '@nestjs/graphql'; | ||
import { ApolloGatewayDriver, ApolloGatewayDriverConfig } from '../../lib'; | ||
|
||
@Module({ | ||
imports: [ | ||
GraphQLModule.forRoot<ApolloGatewayDriverConfig & GqlModuleOptions>({ | ||
driver: ApolloGatewayDriver, | ||
gateway: { | ||
serviceList: [ | ||
{ name: 'user', url: 'http://localhost:3000/user/graphql' }, | ||
{ name: 'post', url: 'http://localhost:3000/post/graphql' }, | ||
], | ||
}, | ||
}), | ||
], | ||
}) | ||
export class GatewayModule {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { ValidationPipe } from '@nestjs/common'; | ||
import { NestFactory } from '@nestjs/core'; | ||
import { ApplicationModule } from './app.module'; | ||
import { GatewayModule } from './gateway.module'; | ||
|
||
export async function bootstrap() { | ||
const app = await NestFactory.create(ApplicationModule); | ||
app.useGlobalPipes(new ValidationPipe()); | ||
await app.listen(3000); | ||
|
||
const gateway = await NestFactory.create(GatewayModule); | ||
await gateway.listen(3001); | ||
} | ||
|
||
bootstrap(); |
14 changes: 14 additions & 0 deletions
14
packages/apollo/tests/code-first-multigraph/post/external-user/user.resolver.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { Resolver, ResolveField, Parent } from '@nestjs/graphql'; | ||
|
||
import { Post, Publication } from '../post.type'; | ||
import { User } from './user.type'; | ||
|
||
@Resolver(() => User) | ||
export class UserResolver { | ||
@ResolveField('posts', () => [Post]) | ||
userPosts(@Parent() user: User): Post[] { | ||
return [ | ||
{ id: 10, authorId: user.id, status: Publication.DRAFT, title: 'test' }, | ||
]; | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
packages/apollo/tests/code-first-multigraph/post/external-user/user.type.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { ObjectType, Directive, Field, ID } from '@nestjs/graphql'; | ||
|
||
@ObjectType() | ||
@Directive('@extends') | ||
@Directive('@key(fields: "id")') | ||
export class User { | ||
@Field(() => ID) | ||
@Directive('@external') | ||
id: number; | ||
} |
22 changes: 22 additions & 0 deletions
22
packages/apollo/tests/code-first-multigraph/post/post.module.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { GraphQLModule } from '@nestjs/graphql'; | ||
|
||
import { ApolloDriverConfig } from '../../../lib'; | ||
import { ApolloFederationDriver } from '../../../lib/drivers'; | ||
|
||
import { UserResolver } from './external-user/user.resolver'; | ||
import { PostResolver } from './post.resolver'; | ||
|
||
@Module({ | ||
imports: [ | ||
GraphQLModule.forRoot<ApolloDriverConfig>({ | ||
driver: ApolloFederationDriver, | ||
debug: false, | ||
autoSchemaFile: true, | ||
path: '/post/graphql', | ||
include: [PostModule], | ||
}), | ||
], | ||
providers: [PostResolver, UserResolver], | ||
}) | ||
export class PostModule {} |
48 changes: 48 additions & 0 deletions
48
packages/apollo/tests/code-first-multigraph/post/post.resolver.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import { | ||
Resolver, | ||
Query, | ||
ResolveField, | ||
Parent, | ||
ID, | ||
Args, | ||
} from '@nestjs/graphql'; | ||
|
||
import { User } from './external-user/user.type'; | ||
import { Post, Publication } from './post.type'; | ||
|
||
@Resolver(() => Post) | ||
export class PostResolver { | ||
store = new Set<Post>([ | ||
{ | ||
id: 1, | ||
authorId: 1, | ||
title: 'How to Jedi II', | ||
status: Publication.PUBLISHED, | ||
}, | ||
{ | ||
id: 2, | ||
authorId: 2, | ||
title: 'Why lightsabers are unreliable', | ||
status: Publication.DRAFT, | ||
}, | ||
]); | ||
|
||
@Query(() => [Post]) | ||
posts() { | ||
return [...this.store]; | ||
} | ||
|
||
@Query(() => [Post]) | ||
post(@Args('id', { type: () => ID }) id: number): Post | undefined { | ||
for (const post of this.store) { | ||
if (post.id.toString() === id.toString()) { | ||
return post; | ||
} | ||
} | ||
} | ||
|
||
@ResolveField(() => User) | ||
user(@Parent() post: Post) { | ||
return { __typename: 'User', id: post.authorId }; | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
packages/apollo/tests/code-first-multigraph/post/post.type.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { | ||
registerEnumType, | ||
ObjectType, | ||
Directive, | ||
Field, | ||
ID, | ||
} from '@nestjs/graphql'; | ||
|
||
export enum Publication { | ||
PUBLISHED, | ||
DRAFT, | ||
} | ||
|
||
registerEnumType(Publication, { name: 'Publication' }); | ||
|
||
@ObjectType() | ||
@Directive('@key(fields: "id")') | ||
export class Post { | ||
@Field(() => ID) | ||
id: number; | ||
|
||
@Field() | ||
title: string; | ||
|
||
@Field(() => Publication) | ||
status: Publication; | ||
|
||
@Field(() => ID) | ||
authorId: number; | ||
} |
21 changes: 21 additions & 0 deletions
21
packages/apollo/tests/code-first-multigraph/user/user.module.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { GraphQLModule } from '@nestjs/graphql'; | ||
|
||
import { ApolloDriverConfig } from '../../../lib'; | ||
import { ApolloFederationDriver } from '../../../lib/drivers'; | ||
|
||
import { UserResolver } from './user.resolver'; | ||
|
||
@Module({ | ||
imports: [ | ||
GraphQLModule.forRoot<ApolloDriverConfig>({ | ||
driver: ApolloFederationDriver, | ||
debug: false, | ||
autoSchemaFile: true, | ||
path: '/user/graphql', | ||
include: [UserModule], | ||
}), | ||
], | ||
providers: [UserResolver], | ||
}) | ||
export class UserModule {} |
51 changes: 51 additions & 0 deletions
51
packages/apollo/tests/code-first-multigraph/user/user.resolver.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import { | ||
Field, | ||
ObjectType, | ||
Resolver, | ||
Query, | ||
ID, | ||
Directive, | ||
Args, | ||
ResolveReference, | ||
} from '@nestjs/graphql'; | ||
|
||
@ObjectType() | ||
@Directive('@key(fields: "id")') | ||
class User { | ||
@Field(() => ID) | ||
id: number; | ||
|
||
@Field() | ||
name: string; | ||
} | ||
|
||
@Resolver(() => User) | ||
export class UserResolver { | ||
private readonly store = new Set<User>([ | ||
{ id: 1, name: 'Luke' }, | ||
{ id: 2, name: 'Han' }, | ||
]); | ||
|
||
@Query(() => [User]) | ||
users(): User[] { | ||
return [...this.store]; | ||
} | ||
|
||
@Query(() => User) | ||
user(@Args('id', { type: () => ID }) id: number): User | undefined { | ||
for (const user of this.store) { | ||
if (user.id.toString() === id.toString()) { | ||
return user; | ||
} | ||
} | ||
} | ||
|
||
@ResolveReference() | ||
resolveReference(reference: { | ||
__typename: string; | ||
id: number; | ||
}): User | undefined { | ||
console.log(reference); | ||
return this.user(reference.id); | ||
} | ||
} |
Oops, something went wrong.