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
@@ -137,11 +137,13 @@ export class AuthorResolver {
137
137
138
138
> warning **Warning** The logic inside the `AuthorsService` and `PostsService` classes can be as simple or sophisticated as needed. The main point of this example is to show how to construct resolvers and how they can interact with other providers.
139
139
140
-
In the example above, we created the `AuthorResolver` which defines one query and one field resolver function. We can define multiple `@Query()`resolver functions, and they will be aggregated into a single Query type definition in the generated SDL and the appropriate keys in the resolver map. This allows you to create queries close to the models and services that they use, and to keep them well organized in modules.
140
+
In the example above, we created the `AuthorResolver` which defines one query and one field resolver function. To create a resolver, we annotate the class with the `@Resolver()`decorator. The argument passed in to the `@Resolver()` decorator is optional, but in our case, since we have also defined a **field resolver** (for the `posts` property of the `Author` object type), it becomes required; in that case, the value is used to indicate which class is the parent (type) for any field resolver defined within this class.
141
141
142
-
Note that to create a resolver, we must annotate the class with the `@Resolver()`decorator. The argument passed in to the `@Resolver()` decorator is optional, but since we have also defined a field resolver (for the `posts` property of the `Author` object type), it's required to indicate which class is a parent for this particular field resolver (`Author.posts` relation). In addition, we defined a first query to get the author object based on the `id` sent in the request. Queries enable clients to fetch data, but not to **modify** data. To specify that the method is a query handler, use the `@Query()` decorator.
142
+
We can define multiple `@Query()`resolver functions (both within this class, and in any other resolver class), and they will be aggregated into a single **Query type** definition in the generated SDL and the appropriate entries in the resolver map. This allows you to create queries close to the models and services that they use, and to keep them well organized in modules.
143
143
144
-
In the above examples, the `@Query()` and `@ResolveField()` decorators are associated with the types based on the method name. For example, consider the following construction from the example above:
144
+
In this example, we defined a query handler to get the author object based on the `id` sent in the request. To specify that the method is a query handler, use the `@Query()` decorator.
145
+
146
+
In the above examples, the `@Query()` decorator generates a query type name based on the method name. For example, consider the following construction from the example above:
145
147
146
148
```typescript
147
149
@Query(returns=>Author)
@@ -150,15 +152,15 @@ In the above examples, the `@Query()` and `@ResolveField()` decorators are assoc
150
152
}
151
153
```
152
154
153
-
This generates the resolver map entry for the author query in our schema (because the method name `author` matches the `author` field in the `Query` type):
155
+
This generates the following resolver map entry for the author query in our schema (the query type entry uses the same name as the method name):
154
156
155
157
```graphql
156
158
typeQuery {
157
159
author(id: Int!): Author
158
160
}
159
161
```
160
162
161
-
Conventionally, wewouldprefertodecouplethese, usingnameslike `getAuthor()` or `getPosts()` forourresolvermethods. Wecaneasilydothisbypassingthemappingnamesasargumentsofthedecorator, asshownbelow
- description: adescriptionthatwillbeusedtogenerateGraphQLschema documentation (e.g. in GraphQL playground); a `string`
198
+
- deprecationReason: sets query metadata to show the query as deprecated (e.g., in GraphQL playground); a `string`
199
+
- nullable: whether the query can return a null data response; `boolean` or `'items'` or `'itemsAndList'` (see above for details of `'items'` and `'itemsAndList'`)
200
+
201
+
Usually you won't have to pass an object into the `@Args()` decorator as seen with the `getAuthor()` method above. For example, if an identifier's type is string, the following construction is sufficient:
193
202
194
203
```typescript
195
204
@Args('id') id: string
196
205
```
197
206
198
-
However, the `number` type doesn't give us enough information about the expected GraphQL representation (`Int` vs. `Float`). Thus we have to **explicitly** pass the type reference.
207
+
However, in this case, the `number` type is used, and it presents a challenge. The `number` type doesn't give us enough information about the expected GraphQL representation (e.g., `Int` vs. `Float`). Thus we have to **explicitly** pass the type reference.
199
208
200
-
In addition, all queries can take multiple arguments. Let's imagine that we want to fetch an author based on its `firstName` and `lastName`. In this case, we can call `@Args` twice:
209
+
Query handler methods can take multiple arguments. Let's imagine that we want to fetch an author based on its `firstName` and `lastName`. In this case, we can call `@Args` twice:
201
210
202
211
```typescript
203
212
getAuthor(
@@ -206,9 +215,9 @@ getAuthor(
206
215
) {}
207
216
```
208
217
209
-
> info **Hint** Note that we can pass a second argument to the `@Args()` decorator, which is an options object. The options object allows us to specify a default value, description, deprecation reason, or if a field is nullable.
218
+
> info **Hint** Note that we can pass a second argument to the `@Args()` decorator, which is an options object. The options object allows us to specify a default value (`'defaultValue'` key), description (`'description'` key, ), deprecation reason (`'deprecationReason'` key), or if a field is nullable (`'nullable'` key).
210
219
211
-
With inline `@Args()` calls, the code becomes bloated. Instead, you can create a dedicated `GetAuthorArgs` class:
220
+
With inline `@Args()` calls, code like the example above becomes bloated. Instead, you can create a dedicated `GetAuthorArgs` class using the `@ArgsType()` decorator.
212
221
213
222
```typescript
214
223
@Args() args: GetAuthorArgs
@@ -221,7 +230,7 @@ import { MinLength } from 'class-validator';
0 commit comments