Skip to content

Commit 608e40a

Browse files
Merge pull request #2461 from UtopiaBeam/utopiabeam/apollo-fed-2
docs(graphql): add federation 2 doc
2 parents 0864142 + 81adb3a commit 608e40a

File tree

1 file changed

+130
-1
lines changed

1 file changed

+130
-1
lines changed

content/graphql/federation.md

Lines changed: 130 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ To quote the [Apollo docs](https://blog.apollographql.com/apollo-federation-f260
1111

1212
> warning **Warning** Federation currently does not support subscriptions.
1313
14-
In the following sections, we'll set up a demo application that consits of a gateway and two federated endpoints: Users service and Posts service.
14+
In the following sections, we'll set up a demo application that consists of a gateway and two federated endpoints: Users service and Posts service.
1515

1616
#### Federation with Apollo
1717

@@ -738,3 +738,132 @@ import { GraphQLModule } from '@nestjs/graphql';
738738
})
739739
export class AppModule {}
740740
```
741+
742+
### Federation 2
743+
744+
To quote the [Apollo docs](https://www.apollographql.com/docs/federation/federation-2/new-in-federation-2), Federation 2 improves developer experience from the original Apollo Federation (called Federation 1 in this doc), which is backward compatible with most original supergraphs.
745+
746+
> warning **Warning** Mercurius doesn't fully support Federation 2. You can see the list of libraries that support Federation 2 [here](https://www.apollographql.com/docs/federation/supported-subgraphs#javascript--typescript).
747+
748+
In the following sections, we'll upgrade the previous example to Federation 2.
749+
750+
#### Federated example: Users
751+
752+
One change in Federation 2 is that entities have no originating subgraph, so we don't need to extend `Query` anymore. For more detail please refer to [the entities topic](https://www.apollographql.com/docs/federation/federation-2/new-in-federation-2#entities) in Apollo Federation 2 docs.
753+
754+
#### Schema first
755+
756+
We can simply remove `extend` keyword from the schema.
757+
758+
```graphql
759+
type User @key(fields: "id") {
760+
id: ID!
761+
name: String!
762+
}
763+
764+
type Query {
765+
getUser(id: ID!): User
766+
}
767+
```
768+
769+
#### Code first
770+
771+
To use Federation 2, we need to specify the federation version in `autoSchemaFile` option.
772+
773+
```ts
774+
import {
775+
ApolloFederationDriver,
776+
ApolloFederationDriverConfig,
777+
} from '@nestjs/apollo';
778+
import { Module } from '@nestjs/common';
779+
import { UsersResolver } from './users.resolver';
780+
import { UsersService } from './users.service'; // Not included in this example
781+
782+
@Module({
783+
imports: [
784+
GraphQLModule.forRoot<ApolloFederationDriverConfig>({
785+
driver: ApolloFederationDriver,
786+
autoSchemaFile: {
787+
federation: 2,
788+
},
789+
}),
790+
],
791+
providers: [UsersResolver, UsersService],
792+
})
793+
export class AppModule {}
794+
```
795+
796+
#### Federated example: Posts
797+
798+
With the same reason as above, we don't need to extend `User` and `Query` anymore.
799+
800+
#### Schema first
801+
802+
We can simply remove `extend` and `external` directives from the schema
803+
804+
```graphql
805+
type Post @key(fields: "id") {
806+
id: ID!
807+
title: String!
808+
body: String!
809+
user: User
810+
}
811+
812+
type User @key(fields: "id") {
813+
id: ID!
814+
posts: [Post]
815+
}
816+
817+
type Query {
818+
getPosts: [Post]
819+
}
820+
```
821+
822+
#### Code first
823+
824+
Since we don't extend `User` entity anymore, we can simply remove `extends` and `external` directives from `User`.
825+
826+
```ts
827+
import { Directive, ObjectType, Field, ID } from '@nestjs/graphql';
828+
import { Post } from './post.entity';
829+
830+
@ObjectType()
831+
@Directive('@key(fields: "id")')
832+
export class User {
833+
@Field((type) => ID)
834+
id: number;
835+
836+
@Field((type) => [Post])
837+
posts?: Post[];
838+
}
839+
```
840+
841+
Also, similarly to the User service, we need to specify in the `GraphQLModule` to use Federation 2.
842+
843+
```ts
844+
import {
845+
ApolloFederationDriver,
846+
ApolloFederationDriverConfig,
847+
} from '@nestjs/apollo';
848+
import { Module } from '@nestjs/common';
849+
import { User } from './user.entity';
850+
import { PostsResolvers } from './posts.resolvers';
851+
import { UsersResolvers } from './users.resolvers';
852+
import { PostsService } from './posts.service'; // Not included in example
853+
854+
@Module({
855+
imports: [
856+
GraphQLModule.forRoot<ApolloFederationDriverConfig>({
857+
driver: ApolloFederationDriver,
858+
autoSchemaFile: {
859+
federation: 2,
860+
},
861+
buildSchemaOptions: {
862+
orphanedTypes: [User],
863+
},
864+
}),
865+
],
866+
providers: [PostsResolver, UsersResolver, PostsService],
867+
})
868+
export class AppModule {}
869+
```

0 commit comments

Comments
 (0)