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
@@ -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 access the playground, you need a basic GraphQL server configured and running. To see it now, you can install and build 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 and build the [working example here](https://github.com/nestjs/nest/tree/master/sample/23-graphql-code-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, you can then open your web browser and navigate to `http://localhost:3000/graphql` (host and port may vary depending on your configuration). You will then see the GraphQL playground, as shown below.
> 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.
150
152
151
-
In the example above, we created the `AuthorResolver` 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.
153
+
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.
152
154
153
155
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.
154
156
@@ -173,11 +175,14 @@ type Query {
173
175
}
174
176
```
175
177
178
+
> info **Hint** LearnmoreaboutGraphQLqueries [here](http://graphql.org/learn/queries/).
The `@Query()` decorator'soptionsobject (where we pass `{{ '{' }}name: 'author'{{ '}' }}` above) acceptsanumberofkeys:
208
213
209
-
- name: nameofthequery; a `string`
210
-
- description: adescriptionthatwillbeusedtogenerateGraphQLschema documentation (e.g., in GraphQL playground); a `string`
211
-
- deprecationReason: sets query metadata to show the query as deprecated (e.g., in GraphQL playground); a `string`
212
-
- nullable: whether the query can return a null data response; `boolean` or `'items'` or `'itemsAndList'` (see above for details of `'items'` and `'itemsAndList'`)
214
+
- `name`: nameofthequery; a `string`
215
+
- `description`: adescriptionthatwillbeusedtogenerateGraphQLschema documentation (e.g., in GraphQL playground); a `string`
216
+
- `deprecationReason`: sets query metadata to show the query as deprecated (e.g., in GraphQL playground); a `string`
217
+
- `nullable`: whether the query can return a null data response; `boolean` or `'items'` or `'itemsAndList'` (see above for details of `'items'` and `'itemsAndList'`)
213
218
214
219
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:
215
220
@@ -236,9 +241,10 @@ With inline `@Args()` calls, code like the example above becomes bloated. Instea
236
241
@Args() args: GetAuthorArgs
237
242
```
238
243
239
-
Create the `GetAuthorArgs()` class using `@ArgsType()` as shown below:
244
+
Create the `GetAuthorArgs` class using `@ArgsType()` as shown below:
240
245
241
246
```typescript
247
+
@@filename(authors/dto/get-author.args)
242
248
import { MinLength } from'class-validator';
243
249
import { Field, ArgsType } from'@nestjs/graphql';
244
250
@@ -292,11 +298,16 @@ type Query {
292
298
293
299
### Schema first resolver
294
300
295
-
Theschema above exposes a single query - `author(id: Int!): Author`. Let's now create an `AuthorResolver` class that resolves author queries:
301
+
Theschema above exposes a single query - `author(id: Int!): Author`.
302
+
303
+
> info **Hint** Learn more about GraphQL queries [here](http://graphql.org/learn/queries/).
304
+
305
+
Let's now create an `AuthorsResolver` class that resolves author queries:
296
306
297
307
```typescript
308
+
@@filename(authors/authors.resolver)
298
309
@Resolver('Author')
299
-
export class AuthorResolver {
310
+
export class AuthorsResolver {
300
311
constructor(
301
312
privateauthorsService: AuthorsService,
302
313
private postsService: PostsService,
@@ -356,8 +367,9 @@ type Query {
356
367
Conventionally, wewouldprefertodecouplethese, usingnameslike `getAuthor()` or `getPosts()` forourresolvermethods. Wecaneasilydothisbypassingthemappingnameasanargumenttothedecorator, asshownbelow:
357
368
358
369
```typescript
370
+
@@filename(authors/authors.resolver.ts)
359
371
@Resolver('Author')
360
-
exportclassAuthorResolver {
372
+
exportclassAuthorsResolver {
361
373
constructor(
362
374
private authorsService: AuthorsService,
363
375
private postsService: PostsService,
@@ -381,6 +393,7 @@ export class AuthorResolver {
381
393
Assuming that we use the schema first approach and have enabled the typings generation feature (with `outputAs: 'class'` as shown in the [previous](/graphql/quick-start) chapter), once you run the application it will generate the following file (in the location you specified in the `GraphQLModule.forRoot()` method. For example, in `src/graphql.ts`)
382
394
383
395
```typescript
396
+
@@filename(graphql.ts)
384
397
exportclassAuthor {
385
398
id:number;
386
399
firstName?:string;
@@ -426,7 +439,7 @@ export class CreatePostInput extends Post {
426
439
}
427
440
```
428
441
429
-
#### GraphQL resolver argument decorators
442
+
#### GraphQL argument decorators
430
443
431
444
We can access the standard GraphQL resolver arguments using dedicated decorators. Below is a comparison of the Nest decorators and the plain Apollo parameters they represent.
432
445
@@ -464,20 +477,19 @@ These arguments have the following meanings:
464
477
465
478
Once we're done with the above steps, we have declaratively specified all the information needed by the `GraphQLModule` to generate a resolver map. The `GraphQLModule` uses reflection to introspect the meta data provided via the decorators, and transforms classes into the correct resolver map automatically.
466
479
467
-
The only other thing you need to take care of is to **provide** (i.e., list as a `provider` in some module) the resolver class(es) (`AuthorResolver`), and importing the module (`AuthorsModule`) somewhere, so Nest will be able to utilize it.
480
+
The only other thing you need to take care of is to **provide** (i.e., list as a `provider` in some module) the resolver class(es) (`AuthorsResolver`), and importing the module (`AuthorsModule`) somewhere, so Nest will be able to utilize it.
468
481
469
482
For example, we can do this in an `AuthorsModule`, which can also provide other services needed in this context. Be sure to import `AuthorsModule` somewhere (e.g., in the root module, or some other module imported by the root module).
470
483
471
484
```typescript
485
+
@@filename(authors/authors.module)
472
486
@Module({
473
487
imports: [PostsModule],
474
-
providers: [AuthorsService, AuthorResolver],
488
+
providers: [AuthorsService, AuthorsResolver],
475
489
})
476
490
exportclassAuthorsModule {}
477
491
```
478
492
479
-
> info **Hint** Learn more about GraphQL queries [here](http://graphql.org/learn/queries/).
480
-
481
493
> info **Hint** It is helpful to organize your code by your so-called **domain model** (similar to the way you would organize entry points in a REST API). In this approach, keep your models (`ObjectType` classes), resolvers and services together within a Nest module representing the domain model. Keep all of these components in a single folder per module. When you do this, and use the [Nest CLI](/cli/overview) to generate each element, Nest will wire all of these parts together (locating files in appropriate folders, generating entries in `provider` and `imports` arrays, etc.) automatically for you.
482
494
483
495
#### CLI Plugin
@@ -497,6 +509,7 @@ Please, note that your filenames **must have** one of the following suffixes: `[
497
509
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:
498
510
499
511
```typescript
512
+
@@filename(authors/models/author.model)
500
513
@ObjectType()
501
514
exportclassAuthor {
502
515
@Field(type=>Int)
@@ -518,6 +531,7 @@ While not a significant issue with medium-sized projects, it becomes verbose & h
518
531
Now, with the GraphQL plugin enabled, the above class definition can be declared simply:
0 commit comments