Skip to content

Commit 9329437

Browse files
docs(graphql-federation): minor updates
1 parent ef9567f commit 9329437

File tree

1 file changed

+49
-47
lines changed

1 file changed

+49
-47
lines changed

content/graphql/federation.md

Lines changed: 49 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@ 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
21-
$ npm install --save @apollo/federation`
21+
$ npm install --save @apollo/federation
2222
```
2323

2424
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.
@@ -37,15 +37,15 @@ extend type Query {
3737
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.
3838

3939
```typescript
40-
import { Args, Query, Resolver, ResolveReference } from "@nestjs/graphql";
41-
import { UsersService } from "./users.service";
40+
import { Args, Query, Resolver, ResolveReference } from '@nestjs/graphql';
41+
import { UsersService } from './users.service';
4242

43-
@Resolver("User")
43+
@Resolver('User')
4444
export class UsersResolvers {
4545
constructor(private readonly usersService: UsersService) {}
4646

4747
@Query()
48-
getUser(@Args("id") id: string) {
48+
getUser(@Args('id') id: string) {
4949
return this.usersService.findById(id);
5050
}
5151

@@ -59,17 +59,17 @@ export class UsersResolvers {
5959
Finally, we hook everything up in a module together with a `GraphQLFederationModule`. This module accepts the same options as the regular `GraphQLModule`.
6060

6161
```typescript
62-
import { Module } from "@nestjs/common";
63-
import { GraphQLFederationModule } from "@nestjs/graphql";
64-
import { UsersResolvers } from "./users.resolvers";
62+
import { Module } from '@nestjs/common';
63+
import { GraphQLFederationModule } from '@nestjs/graphql';
64+
import { UsersResolvers } from './users.resolvers';
6565

6666
@Module({
6767
imports: [
6868
GraphQLFederationModule.forRoot({
69-
typePaths: ["**/*.graphql"]
70-
})
69+
typePaths: ['**/*.graphql'],
70+
}),
7171
],
72-
providers: [UsersResolvers]
72+
providers: [UsersResolvers],
7373
})
7474
export class AppModule {}
7575
```
@@ -99,40 +99,40 @@ extend type Query {
9999
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 User service discussed above will be called on the `resolveReference` method.
100100

101101
```typescript
102-
import { Query, Resolver, Parent, ResolveProperty } from "@nestjs/graphql";
103-
import { PostsService } from "./posts.service";
104-
import { Post } from "./posts.interfaces";
102+
import { Query, Resolver, Parent, ResolveProperty } from '@nestjs/graphql';
103+
import { PostsService } from './posts.service';
104+
import { Post } from './posts.interfaces';
105105

106-
@Resolver("Post")
106+
@Resolver('Post')
107107
export class PostsResolvers {
108108
constructor(private readonly postsService: PostsService) {}
109109

110-
@Query("getPosts")
110+
@Query('getPosts')
111111
getPosts() {
112112
return this.postsService.findAll();
113113
}
114114

115-
@ResolveProperty("user")
115+
@ResolveProperty('user')
116116
getUser(@Parent() post: Post) {
117-
return { __typename: "User", id: post.userId };
117+
return { __typename: 'User', id: post.userId };
118118
}
119119
}
120120
```
121121

122122
The Posts service has virtually the same module, but is included below for the sake of completeness:
123123

124124
```typescript
125-
import { Module } from "@nestjs/common";
126-
import { GraphQLFederationModule } from "@nestjs/graphql";
127-
import { PostsResolvers } from "./posts.resolvers";
125+
import { Module } from '@nestjs/common';
126+
import { GraphQLFederationModule } from '@nestjs/graphql';
127+
import { PostsResolvers } from './posts.resolvers';
128128

129129
@Module({
130130
imports: [
131131
GraphQLFederationModule.forRoot({
132-
typePaths: ["**/*.graphql"]
133-
})
132+
typePaths: ['**/*.graphql'],
133+
}),
134134
],
135-
providers: [PostsResolvers]
135+
providers: [PostsResolvers],
136136
})
137137
export class AppModule {}
138138
```
@@ -144,24 +144,24 @@ First install the optional dependency for the gateway: `npm install --save @apol
144144
Our gateway only needs a list of endpoints and will auto-discover the schemas from there. The code for our gateway is therefore very short:
145145

