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
+2-2Lines changed: 2 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -63,7 +63,7 @@ As mentioned, these options will be forwarded to the `ApolloServer` constructor.
63
63
64
64
#### GraphQL playground
65
65
66
-
The playground is a graphical, interactive, in-browser GraphQL IDE, available by default on the same URL as the GraphQL server itself. To see the playground running, you need a basic GraphQL server configured and running. To see it now, you can install the [working example here](https://github.com/nestjs/nest/tree/master/sample/12-graphql-schema-first). Alternatively, if you're following along with these code samples, once you've complete the steps in the [Resolvers chapter](/graphql/resolvers-map), you can access the playground.
66
+
The playground is a graphical, interactive, in-browser GraphQL IDE, available by default on the same URL as the GraphQL server itself. To access the playground, you need a basic GraphQL server configured and running. To see it now, you can install the [working example here](https://github.com/nestjs/nest/tree/master/sample/12-graphql-schema-first). Alternatively, if you're following along with these code samples, once you've complete the steps in the [Resolvers chapter](/graphql/resolvers-map), you can access the playground.
67
67
68
68
With that in place, and with your application running in the background, open your web browser and navigate to `http://localhost:3000/graphql` (host and port may vary depending on your configuration). You should see the GraphQL playground, as shown below.
69
69
@@ -89,7 +89,7 @@ To use the code first approach, start by adding the `autoSchemaFile` property to
Copy file name to clipboardExpand all lines: content/graphql/resolvers-map.md
+47-16Lines changed: 47 additions & 16 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -8,6 +8,19 @@ In the code first approach, we don't write GraphQL SDL by hand. Instead we use T
8
8
9
9
Most of the definitions in a GraphQL schema are **object types**. Each object type you define should represent an object that an application client might need to interact with. For example, our sample API needs to be able to fetch a list of authors and their posts, so we should define the `Author` type and `Post` type to support this functionality.
10
10
11
+
In the schema first approach, we'd define such a schema with SDL like this:
import { Field, Int, ObjectType } from '@nestjs/graphql';
13
26
import { Post } from './post';
@@ -32,18 +45,15 @@ export class Author {
32
45
33
46
The `Author` object type has a collection of fields. Each field has a type. A field's type can be either an object type or a scalar type. A scalar type is a primitive (like `ID`, `String`, `Boolean`, or `Int`) that resolves to a single value. In addition to GraphQL's built-in scalar types, you can define custom scalar types.
34
47
35
-
The `Author` object type will result in generating the following part of the GraphQL schema in SDL:
48
+
The above `Author` object type definition will generate the SDL we showed above.
36
49
37
-
```graphql
38
-
typeAuthor {
39
-
id: Int!
40
-
firstName: String
41
-
lastName: String
42
-
posts: [Post]
43
-
}
44
-
```
50
+
The `@Field()` decorator accepts an optional function returning a type (e.g., `type => Int`), and optionally an object with the following keys:
45
51
46
-
The `@Field()` decoratorallowsspecifyingwhetherafieldisnullable (each field is non-nullable by default), providingatypefunction, settingadescription, ormarkingafieldasdeprecated:
52
+
-`nullable`: for specifying whether a field is nullable (in SDL, each field is non-nullable by default)
53
+
-`description`: for setting a field description
54
+
-`deprecationReason`: for marking a field as deprecated
> info **Hint** You can also add a description to, or deprecate, the whole object type: `@ObjectType({{ '{' }} description: 'Author model' {{ '}' }})`.
54
64
55
-
When the field is an array, we must manually indicate the array type as shown below:
65
+
When the field is an array, we must manually indicate the array type in the decorator type function, as shown below:
56
66
57
67
```typescript
58
68
@Field(type=> [Post])
@@ -68,7 +78,7 @@ To declare that the array's items (not the array itself) are nullable, set the `
68
78
posts: Post[];
69
79
```
70
80
71
-
> info **Hint** If both the array and its items are nullable, use the `'itemsAndList'` property instead.
81
+
> info **Hint** If both the array and its items are nullable, set `nullable` to `'itemsAndList'` instead.
72
82
73
83
Now that the `Author` object type is created, let's define the `Post` object type.
@@ -123,11 +135,30 @@ export class AuthorResolver {
123
135
124
136
> info **Hint** All decorators (e.g., `@Resolver`, `@ResolveField`, `@Args`, etc.) are exported from the `@nestjs/graphql` package.
125
137
126
-
> 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 resolvers can interact with other providers.
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
+
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.
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.
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:
In the example above, we created the `AuthorResolver` which defines one query and one field resolver. 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.
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):
154
+
155
+
```graphql
156
+
typeQuery {
157
+
author(id: Int!): Author
158
+
}
159
+
```
129
160
130
-
Conventionally, we would use something like `getAuthor()` or `getPosts()`as method names. We can easily do this by passing the real names as arguments of the decorator.
161
+
Conventionally, wewouldprefertodecouplethese, usingnameslike `getAuthor()` or `getPosts()` forourresolvermethods. Wecaneasilydothisbypassingthemappingnamesasargumentsofthedecorator, asshownbelow
0 commit comments