Skip to content

Commit 31d6051

Browse files
committed
docs(graphql): add schema-first apollo federation 2
1 parent 4c35b19 commit 31d6051

File tree

1 file changed

+84
-0
lines changed

1 file changed

+84
-0
lines changed

content/graphql/federation.md

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -738,3 +738,87 @@ 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 currently 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 has no originating subgraph, so we don't need to extend `Query` anymore. For more detail please refer to [this topic](https://www.apollographql.com/docs/federation/federation-2/new-in-federation-2#entities) in Apollo 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 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+
TBD

0 commit comments

Comments
 (0)