@@ -110,12 +110,12 @@ export class UsersResolvers {
110
110
constructor (private usersService : UsersService ) {}
111
111
112
112
@Query (returns => User )
113
- getUser(@Args (' id' ) id : string ) {
113
+ getUser(@Args (' id' ) id : number ) : User {
114
114
return this .usersService .findById (id );
115
115
}
116
116
117
117
@ResolveReference ()
118
- resolveReference(reference : { __typename: string ; id: string }) {
118
+ resolveReference(reference : { __typename: string ; id: number }): User {
119
119
return this .usersService .findById (reference .id );
120
120
}
121
121
}
@@ -127,14 +127,15 @@ Finally, we hook everything up in a module together with a `GraphQLFederationMod
127
127
import { Module } from ' @nestjs/common' ;
128
128
import { GraphQLFederationModule } from ' @nestjs/graphql' ;
129
129
import { UsersResolvers } from ' ./users.resolvers' ;
130
+ import { UsersService } from ' ./users.service' ; // Not included in this example
130
131
131
132
@Module ({
132
133
imports: [
133
134
GraphQLFederationModule .forRoot ({
134
135
autoSchemaFile: true ,
135
136
}),
136
137
],
137
- providers: [UsersResolvers ],
138
+ providers: [UsersResolvers , UsersService ],
138
139
})
139
140
export class AppModule {}
140
141
```
@@ -211,7 +212,7 @@ export class AppModule {}
211
212
We will need to create a class representing our ` User ` entity. Even though it lives in another service, we will be using and extending it. Note the ` @extends ` and ` @external ` directives.
212
213
213
214
``` ts
214
- import { Directive , Field , ID , ObjectType } from ' @nestjs/graphql' ;
215
+ import { Directive , ObjectType , Field , ID } from ' @nestjs/graphql' ;
215
216
import { Post } from ' ./post.entity' ;
216
217
217
218
@ObjectType ()
@@ -223,10 +224,10 @@ export class User {
223
224
public id: number ;
224
225
225
226
@Field (type => [Post ])
226
- public posts: Post [];
227
+ public posts? : Post [];
227
228
228
- constructor (post : Partial <User >) {
229
- Object .assign (this , post );
229
+ constructor (user : Partial <User >) {
230
+ Object .assign (this , user );
230
231
}
231
232
}
232
233
```
@@ -235,17 +236,17 @@ We create the resolver for our extension on the `User` entity as follows:
235
236
236
237
``` ts
237
238
import { Parent , ResolveField , Resolver } from ' @nestjs/graphql' ;
238
- import { PostService } from ' ./post .service' ;
239
+ import { PostsService } from ' ./posts .service' ;
239
240
import { Post } from ' ./post.entity' ;
240
241
import { User } from ' ./user.entity' ;
241
242
242
243
@Resolver (of => User )
243
244
export class UsersResolvers {
244
- constructor (private readonly postService : PostService ) {}
245
+ constructor (private readonly postsService : PostsService ) {}
245
246
246
247
@ResolveField (of => [Post ])
247
- public posts(@Parent () user : User ) {
248
- return this .postService .forAuthor (user .id );
248
+ public posts(@Parent () user : User ): Post [] {
249
+ return this .postsService .forAuthor (user .id );
249
250
}
250
251
}
251
252
```
@@ -269,7 +270,7 @@ export class Post {
269
270
public authorId: number ;
270
271
271
272
@Field (type => User )
272
- public user: User ;
273
+ public user? : User ;
273
274
274
275
constructor (post : Partial <Post >) {
275
276
Object .assign (this , post );
@@ -280,28 +281,28 @@ export class Post {
280
281
And its resolver:
281
282
282
283
``` ts
283
- import { Query , Args , ResolveReference , Resolver } from ' @nestjs/graphql' ;
284
- import { PostService } from ' ./post .service' ;
284
+ import { Query , Args , ResolveField , Resolver , Parent } from ' @nestjs/graphql' ;
285
+ import { PostsService } from ' ./posts .service' ;
285
286
import { Post } from ' ./post.entity' ;
286
287
import { User } from ' ./user.entity' ;
287
288
288
289
@Resolver (of => Post )
289
- export class PostResolver {
290
- constructor (private readonly postService : PostService ) {}
290
+ export class PostsResolvers {
291
+ constructor (private readonly postsService : PostsService ) {}
291
292
292
293
@Query (returns => Post )
293
- public findPost(@Args (' id' ) id : number ) {
294
- return this .postService .findOne (id );
294
+ public findPost(@Args (' id' ) id : number ): Post {
295
+ return this .postsService .findOne (id );
295
296
}
296
297
297
298
@Query (returns => [Post ])
298
- public getPosts() {
299
- return this .postService .all ();
299
+ public getPosts(): Post [] {
300
+ return this .postsService .all ();
300
301
}
301
302
302
303
@ResolveField (of => User )
303
- public user(@Parent () post ) {
304
- return { __typename: ' User' , id: post .userId };
304
+ public user(@Parent () post : Post ) : any {
305
+ return { __typename: ' User' , id: post .authorId };
305
306
}
306
307
}
307
308
```
@@ -311,9 +312,10 @@ And finally tie it together in a module. Note the schema build options, where we
311
312
``` ts
312
313
import { Module } from ' @nestjs/common' ;
313
314
import { GraphQLFederationModule } from ' @nestjs/graphql' ;
314
- import { User } from ' ./user/user .entity' ;
315
- import { PostResolver } from ' ./post.resolver ' ;
315
+ import { User } from ' ./user.entity' ;
316
+ import { PostsResolvers } from ' ./posts.resolvers ' ;
316
317
import { UsersResolvers } from ' ./users.resolvers' ;
318
+ import { PostsService } from ' ./posts.service' ; // Not included in example
317
319
318
320
@Module ({
319
321
imports: [
@@ -324,9 +326,9 @@ import { UsersResolvers } from './users.resolvers';
324
326
},
325
327
}),
326
328
],
327
- providers: [PostResolver , UsersResolvers ],
329
+ providers: [PostsResolvers , UsersResolvers , PostsService ],
328
330
})
329
- export class ApplicationModule {}
331
+ export class AppModule {}
330
332
```
331
333
332
334
#### Federated example: Gateway
0 commit comments