Skip to content
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
merged 17 commits into from
Jul 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion packages/apollo/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,18 @@
"url": "git+https://github.com/nestjs/graphql.git"
},
"scripts": {
"test:e2e": "jest --config ./tests/jest-e2e.ts --runInBand",
"test:e2e": "jest --config ./tests/jest-e2e.ts --runInBand && yarn test:e2e:fed2",
"test:e2e:fed2": "jest --config ./tests/jest-e2e-fed2.ts --runInBand",
"test:e2e:dev": "jest --config ./tests/jest-e2e.ts --runInBand --watch"
},
"bugs": {
"url": "https://github.com/nestjs/graphql/issues"
},
"devDependencies": {
"@apollo/gateway": "0.51.0",
"@apollo/gateway-v2": "npm:@apollo/gateway@2.0.5",
"@apollo/subgraph-v2": "npm:@apollo/subgraph@2.0.5",
"graphql-16": "npm:graphql@16.5.0",
"@nestjs/common": "8.4.7",
"@nestjs/core": "8.4.7",
"@nestjs/platform-express": "8.4.7",
Expand Down
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 {}
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 {}
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',
});
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;
}
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 {}
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 };
}
}
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;
}
}
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;
}
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);
}
}
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,

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Change Request] federationVersion: 2

},
plugins: [ApolloServerPluginInlineTraceDisabled()],
}),
UsersModule,
],
})
export class AppModule {}
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;
}
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 {}
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);
}
}
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));
}
}
Loading