Skip to content

Commit e196889

Browse files
docs(graphql-federation): minor content tweaks
1 parent 57dcdb4 commit e196889

File tree

1 file changed

+36
-48
lines changed

1 file changed

+36
-48
lines changed

content/graphql/federation.md

Lines changed: 36 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,15 @@ In the next example, we'll set up a demo application with a gateway and two fede
1515

1616
#### Federated example: Users
1717

18-
First install the optional dependency for federation:
18+
First, install the optional dependency for federation:
1919

2020
```bash
2121
$ npm install --save @apollo/federation
2222
```
2323

24-
##### Schema first
24+
#### Schema first
2525

26-
The User service has a simple schema. Note the `@key` directive: it tells the Apollo query planner that a particular instance of User can be fetched if you have its `id`. Also note that we extend the `Query` type.
26+
The User service has a simple schema. Note the `@key` directive: it tells the Apollo query planner that a particular instance of User can be fetched if you have its `id`. Also, note that we extend the `Query` type.
2727

2828
```graphql
2929
type User @key(fields: "id") {
@@ -36,7 +36,7 @@ extend type Query {
3636
}
3737
```
3838

39-
Our resolver has one extra method: `resolveReference`. It's called by the Apollo Gateway whenever a related resource requires a User instance. We'll see an example of this in the Posts service later on. Please note the `@ResolveReference` decorator.
39+
Our resolver has one extra method: `resolveReference()`. It's called by the Apollo Gateway whenever a related resource requires a User instance. We'll see an example of this in the Posts service later on. Please note the `@ResolveReference()` decorator.
4040

4141
```typescript
4242
import { Args, Query, Resolver, ResolveReference } from '@nestjs/graphql';
@@ -76,7 +76,7 @@ import { UsersResolvers } from './users.resolvers';
7676
export class AppModule {}
7777
```
7878

79-
##### Code first
79+
#### Code first
8080

8181
Code first federation is very similar to regular code first GraphQL. We simply add some extra decorators to the `User` entity.
8282

@@ -86,30 +86,26 @@ import { Directive, Field, ID, ObjectType } from '@nestjs/graphql';
8686
@ObjectType()
8787
@Directive('@key(fields: "id")')
8888
export class User {
89-
@Field(type => ID)
90-
public id: number;
89+
@Field((type) => ID)
90+
id: number;
9191

9292
@Field()
93-
public name: string;
94-
95-
constructor(user: Partial<User>) {
96-
Object.assign(this, user);
97-
}
93+
name: string;
9894
}
9995
```
10096

101-
Our resolver has one extra method: `resolveReference`. It's called by the Apollo Gateway whenever a related resource requires a `User` instance. We'll see an example of this in the Posts service later on. Please note the `@ResolveReference` decorator.
97+
Our resolver has one extra method: `resolveReference()`. It's called by the Apollo Gateway whenever a related resource requires a `User` instance. We'll see an example of this in the Posts service later on. Please note the `@ResolveReference()` decorator.
10298

10399
```ts
104100
import { Args, Query, Resolver, ResolveReference } from '@nestjs/graphql';
105101
import { User } from './user.entity';
106102
import { UsersService } from './users.service';
107103

