You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -21,6 +21,8 @@ First install the optional dependency for federation:
21
21
$ npm install --save @apollo/federation
22
22
```
23
23
24
+
##### Schema first
25
+
24
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.
25
27
26
28
```graphql
@@ -38,20 +40,20 @@ Our resolver has one extra method: `resolveReference`. It's called by the Apollo
38
40
39
41
```typescript
40
42
import { Args, Query, Resolver, ResolveReference } from '@nestjs/graphql';
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.
Finally, we hook everything up in a module together with a `GraphQLFederationModule`. This module accepts the same options as the regular `GraphQLModule`.
Our Post microservice serves aggregated posts via a `getPosts` query, but also extends our User type with `user.posts`
145
+
146
+
##### Schema first
147
+
79
148
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.
import { Query, Resolver, Parent, ResolveProperty } from '@nestjs/graphql';
171
+
import { Query, Resolver, Parent, ResolveField } from '@nestjs/graphql';
103
172
import { PostsService } from './posts.service';
104
173
import { Post } from './posts.interfaces';
105
174
@@ -112,7 +181,7 @@ export class PostsResolvers {
112
181
returnthis.postsService.findAll();
113
182
}
114
183
115
-
@ResolveProperty('user')
184
+
@ResolveField('user')
116
185
getUser(@Parent() post: Post) {
117
186
return { __typename: 'User', id: post.userId };
118
187
}
@@ -137,11 +206,134 @@ import { PostsResolvers } from './posts.resolvers';
137
206
exportclassAppModule {}
138
207
```
139
208
209
+
##### Code first
210
+
211
+
We will need to create a class representing our User entity. Even though it lives in another microservice, we will be using and extending it. Note the `@extends` and `@external` directives
First install the optional dependency for the gateway: `npm install --save @apollo/gateway`.
143
335
144
-
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:
336
+
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:
0 commit comments