Skip to content

Commit 7280d85

Browse files
Merge branch 'greguintow-feat/add-fed2-support'
2 parents b915495 + b524322 commit 7280d85

32 files changed

+937
-22
lines changed

packages/apollo/package.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,18 @@
1515
"url": "git+https://github.com/nestjs/graphql.git"
1616
},
1717
"scripts": {
18-
"test:e2e": "jest --config ./tests/jest-e2e.ts --runInBand",
18+
"test:e2e": "jest --config ./tests/jest-e2e.ts --runInBand && yarn test:e2e:fed2",
19+
"test:e2e:fed2": "jest --config ./tests/jest-e2e-fed2.ts --runInBand",
1920
"test:e2e:dev": "jest --config ./tests/jest-e2e.ts --runInBand --watch"
2021
},
2122
"bugs": {
2223
"url": "https://github.com/nestjs/graphql/issues"
2324
},
2425
"devDependencies": {
2526
"@apollo/gateway": "0.51.0",
27+
"@apollo/gateway-v2": "npm:@apollo/[email protected]",
28+
"@apollo/subgraph-v2": "npm:@apollo/[email protected]",
29+
"graphql-16": "npm:[email protected]",
2630
"@nestjs/common": "8.4.7",
2731
"@nestjs/core": "8.4.7",
2832
"@nestjs/platform-express": "8.4.7",
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { IntrospectAndCompose } from '@apollo/gateway-v2';
2+
import { Module } from '@nestjs/common';
3+
import { GraphQLModule } from '@nestjs/graphql';
4+
import { ApolloGatewayDriver, ApolloGatewayDriverConfig } from '../../../lib';
5+
6+
@Module({
7+
imports: [
8+
GraphQLModule.forRoot<ApolloGatewayDriverConfig>({
9+
driver: ApolloGatewayDriver,
10+
gateway: {
11+
debug: false,
12+
supergraphSdl: new IntrospectAndCompose({
13+
subgraphs: [
14+
{ name: 'users', url: 'http://localhost:3001/graphql' },
15+
{ name: 'posts', url: 'http://localhost:3002/graphql' },
16+
],
17+
}),
18+
},
19+
}),
20+
],
21+
})
22+
export class AppModule {}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { Module } from '@nestjs/common';
2+
import { GraphQLModule } from '@nestjs/graphql';
3+
import { ApolloServerPluginInlineTraceDisabled } from 'apollo-server-core';
4+
import { ApolloDriverConfig } from '../../../lib';
5+
import { ApolloFederationDriver } from '../../../lib/drivers';
6+
import { PostsModule } from './posts/posts.module';
7+
import { User } from './posts/user.entity';
8+
9+
@Module({
10+
imports: [
11+
GraphQLModule.forRoot<ApolloDriverConfig>({
12+
driver: ApolloFederationDriver,
13+
autoSchemaFile: {
14+
federation: 2,
15+
},
16+
buildSchemaOptions: {
17+
orphanedTypes: [User],
18+
},
19+
plugins: [ApolloServerPluginInlineTraceDisabled()],
20+
}),
21+
PostsModule,
22+
],
23+
})
24+
export class AppModule {}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { registerEnumType } from '@nestjs/graphql';
2+
3+
export enum PostType {
4+
IMAGE = 'IMAGE',
5+
TEXT = 'TEXT',
6+
}
7+
8+
registerEnumType(PostType, {
9+
name: 'PostType',
10+
});
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { Directive, Field, ID, ObjectType } from '@nestjs/graphql';
2+
import { PostType } from './post-type.enum';
3+
4+
@ObjectType()
5+
@Directive('@key(fields: "id")')
6+
export class Post {
7+
@Field(() => ID)
8+
id: string;
9+
10+
@Field()
11+
title: string;
12+
13+
@Field()
14+
body: string;
15+
16+
userId: string;
17+
18+
@Field({ nullable: true })
19+
publishDate: Date;
20+
21+
@Field(() => PostType, { nullable: true })
22+
type: PostType;
23+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { Module } from '@nestjs/common';
2+
import { PostsResolvers } from './posts.resolvers';
3+
import { UsersResolvers } from './users.resolvers';
4+
import { PostsService } from './posts.service';
5+
6+
@Module({
7+
providers: [PostsResolvers, PostsService, UsersResolvers],
8+
})
9+
export class PostsModule {}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import {
2+
Args,
3+
ID,
4+
Mutation,
5+
Parent,
6+
Query,
7+
ResolveField,
8+
Resolver,
9+
} from '@nestjs/graphql';
10+
import { PostType } from './post-type.enum';
11+
import { Post } from './posts.entity';
12+
import { PostsService } from './posts.service';
13+
import { User } from './user.entity';
14+
15+
@Resolver(Post)
16+
export class PostsResolvers {
17+
constructor(private readonly postsService: PostsService) {}
18+
19+
@Query(() => [Post])
20+
getPosts(
21+
@Args('type', { nullable: true, type: () => PostType }) type: PostType,
22+
) {
23+
if (type) {
24+
return this.postsService.findByType(type);
25+
} else {
26+
return this.postsService.findAll();
27+
}
28+
}
29+
30+
@Mutation(() => Post)
31+
publishPost(
32+
@Args('id', { type: () => ID }) id,
33+
@Args('publishDate') publishDate: Date,
34+
) {
35+
return this.postsService.publish(id, publishDate);
36+
}
37+
38+
@ResolveField('user', () => User, { nullable: true })
39+
getUser(@Parent() post: Post) {
40+
return { __typename: 'User', id: post.userId };
41+
}
42+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import { Injectable } from '@nestjs/common';
2+
import { Post } from './posts.entity';
3+
import { PostType } from './post-type.enum';
4+
5+
@Injectable()
6+
export class PostsService {
7+
private readonly posts: Post[] = [
8+
{
9+
id: '1',
10+
title: 'HELLO WORLD',
11+
body: 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.',
12+
userId: '5',
13+
publishDate: new Date(0),
14+
type: PostType.TEXT,
15+
},
16+
];
17+
18+
findAll() {
19+
return Promise.resolve(this.posts);
20+
}
21+
22+
findById(id: string) {
23+
return Promise.resolve(this.posts.find((p) => p.id === id));
24+
}
25+
26+
findByUserId(id: string) {
27+
return Promise.resolve(this.posts.filter((p) => p.userId === id));
28+
}
29+
30+
findByType(type: PostType) {
31+
return Promise.resolve(this.posts.filter((p) => p.type === type));
32+
}
33+
34+
async publish(id: string, publishDate: Date) {
35+
const post = await this.findById(id);
36+
post.publishDate = publishDate;
37+
return post;
38+
}
39+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { Directive, Field, ID, ObjectType } from '@nestjs/graphql';
2+
3+
@ObjectType()
4+
@Directive('@key(fields: "id")')
5+
export class User {
6+
@Field(() => ID)
7+
id: string;
8+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { ResolveField, Resolver } from '@nestjs/graphql';
2+
import { Post } from './posts.entity';
3+
import { PostsService } from './posts.service';
4+
import { User } from './user.entity';
5+
6+
@Resolver(User)
7+
export class UsersResolvers {
8+
constructor(private readonly postsService: PostsService) {}
9+
10+
@ResolveField('posts', () => [Post])
11+
getPosts(reference: any) {
12+
return this.postsService.findByUserId(reference.id);
13+
}
14+
}

0 commit comments

Comments
 (0)