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
+4-6Lines changed: 4 additions & 6 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -12,15 +12,15 @@ Start by installing the required packages:
12
12
$ npm i --save @nestjs/graphql graphql-tools graphql
13
13
```
14
14
15
-
And depending on what you use (Express or Fastify), you need to install `apollo-server-express` or `apollo-server-fastify`.
15
+
Depending on what underlying platform you use (Express or Fastify), you must also install either`apollo-server-express` or `apollo-server-fastify`.
16
16
17
17
#### Overview
18
18
19
19
Nest offers two ways of building GraphQL applications, the **code first** and the **schema first** methods.
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.
22
22
23
-
In the **schema first** approach, the source of truth is GraphQL SDL (Schema Definition Language) files. SDL is a language-agnostic way to share schema files between different platforms. Nest automatically generates your TypeScript definitions based on the GraphQL schemas (using either classes or interfaces) to reduce redundancy.
23
+
In the **schema first** approach, the source of truth is GraphQL SDL (Schema Definition Language) files. SDL is a language-agnostic way to share schema files between different platforms. Nest automatically generates your TypeScript definitions (using either classes or interfaces) based on the GraphQL schemas to reduce the need to write redundant boilerplate code.
24
24
25
25
#### Getting started with GraphQL & TypeScript
26
26
@@ -105,17 +105,15 @@ A fully working code first sample is available [here](https://github.com/nestjs/
105
105
106
106
#### Schema first
107
107
108
-
To use the schema first approach, start by adding a `typePaths` property to the options object.
108
+
To use the schema first approach, start by adding a `typePaths` property to the options object. The `typePaths` property indicates where the `GraphQLModule` should look for GraphQL SDL schema definition files you'll be writing. These files will be combined in memory; this allows you to split your schemas into several files and locate them near their resolvers.
109
109
110
110
```typescript
111
111
GraphQLModule.forRoot({
112
112
typePaths: ['./**/*.graphql'],
113
113
}),
114
114
```
115
115
116
-
The `typePaths` property indicates where the `GraphQLModule` should look for GraphQL SDL schema definition files you'll be writing. These files will be combined in memory; this allows you to split your schemas into several files and locate them near their resolvers.
117
-
118
-
You will typically also need to have TypeScript definitions (classes and interfaces) that correspond to the GraphQL SDL types. Creating the corresponding TypeScript definitions by hand is redundant and tedious. It leaves us without a single source of truth -- each change made within SDL forces us to adjust TypeScript definitions as well. To address this, the `@nestjs/graphql` package can automatically generate TypeScript definitions from the abstract syntax tree ([AST](https://en.wikipedia.org/wiki/Abstract_syntax_tree)). To enable this feature, add the `definitions` options property when configuring the `GraphQLModule`.
116
+
You will typically also need to have TypeScript definitions (classes and interfaces) that correspond to the GraphQL SDL types. Creating the corresponding TypeScript definitions by hand is redundant and tedious. It leaves us without a single source of truth -- each change made within SDL forces us to adjust TypeScript definitions as well. To address this, the `@nestjs/graphql` package can **automatically generate** TypeScript definitions from the abstract syntax tree ([AST](https://en.wikipedia.org/wiki/Abstract_syntax_tree)). To enable this feature, add the `definitions` options property when configuring the `GraphQLModule`.
Copy file name to clipboardExpand all lines: content/graphql/resolvers-map.md
+9-9Lines changed: 9 additions & 9 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,10 +1,10 @@
1
1
### Resolvers
2
2
3
-
Resolvers provide the instructions for turning a [GraphQL](https://graphql.org/) operation (a query, mutation, or subscription) into data. They return the same shape of data we specify in our schema -- either synchronously or as a promise that resolves to a result of that shape. Typically, you create a **resolver map** manually. The `@nestjs/graphql` package, on the other hand, generates a resolver map automatically using the metadata provided by the decorators. To demonstrate the library basics, we'll create a simple authors API.
3
+
Resolvers provide the instructions for turning a [GraphQL](https://graphql.org/) operation (a query, mutation, or subscription) into data. They return the same shape of data we specify in our schema -- either synchronously or as a promise that resolves to a result of that shape. Typically, you create a **resolver map** manually. The `@nestjs/graphql` package, on the other hand, generates a resolver map automatically using the metadata provided by decorators you use to annotate classes. To demonstrate the process of using the package features to create a GraphQL API, we'll create a simple authors API.
4
4
5
5
#### Code first
6
6
7
-
In the code first approach, we don't create our GraphQL schema by writing GraphQL SDL by hand. Instead we use TypeScript decorators to generate the SDL from TypeScript class definitions. The `@nestjs/graphql` package reads the metadata defined through the decorators and automatically generates the schema for you.
7
+
In the code first approach, we don't follow the typical process of creating our GraphQL schema by writing GraphQL SDL by hand. Instead, we use TypeScript decorators to generate the SDL from TypeScript class definitions. The `@nestjs/graphql` package reads the metadata defined through the decorators and automatically generates the schema for you.
8
8
9
9
Most of the definitions in a GraphQL schema are **object types**. Each object type you define should represent a domain 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
@@ -43,7 +43,7 @@ export class Author {
43
43
44
44
> info **Hint** TypeScript's metadata reflection system has several limitations which make it impossible to, for instance, determine what properties a class consists of or recognize whether a given property is optional or required. Because of these limitations, we must either explicitly use the `@Field` decorator in our schema definition classes, or use a [CLI plugin](/graphql/resolvers#cli-plugin) to generate these for us.
45
45
46
-
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 an 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.
46
+
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.
47
47
48
48
> info **Hint** In addition to GraphQL's built-in scalar types, you can define custom scalar types.
49
49
@@ -80,7 +80,7 @@ When the field is an array, we must manually indicate the array type in the deco
80
80
posts: Post[];
81
81
```
82
82
83
-
> info **Hint** With `[ ]`, we can determine the depth of the array. For example, using `[[Int]]` would represent an integer matrix.
83
+
> info **Hint** With `[ ]`, we can indicate the depth of the array. For example, using `[[Int]]` would represent an integer matrix.
84
84
85
85
To declare that an array's items (not the array itself) are nullable, set the `nullable` property to `'items'` as shown below:
86
86
@@ -207,7 +207,7 @@ type Query {
207
207
The `@Query()` decorator'soptionsobject (where we pass `{{ '{' }}name: 'author'{{ '}' }}` above) acceptsanumberofkeys:
208
208
209
209
- name: nameofthequery; a `string`
210
-
- description: adescriptionthatwillbeusedtogenerateGraphQLschema documentation (e.g. in GraphQL playground); a `string`
210
+
- description: adescriptionthatwillbeusedtogenerateGraphQLschema documentation (e.g., in GraphQL playground); a `string`
211
211
- deprecationReason: sets query metadata to show the query as deprecated (e.g., in GraphQL playground); a `string`
212
212
- nullable: whether the query can return a null data response; `boolean` or `'items'` or `'itemsAndList'` (see above for details of `'items'` and `'itemsAndList'`)
213
213
@@ -230,13 +230,13 @@ getAuthor(
230
230
231
231
> 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).
232
232
233
-
With inline `@Args()` calls, code like the example above becomes bloated. Instead, you can create a dedicated `GetAuthorArgs` class using the `@ArgsType()` decorator.
233
+
With inline `@Args()` calls, code like the example above becomes bloated. Instead, you can create a dedicated `GetAuthorArgs`arguments class and access it in the handler method as follows:
234
234
235
235
```typescript
236
236
@Args() args: GetAuthorArgs
237
237
```
238
238
239
-
With the following body:
239
+
Create the `GetAuthorArgs()` class using `@ArgsType()` as shown below:
> info **Hint** Notethatargumentsclasseslike `GetAuthorArgs` playverywellwiththe `ValidationPipe` (read [more](/techniques/validation)).
267
267
268
268
#### Schema first
269
269
@@ -478,7 +478,7 @@ export class AuthorsModule {}
478
478
479
479
> info **Hint** Learn more about GraphQL queries [here](http://graphql.org/learn/queries/).
480
480
481
-
> 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 automatically for you.
481
+
> 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.
0 commit comments