Skip to content

Commit 854b3c1

Browse files
committed
updates
1 parent b023cce commit 854b3c1

File tree

2 files changed

+15
-10
lines changed

2 files changed

+15
-10
lines changed

content/graphql/quick-start.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ Depending on what underlying platform you use (Express or Fastify), you must als
1616

1717
#### Overview
1818

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**.
2020

2121
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.
2222

content/graphql/resolvers-map.md

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ export class Author {
4646
4747
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.
4848

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)).
5050
5151
The above `Author` object type definition will cause Nest to **generate** the SDL we showed above:
5252

@@ -59,7 +59,11 @@ type Author {
5959
}
6060
```
6161

62-
The `@Field()` decorator accepts a type function (e.g., `type => Int`), and optionally an object with the following keys:
62+
The `@Field()` decorator accepts an optional type function (e.g., `type => Int`), and optionally an options object.
63+
64+
The type function is required when there's the potential for ambiguity between the TypeScript type system and the GraphQL type system. Specifically: it is **not** required for `string` and `boolean` types; it **is** required for arrays, numbers (which must be mapped to either an `Int` or a `Float`) and object types. The type function should simply return the desired GraphQL type (as shown in various examples in these chapters).
65+
66+
The options object can have any of the following key/value pairs:
6367

6468
- `nullable`: for specifying whether a field is nullable (in SDL, each field is non-nullable by default); `boolean`
6569
- `description`: for setting a field description; `string`
@@ -114,6 +118,7 @@ export class Post {
114118
The `Post` object type will result in generating the following part of the GraphQL schema in SDL:
115119

116120
```graphql
121+
@@filename(schema.gql)
117122
type Post {
118123
id: Int!
119124
title: String!
@@ -123,7 +128,7 @@ type Post {
123128

124129
### Code first resolver
125130

126-
At this point, we've defined the objects that exist in our data graph, but clients don't yet have a way to interact with those objects. To address that, we need to define a resolver class. We'll start with the example below.
131+
At this point, we've defined the objects (type definitions) that can exist in our data graph, but clients don't yet have a way to interact with those objects. To address that, we need to define a resolver class. In the code first method, defining a resolver class both defines resolver functions **and** generates the **Query type**. This will be clear as we work through the example below:
127132

128133
```typescript
129134
@@filename(authors/authors.resolver)
@@ -151,9 +156,9 @@ export class AuthorsResolver {
151156
152157
> 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.
153158
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.
155160

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.
157162

158163
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.
159164

@@ -210,14 +215,14 @@ type Query {
210215
}
211216
```
212217

213-
The `@Query()` decorator's options object (where we pass `{{ '{' }}name: 'author'{{ '}' }}` above) accepts a number of keys:
218+
The `@Query()` decorator's options object (where we pass `{{ '{' }}name: 'author'{{ '}' }}` above) accepts a number of key/value pairs:
214219

215220
- `name`: name of the query; a `string`
216221
- `description`: a description that will be used to generate GraphQL schema documentation (e.g., in GraphQL playground); a `string`
217222
- `deprecationReason`: sets query metadata to show the query as deprecated (e.g., in GraphQL playground); a `string`
218223
- `nullable`: whether the query can return a null data response; `boolean` or `'items'` or `'itemsAndList'` (see above for details of `'items'` and `'itemsAndList'`)
219224

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:
221226

222227
```typescript
223228
@Args('id') id: string
@@ -505,7 +510,7 @@ The GraphQL plugin will automatically:
505510
- set the `nullable` property depending on the question mark (e.g. `name?: string` will set `nullable: true`)
506511
- set the `type` property depending on the type (supports arrays as well)
507512

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).
509514

510515
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:
511516

@@ -564,7 +569,7 @@ You can use the `options` property to customize the behavior of the plugin.
564569
```javascript
565570
"plugins": [
566571
{
567-
"name": "@nestjs/swagger/graphql",
572+
"name": "@nestjs/graphql/plugin",
568573
"options": {
569574
"typeFileNameSuffix": [".input.ts", ".args.ts"]
570575
}

0 commit comments

Comments
 (0)