Skip to content

Commit c2703fe

Browse files
committed
docs(graphql): add code-first federation 2 for posts
1 parent be810d9 commit c2703fe

File tree

1 file changed

+47
-2
lines changed

1 file changed

+47
-2
lines changed

content/graphql/federation.md

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -743,7 +743,7 @@ export class AppModule {}
743743

744744
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.
745745

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).
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).
747747
748748
In the following sections, we'll upgrade the previous example to Federation 2.
749749

@@ -821,4 +821,49 @@ type Query {
821821

822822
#### Code first
823823

824-
TBD
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, similar to User service, we need to specify `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)