-
Notifications
You must be signed in to change notification settings - Fork 382
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support Apollo Federation 2 #2251
Merged
kamilmysliwiec
merged 17 commits into
nestjs:master
from
greguintow:feat/add-fed2-support
Jul 20, 2022
Merged
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
03eb37e
feat: add support for federation 2
greguintow dc10a21
fix: always print federated schema with directives
greguintow 6d46d0e
fix: check if subgraph is version 2 before generating type defs
greguintow df1f85e
chore: update package version
greguintow 1c1898b
fix: apollo subgraph version import
greguintow d873051
feat: check if is trying to use fed2 without apollo subgraph 2
greguintow 68405e8
feat: create util func to stringify object without quotes in props
greguintow 6c360b9
feat: allow to use alias directives for import in federation 2
greguintow 60ccd20
fix: resolve reference not working for federation 2
greguintow a4be311
feat: create e2e tests for federation 2
greguintow 787f443
chore: remove fed2 tests from jest-e2e
greguintow 93d206c
Merge branch 'nestjs:master' into feat/add-fed2-support
greguintow 87cdb1c
chore: revert package changes
greguintow d6b77b9
feat: create schema file config interface and migrate use fed2 to it
greguintow d9f7446
feat: create util functions for auto schema file config
greguintow 14f97bc
refactor: use fed2 implementation due to changes
greguintow cb6090d
refactor: federation 2 tests due to use fed2 changes
greguintow File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
22 changes: 22 additions & 0 deletions
22
packages/apollo/tests/code-first-graphql-federation2/gateway/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,22 @@ | ||
import { IntrospectAndCompose } from '@apollo/gateway-v2'; | ||
import { Module } from '@nestjs/common'; | ||
import { GraphQLModule } from '@nestjs/graphql'; | ||
import { ApolloGatewayDriver, ApolloGatewayDriverConfig } from '../../../lib'; | ||
|
||
@Module({ | ||
imports: [ | ||
GraphQLModule.forRoot<ApolloGatewayDriverConfig>({ | ||
driver: ApolloGatewayDriver, | ||
gateway: { | ||
debug: false, | ||
supergraphSdl: new IntrospectAndCompose({ | ||
subgraphs: [ | ||
{ name: 'users', url: 'http://localhost:3001/graphql' }, | ||
{ name: 'posts', url: 'http://localhost:3002/graphql' }, | ||
], | ||
}), | ||
}, | ||
}), | ||
], | ||
}) | ||
export class AppModule {} |
24 changes: 24 additions & 0 deletions
24
...ages/apollo/tests/code-first-graphql-federation2/posts-service/federation-posts.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,24 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { GraphQLModule } from '@nestjs/graphql'; | ||
import { ApolloServerPluginInlineTraceDisabled } from 'apollo-server-core'; | ||
import { ApolloDriverConfig } from '../../../lib'; | ||
import { ApolloFederationDriver } from '../../../lib/drivers'; | ||
import { PostsModule } from './posts/posts.module'; | ||
import { User } from './posts/user.entity'; | ||
|
||
@Module({ | ||
imports: [ | ||
GraphQLModule.forRoot<ApolloDriverConfig>({ | ||
driver: ApolloFederationDriver, | ||
autoSchemaFile: { | ||
useFed2: true, | ||
}, | ||
buildSchemaOptions: { | ||
orphanedTypes: [User], | ||
}, | ||
plugins: [ApolloServerPluginInlineTraceDisabled()], | ||
}), | ||
PostsModule, | ||
], | ||
}) | ||
export class AppModule {} |
10 changes: 10 additions & 0 deletions
10
packages/apollo/tests/code-first-graphql-federation2/posts-service/posts/post-type.enum.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 { registerEnumType } from '@nestjs/graphql'; | ||
|
||
export enum PostType { | ||
IMAGE = 'IMAGE', | ||
TEXT = 'TEXT', | ||
} | ||
|
||
registerEnumType(PostType, { | ||
name: 'PostType', | ||
}); |
23 changes: 23 additions & 0 deletions
23
packages/apollo/tests/code-first-graphql-federation2/posts-service/posts/posts.entity.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,23 @@ | ||
import { Directive, Field, ID, ObjectType } from '@nestjs/graphql'; | ||
import { PostType } from './post-type.enum'; | ||
|
||
@ObjectType() | ||
@Directive('@key(fields: "id")') | ||
export class Post { | ||
@Field(() => ID) | ||
id: string; | ||
|
||
@Field() | ||
title: string; | ||
|
||
@Field() | ||
body: string; | ||
|
||
userId: string; | ||
|
||
@Field({ nullable: true }) | ||
publishDate: Date; | ||
|
||
@Field(() => PostType, { nullable: true }) | ||
type: PostType; | ||
} |
9 changes: 9 additions & 0 deletions
9
packages/apollo/tests/code-first-graphql-federation2/posts-service/posts/posts.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,9 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { PostsResolvers } from './posts.resolvers'; | ||
import { UsersResolvers } from './users.resolvers'; | ||
import { PostsService } from './posts.service'; | ||
|
||
@Module({ | ||
providers: [PostsResolvers, PostsService, UsersResolvers], | ||
}) | ||
export class PostsModule {} |
42 changes: 42 additions & 0 deletions
42
packages/apollo/tests/code-first-graphql-federation2/posts-service/posts/posts.resolvers.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,42 @@ | ||
import { | ||
Args, | ||
ID, | ||
Mutation, | ||
Parent, | ||
Query, | ||
ResolveField, | ||
Resolver, | ||
} from '@nestjs/graphql'; | ||
import { PostType } from './post-type.enum'; | ||
import { Post } from './posts.entity'; | ||
import { PostsService } from './posts.service'; | ||
import { User } from './user.entity'; | ||
|
||
@Resolver(Post) | ||
export class PostsResolvers { | ||
constructor(private readonly postsService: PostsService) {} | ||
|
||
@Query(() => [Post]) | ||
getPosts( | ||
@Args('type', { nullable: true, type: () => PostType }) type: PostType, | ||
) { | ||
if (type) { | ||
return this.postsService.findByType(type); | ||
} else { | ||
return this.postsService.findAll(); | ||
} | ||
} | ||
|
||
@Mutation(() => Post) | ||
publishPost( | ||
@Args('id', { type: () => ID }) id, | ||
@Args('publishDate') publishDate: Date, | ||
) { | ||
return this.postsService.publish(id, publishDate); | ||
} | ||
|
||
@ResolveField('user', () => User, { nullable: true }) | ||
getUser(@Parent() post: Post) { | ||
return { __typename: 'User', id: post.userId }; | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
packages/apollo/tests/code-first-graphql-federation2/posts-service/posts/posts.service.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,39 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
import { Post } from './posts.entity'; | ||
import { PostType } from './post-type.enum'; | ||
|
||
@Injectable() | ||
export class PostsService { | ||
private readonly posts: Post[] = [ | ||
{ | ||
id: '1', | ||
title: 'HELLO WORLD', | ||
body: 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.', | ||
userId: '5', | ||
publishDate: new Date(0), | ||
type: PostType.TEXT, | ||
}, | ||
]; | ||
|
||
findAll() { | ||
return Promise.resolve(this.posts); | ||
} | ||
|
||
findById(id: string) { | ||
return Promise.resolve(this.posts.find((p) => p.id === id)); | ||
} | ||
|
||
findByUserId(id: string) { | ||
return Promise.resolve(this.posts.filter((p) => p.userId === id)); | ||
} | ||
|
||
findByType(type: PostType) { | ||
return Promise.resolve(this.posts.filter((p) => p.type === type)); | ||
} | ||
|
||
async publish(id: string, publishDate: Date) { | ||
const post = await this.findById(id); | ||
post.publishDate = publishDate; | ||
return post; | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
packages/apollo/tests/code-first-graphql-federation2/posts-service/posts/user.entity.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,8 @@ | ||
import { Directive, Field, ID, ObjectType } from '@nestjs/graphql'; | ||
|
||
@ObjectType() | ||
@Directive('@key(fields: "id")') | ||
export class User { | ||
@Field(() => ID) | ||
id: string; | ||
} |
14 changes: 14 additions & 0 deletions
14
packages/apollo/tests/code-first-graphql-federation2/posts-service/posts/users.resolvers.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 { ResolveField, Resolver } from '@nestjs/graphql'; | ||
import { Post } from './posts.entity'; | ||
import { PostsService } from './posts.service'; | ||
import { User } from './user.entity'; | ||
|
||
@Resolver(User) | ||
export class UsersResolvers { | ||
constructor(private readonly postsService: PostsService) {} | ||
|
||
@ResolveField('posts', () => [Post]) | ||
getPosts(reference: any) { | ||
return this.postsService.findByUserId(reference.id); | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
...ages/apollo/tests/code-first-graphql-federation2/users-service/federation-users.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,20 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { GraphQLModule } from '@nestjs/graphql'; | ||
import { ApolloServerPluginInlineTraceDisabled } from 'apollo-server-core'; | ||
import { ApolloDriverConfig } from '../../../lib'; | ||
import { ApolloFederationDriver } from '../../../lib/drivers'; | ||
import { UsersModule } from './users/users.module'; | ||
|
||
@Module({ | ||
imports: [ | ||
GraphQLModule.forRoot<ApolloDriverConfig>({ | ||
driver: ApolloFederationDriver, | ||
autoSchemaFile: { | ||
useFed2: true, | ||
}, | ||
plugins: [ApolloServerPluginInlineTraceDisabled()], | ||
}), | ||
UsersModule, | ||
], | ||
}) | ||
export class AppModule {} |
11 changes: 11 additions & 0 deletions
11
packages/apollo/tests/code-first-graphql-federation2/users-service/users/users.entity.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,11 @@ | ||
import { Directive, Field, ID, ObjectType } from '@nestjs/graphql'; | ||
|
||
@ObjectType() | ||
@Directive('@key(fields: "id")') | ||
export class User { | ||
@Field(() => ID) | ||
id: string; | ||
|
||
@Field() | ||
name: string; | ||
} |
8 changes: 8 additions & 0 deletions
8
packages/apollo/tests/code-first-graphql-federation2/users-service/users/users.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,8 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { UsersResolvers } from './users.resolvers'; | ||
import { UsersService } from './users.service'; | ||
|
||
@Module({ | ||
providers: [UsersResolvers, UsersService], | ||
}) | ||
export class UsersModule {} |
18 changes: 18 additions & 0 deletions
18
packages/apollo/tests/code-first-graphql-federation2/users-service/users/users.resolvers.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 { Args, ID, Query, Resolver, ResolveReference } from '@nestjs/graphql'; | ||
import { User } from './users.entity'; | ||
import { UsersService } from './users.service'; | ||
|
||
@Resolver(User) | ||
export class UsersResolvers { | ||
constructor(private readonly usersService: UsersService) {} | ||
|
||
@Query(() => User, { nullable: true }) | ||
getUser(@Args('id', { type: () => ID }) id: string) { | ||
return this.usersService.findById(id); | ||
} | ||
|
||
@ResolveReference() | ||
resolveReference(reference: { __typename: string; id: string }) { | ||
return this.usersService.findById(reference.id); | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
packages/apollo/tests/code-first-graphql-federation2/users-service/users/users.service.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,16 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
import { User } from './users.entity'; | ||
|
||
@Injectable() | ||
export class UsersService { | ||
private readonly users: User[] = [ | ||
{ | ||
id: '5', | ||
name: 'GraphQL', | ||
}, | ||
]; | ||
|
||
findById(id: string) { | ||
return Promise.resolve(this.users.find((p) => p.id === id)); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[Change Request]
federationVersion: 2