146146
```typescript
147-
import { Module } from "@nestjs/common";
148-
import { GraphQLGatewayModule } from "@nestjs/graphql";
147+
import { Module } from '@nestjs/common';
148+
import { GraphQLGatewayModule } from '@nestjs/graphql';
149149

150150
@Module({
151151
imports: [
152152
GraphQLGatewayModule.forRoot({
153153
server: {
154154
// ... Apollo server options
155-
cors: true
155+
cors: true,
156156
},
157157
gateway: {
158158
serviceList: [
159-
{ name: "users", url: "http://user-service/graphql" },
160-
{ name: "posts", url: "http://post-service/graphql" }
161-
]
162-
}
163-
})
164-
]
159+
{ name: 'users', url: 'http://user-service/graphql' },
160+
{ name: 'posts', url: 'http://post-service/graphql' },
161+
],
162+
},
163+
}),
164+
],
165165
})
166166
export class AppModule {}
167167
```
@@ -170,12 +170,12 @@ export class AppModule {}
170170
171171
#### Sharing context
172172

173-
You can customize the requests between the gateway and federated services using a build service. This allows you to share context about the request. You can easily extend the default `RemoteGraphQLDataSource` and implement one of the hooks. Please refer to [Apollo Docs on RemoteGraphQLDataSource](https://www.apollographql.com/docs/apollo-server/api/apollo-gateway/#remotegraphqldatasource) for more information about the possibilities.
173+
You can customize the requests between the gateway and federated services using a build service. This allows you to share context about the request. You can easily extend the default `RemoteGraphQLDataSource` and implement one of the hooks. Please refer to [Apollo Docs](https://www.apollographql.com/docs/apollo-server/api/apollo-gateway/#remotegraphqldatasource) on `RemoteGraphQLDataSource` for more information about the possibilities.
174174

175175
```typescript
176-
import { Module } from "@nestjs/common";
177-
import { GATEWAY_BUILD_SERVICE, GraphQLGatewayModule } from "@nestjs/graphql";
178-
import { RemoteGraphQLDataSource } from "@apollo/gateway";
176+
import { Module } from '@nestjs/common';
177+
import { GATEWAY_BUILD_SERVICE, GraphQLGatewayModule } from '@nestjs/graphql';
178+
import { RemoteGraphQLDataSource } from '@apollo/gateway';
179179
import { decode } from 'jsonwebtoken';
180180

181181
class AuthenticatedDataSource extends RemoteGraphQLDataSource {
@@ -189,15 +189,15 @@ class AuthenticatedDataSource extends RemoteGraphQLDataSource {
189189
providers: [
190190
{
191191
provide: AuthenticatedDataSource,
192-
useValue: AuthenticatedDataSource
192+
useValue: AuthenticatedDataSource,
193193
},
194194
{
195195
provide: GATEWAY_BUILD_SERVICE,
196-
useFactory: (AuthenticatedDataSource) => {
196+
useFactory: AuthenticatedDataSource => {
197197
return ({ name, url }) => new AuthenticatedDataSource({ url });
198198
},
199-
inject: [AuthenticatedDataSource]
200-
}
199+
inject: [AuthenticatedDataSource],
200+
},
201201
],
202202
exports: [GATEWAY_BUILD_SERVICE],
203203
})
@@ -208,13 +208,15 @@ class BuildServiceModule {}
208208
GraphQLGatewayModule.forRootAsync({
209209
useFactory: async () => ({
210210
gateway: {
211-
serviceList: [/* services */],
211+
serviceList: [
212+
/* services */
213+
],
212214
},
213215
server: {
214216
context: ({ req }) => ({
215-
jwt: req.headers.authorization
216-
})
217-
}
217+
jwt: req.headers.authorization,
218+
}),
219+
},
218220
}),
219221
imports: [BuildServiceModule],
220222
inject: [GATEWAY_BUILD_SERVICE],
@@ -226,4 +228,4 @@ export class AppModule {}
226228

227229
#### Async configuration
228230

229-
Both the Federation and Gateway modules support asynchronous initialization using the same `forRootAsync` that's documented in [Quick start](/graphql/quick-start#async-configuration)
231+
Both the Federation and Gateway modules support asynchronous initialization using the same `forRootAsync` that's documented in [Quick start](/graphql/quick-start#async-configuration).

0 commit comments

Comments
 (0)