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
Copy file name to clipboardExpand all lines: content/graphql/quick-start.md
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -16,7 +16,7 @@ Depending on what underlying platform you use (Express or Fastify), you must als
16
16
17
17
#### Overview
18
18
19
-
Nest offers two ways of building GraphQL applications, the **code first** and the **schema first** methods.
19
+
Nest offers two ways of building GraphQL applications, the **code first** and the **schema first** methods. You should choose the one that works best for you. Most of the chapters in this GraphQL section are divided into two main parts: one you should follow if you adopt **code first**, and the other to be used if you adopt **schema first**.
20
20
21
21
In the **code first** approach, you use decorators and TypeScript classes to generate the corresponding GraphQL schema. This approach is useful if you prefer to work exclusively with TypeScript and avoid context switching between language syntaxes.
Copy file name to clipboardExpand all lines: content/graphql/resolvers-map.md
+14-9Lines changed: 14 additions & 9 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -46,7 +46,7 @@ export class Author {
46
46
47
47
The `Author` object type, like any class, is made of a collection of fields, with each field declaring a type. The types correspond to [GraphQL types](https://graphql.org/learn/schema/). A field's GraphQL type can be either another object type or a scalar type. A GraphQL scalar type is a primitive (like `ID`, `String`, `Boolean`, or `Int`) that resolves to a single value.
48
48
49
-
> info **Hint** In addition to GraphQL's built-in scalar types, you can define custom scalar types.
49
+
> info **Hint** In addition to GraphQL's built-in scalar types, you can define custom scalar types (read [more](/graphql/scalars)).
50
50
51
51
The above `Author` object type definition will cause Nest to **generate** the SDL we showed above:
52
52
@@ -59,7 +59,11 @@ type Author {
59
59
}
60
60
```
61
61
62
-
The `@Field()` decoratoracceptsatypefunction (e.g., `type => Int`), andoptionallyanobjectwiththefollowingkeys:
62
+
The `@Field()` decoratoracceptsanoptionaltypefunction (e.g., `type => Int`), andoptionallyanoptionsobject.
63
+
64
+
Thetypefunctionisrequiredwhenthere'sthepotentialforambiguitybetweentheTypeScripttypesystemandtheGraphQLtypesystem. Specifically: itis **not** requiredfor `string` and `boolean` types; it **is** requiredforarrays, numbers (which must be mapped to either an `Int` or a `Float`) andobjecttypes. ThetypefunctionshouldsimplyreturnthedesiredGraphQLtype (as shown in various examples in these chapters).
@@ -151,9 +156,9 @@ export class AuthorsResolver {
151
156
152
157
> 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.
153
158
154
-
In the example above, we created the `AuthorsResolver` which defines one query and one field resolver function. To create a resolver, we create a class with resolver functions as methods, and we annotate the class with the `@Resolver()` decorator.
159
+
In the example above, we created the `AuthorsResolver` which defines one query resolver function and one field resolver function. To create a resolver, we create a class with resolver functions as methods, and we annotate the class with the `@Resolver()` decorator.
155
160
156
-
The argument passed to the `@Resolver()` decorator is optional. However, 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 (i.e., the corresponding `ObjectType` class name) for any field resolver defined within this class.
161
+
The argument passed to the `@Resolver()` decorator is optional. However, in our case, since the class includes a **field resolver** function (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 (i.e., the corresponding `ObjectType` class name) for any field resolver defined within this class.
157
162
158
163
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.
159
164
@@ -210,14 +215,14 @@ type Query {
210
215
}
211
216
```
212
217
213
-
The `@Query()` decorator'soptionsobject (where we pass `{{ '{' }}name: 'author'{{ '}' }}` above) acceptsanumberofkeys:
218
+
The `@Query()` decorator'soptionsobject (where we pass `{{ '{' }}name: 'author'{{ '}' }}` above) acceptsanumberofkey/valuepairs:
214
219
215
220
- `name`: nameofthequery; a `string`
216
221
- `description`: adescriptionthatwillbeusedtogenerateGraphQLschema documentation (e.g., in GraphQL playground); a `string`
217
222
- `deprecationReason`: sets query metadata to show the query as deprecated (e.g., in GraphQL playground); a `string`
218
223
- `nullable`: whether the query can return a null data response; `boolean` or `'items'` or `'itemsAndList'` (see above for details of `'items'` and `'itemsAndList'`)
219
224
220
-
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:
225
+
Usually your `@Args()` decorator will be simple, and not require an object argument as seen with the `getAuthor()` method above. For example, if an identifier's type is string, the following construction is sufficient:
221
226
222
227
```typescript
223
228
@Args('id') id: string
@@ -505,7 +510,7 @@ The GraphQL plugin will automatically:
505
510
- set the `nullable` property depending on the question mark (e.g. `name?: string` will set `nullable: true`)
506
511
- set the `type` property depending on the type (supports arrays as well)
507
512
508
-
Please, note that your filenames **must have** one of the following suffixes: `['.input.ts', '.args.ts', '.entity.ts']` (e.g., `author.entity.ts`) in order to be analyzed by the plugin.
513
+
Please, note that your filenames **must have** one of the following suffixes in order to be analyzed by the plugin: `['.input.ts', '.args.ts', '.entity.ts']` (e.g., `author.entity.ts`). If you are using a different suffix, you can adjust the plugin's behavior by specifying the `typeFileNameSuffix` option (see below).
509
514
510
515
With what we've learned so far, you have to duplicate a lot of code to let the package know how your type should be declared in GraphQL. For example, you could define a simple `Author` class as follows:
511
516
@@ -564,7 +569,7 @@ You can use the `options` property to customize the behavior of the plugin.
0 commit comments