108-
@Resolver(of => User)
104+
@Resolver((of) => User)
109105
export class UsersResolvers {
110106
constructor(private usersService: UsersService) {}
111107

112-
@Query(returns => User)
108+
@Query((returns) => User)
113109
getUser(@Args('id') id: number): User {
114110
return this.usersService.findById(id);
115111
}
@@ -144,7 +140,7 @@ export class AppModule {}
144140

145141
Our Post service serves aggregated posts via a `getPosts` query, but also extends our `User` type with `user.posts`
146142

147-
##### Schema first
143+
#### Schema first
148144

149145
The Posts service references the User type in its schema by marking it with the `extend` keyword. It also adds one property to the User type. Note the `@key` directive used for matching instances of User, and the `@external` directive indicating that the `id` field is managed elsewhere.
150146

@@ -166,7 +162,7 @@ extend type Query {
166162
}
167163
```
168164

169-
Our resolver has one method of interest here: `getUser`. It returns a reference containing `__typename` and any additional properties your application needs to resolve the reference, in this case only an `id`. The `__typename` is used by the GraphQL Gateway to pinpoint the microservice responsible for the User type and request the instance. The Users service discussed above will be called on the `resolveReference` method.
165+
Our resolver has one method of interest here: `getUser()`. It returns a reference containing `__typename` and any additional properties your application needs to resolve the reference, in this case only an `id`. The `__typename` is used by the GraphQL Gateway to pinpoint the microservice responsible for the User type and request the instance. The Users service discussed above will be called on the `resolveReference()` method.
170166

171167
```typescript
172168
import { Query, Resolver, Parent, ResolveField } from '@nestjs/graphql';
@@ -219,16 +215,12 @@ import { Post } from './post.entity';
219215
@Directive('@extends')
220216
@Directive('@key(fields: "id")')
221217
export class User {
222-
@Field(type => ID)
218+
@Field((type) => ID)
223219
@Directive('@external')
224-
public id: number;
225-
226-
@Field(type => [Post])
227-
public posts?: Post[];
220+
id: number;
228221

229-
constructor(user: Partial<User>) {
230-
Object.assign(this, user);
231-
}
222+
@Field((type) => [Post])
223+
posts?: Post[];
232224
}
233225
```
234226

@@ -240,11 +232,11 @@ import { PostsService } from './posts.service';
240232
import { Post } from './post.entity';
241233
import { User } from './user.entity';
242234

243-
@Resolver(of => User)
235+
@Resolver((of) => User)
244236
export class UsersResolvers {
245237
constructor(private readonly postsService: PostsService) {}
246238

247-
@ResolveField(of => [Post])
239+
@ResolveField((of) => [Post])
248240
public posts(@Parent() user: User): Post[] {
249241
return this.postsService.forAuthor(user.id);
250242
}
@@ -260,21 +252,17 @@ import { User } from './user.entity';
260252
@ObjectType()
261253
@Directive('@key(fields: "id")')
262254
export class Post {
263-
@Field(type => ID)
264-
public id: number;
255+
@Field((type) => ID)
256+
id: number;
265257

266258
@Field()
267-
public title: string;
268-
269-
@Field(type => Int)
270-
public authorId: number;
259+
title: string;
271260

272-
@Field(type => User)
273-
public user?: User;
261+
@Field((type) => Int)
262+
authorId: number;
274263

275-
constructor(post: Partial<Post>) {
276-
Object.assign(this, post);
277-
}
264+
@Field((type) => User)
265+
user?: User;
278266
}
279267
```
280268

@@ -286,28 +274,28 @@ import { PostsService } from './posts.service';
286274
import { Post } from './post.entity';
287275
import { User } from './user.entity';
288276

289-
@Resolver(of => Post)
277+
@Resolver((of) => Post)
290278
export class PostsResolvers {
291279
constructor(private readonly postsService: PostsService) {}
292280

293-
@Query(returns => Post)
294-
public findPost(@Args('id') id: number): Post {
281+
@Query((returns) => Post)
282+
findPost(@Args('id') id: number): Post {
295283
return this.postsService.findOne(id);
296284
}
297285

298-
@Query(returns => [Post])
299-
public getPosts(): Post[] {
286+
@Query((returns) => [Post])
287+
getPosts(): Post[] {
300288
return this.postsService.all();
301289
}
302290

303-
@ResolveField(of => User)
304-
public user(@Parent() post: Post): any {
291+
@ResolveField((of) => User)
292+
user(@Parent() post: Post): any {
305293
return { __typename: 'User', id: post.authorId };
306294
}
307295
}
308296
```
309297

310-
And finally tie it together in a module. Note the schema build options, where we specify that `User` is an outside type.
298+
And finally, tie it together in a module. Note the schema build options, where we specify that `User` is an outside type.
311299

312300
```ts
313301
import { Module } from '@nestjs/common';
@@ -333,7 +321,7 @@ export class AppModule {}
333321

334322
#### Federated example: Gateway
335323

336-
First install the optional dependency for the gateway: `npm install --save @apollo/gateway`.
324+
First, install the optional dependency for the gateway: `npm install --save @apollo/gateway`.
337325

338326
Our gateway only needs a list of endpoints and will auto-discover the schemas from there. Therefore it is the same for code and schema first, and the code for our gateway is very short:
339327

@@ -387,7 +375,7 @@ class AuthenticatedDataSource extends RemoteGraphQLDataSource {
387375
},
388376
{
389377
provide: GATEWAY_BUILD_SERVICE,
390-
useFactory: AuthenticatedDataSource => {
378+
useFactory: (AuthenticatedDataSource) => {
391379
return ({ name, url }) => new AuthenticatedDataSource({ url });
392380
},
393381
inject: [AuthenticatedDataSource],

0 commit comments

Comments
 (0)