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/mapped-types.md
+21-20Lines changed: 21 additions & 20 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,14 +1,16 @@
1
1
### Mapped types
2
2
3
-
##### This chapter applies only to code first approach
3
+
> warning **Warning**This chapter applies only to the code first approach.
4
4
5
-
A common task is to take an existing type and make each of its properties optional. This happens often enough that NestJS provides a way to create new types based on old types — mapped types.
5
+
As you build out features like CRUD (Create/Read/Update/Delete) it's often useful to construct variants on a base entity type. Nest provides several utility functions that perform type transformations to make this task more convenient.
6
6
7
-
#### Partial
7
+
#### Partial: PartialType() function
8
8
9
-
Make all properties of a type optional.
9
+
When building input validation types (also called DTOs), it's often useful to build **create** and **update** variations on the same type. For example, the **create** variant may require all fields, while the **update** variant may make all fields optional.
10
10
11
-
Original type:
11
+
Nest provides the `PartialType()` utility function to make this task easier and minimize boilerplate.
12
+
13
+
The `PartialType()` function returns a type (class) with all the properties of the input type set to optional. For example, suppose we have a **create** type as follows:
12
14
13
15
```typescript
14
16
@InputType()
@@ -24,7 +26,7 @@ class CreateUserInput {
24
26
}
25
27
```
26
28
27
-
Mapped type:
29
+
By default, all of these fields are required. To create a type with the same fields, but with each one optional, use `PartialType()` passing the class reference (`CreateUserInput`) as an argument:
28
30
29
31
```typescript
30
32
@InputType()
@@ -33,13 +35,16 @@ export class UpdateUserInput extends PartialType(CreateUserInput) {}
33
35
34
36
> info **Hint** The `PartialType()` function is imported from the `@nestjs/graphql` package.
35
37
36
-
> warning **Warning** If you want to create an `InputType` based on the `ObjectType`, you must explicitly specify it in the function, as follows: `PartialType(CreateUserInput, InputType)`where `InputType`is a reference to a decorator factory.
38
+
The `PartialType()` function takes an optional second argument that is a reference to the decorator factory of the type being extended. In the example above, we are extending `CreateUserInput` which is annotated with the `@InputType()`decorator. We didn't need to pass `InputType`as the second argument since it's the default value. If you want to extend a class decorated with `@ObjectType`, pass `ObjectType` as the second argument. For example:
Constructs a type by picking the set of properties from type.
45
+
#### Pick: PickType() function
41
46
42
-
Original type:
47
+
The `PickType()` function constructs a new type (class) by picking a set of properties from an input type. For example, suppose we start with a type like:
43
48
44
49
```typescript
45
50
@InputType()
@@ -55,7 +60,7 @@ class CreateUserInput {
55
60
}
56
61
```
57
62
58
-
Mapped type:
63
+
We can pick a set of properties from this class using the `PickType()` utility function:
> info **Hint** The `PickType()` function is imported from the `@nestjs/graphql` package.
66
71
67
-
#### Pick
68
-
69
-
Constructs a type by picking all properties from type and then removing particular set of keys.
72
+
#### Omit: OmitType() function
70
73
71
-
Original type:
74
+
The `OmitType()` function constructs a type by picking all properties from an input type and then removing a particular set of keys. For example, suppose we start with a type like:
72
75
73
76
```typescript
74
77
@InputType()
@@ -84,7 +87,7 @@ class CreateUserInput {
84
87
}
85
88
```
86
89
87
-
Mapped type:
90
+
We can generate a derived type that has every property **except**`email` as shown below. In this construct, the second argument to `OmitType` is an array of property names.
The type mapping utility functions are composable. For example, the following will produce a type (class) that has all of the properties of the `CreateUserInput` type except for `email`, and those properties will be set to optional:
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
@@ -173,13 +173,13 @@ A fully working schema first sample is available [here](https://github.com/nestj
173
173
174
174
#### Accessing generated schema
175
175
176
-
To access the generated schema (in either code first or schema first approach), use the `GraphQLHost` class:
176
+
To access the generated schema (in either the code first or schema first approach), use the `GraphQLSchemaHost` class:
177
177
178
178
```typescript
179
179
const { schema } =app.get(GraphQLSchemaHost);
180
180
```
181
181
182
-
> info **Hint**Make sure to call the `GraphQLSchemaHost#schema` getter when the application is already initialized (after the `onModuleInit` hook triggered by either `app.listen()` or `app.init()` method.
182
+
> info **Hint**You must call the `GraphQLSchemaHost#schema` getter after the application has been initialized (after the `onModuleInit` hook has been triggered by either the `app.listen()` or `app.init()` method).
Copy file name to clipboardExpand all lines: content/graphql/resolvers-map.md
+28-18Lines changed: 28 additions & 18 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -308,19 +308,23 @@ type Query {
308
308
309
309
#### Class inheritance
310
310
311
-
Args:
311
+
YoucanusestandardTypeScriptclassinheritancetocreatebaseclasseswithgenericutilitytypefeatures (fields and field properties, validations, etc.) thatcanbeextended. Forexample, youmayhaveasetofpaginationrelatedargumentsthatalwaysincludethestandard `offset` and `limit` fields, butalsootherindexfieldsthataretype-specific. Youcansetupaclasshierarchyasshownbelow.
312
+
313
+
Base `@ArgsType()` class:
312
314
313
315
```typescript
314
316
@ArgsType()
315
317
classPaginationArgs {
316
-
@Field(type => Int)
318
+
@Field((type) => Int)
317
319
offset: number = 0;
318
320
319
-
@Field(type => Int)
321
+
@Field((type) => Int)
320
322
limit: number = 10;
321
323
}
322
324
```
323
325
326
+
Type specific sub-class of the base `@ArgsType()` class:
327
+
324
328
```typescript
325
329
@ArgsType()
326
330
classGetAuthorArgsextendsPaginationArgs {
@@ -333,19 +337,21 @@ class GetAuthorArgs extends PaginationArgs {
333
337
}
334
338
```
335
339
336
-
Object types:
340
+
The same approach can be taken with `@ObjectType()` objects. Define generic properties on the base class:
337
341
338
342
```typescript
339
343
@ObjectType()
340
344
classCharacter {
341
-
@Field(type=>Int)
345
+
@Field((type)=>Int)
342
346
id:number;
343
347
344
348
@Field()
345
349
name:string;
346
350
}
347
351
```
348
352
353
+
Add type-specific properties on sub-classes:
354
+
349
355
```typescript
350
356
@ObjectType()
351
357
classWarriorextendsCharacter {
@@ -354,13 +360,13 @@ class Warrior extends Character {
354
360
}
355
361
```
356
362
357
-
Resolver inheritance (creating base resolver):
363
+
You can use inheritance with a resolver as well. You can ensure type safety by combining inheritance and TypeScript generics. For example, to create a base class with a generic `findAll` query, use a construction like this:
358
364
359
365
```typescript
360
366
function BaseResolver<TextendsType<unknown>>(classRef:T):any {
@@ -369,20 +375,24 @@ function BaseResolver<T extends Type<unknown>>(classRef: T): any {
369
375
}
370
376
```
371
377
372
-
- explicit return type `any` is required: otherwise TypeScript complains about the usage of private class definition (recommended: define an interface instead of `any`)
378
+
Note the following:
379
+
380
+
- an explicit return type (`any` above) is required: otherwise TypeScript complains about the usage of a private class definition. Recommended: define an interface instead of using `any`.
373
381
-`Type` is imported from the `@nestjs/common` package
374
-
-`isAbstract: true` indicates that SDL shouldn't be generated for this class (you can set this for other types as well, e.g., object type)
382
+
- The `isAbstract: true` property indicates that SDL (Schema Definition Language statements) shouldn't be generated for this class. Note, you can set this property for other types as well to suppress SDL generation.
383
+
384
+
Here's how you could generate a concrete sub-class of the `BaseResolver`:
Copy file name to clipboardExpand all lines: content/graphql/schema-generator.md
+5-5Lines changed: 5 additions & 5 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,8 +1,8 @@
1
1
### Generating SDL
2
2
3
-
##### This chapter applies only to code first approach
3
+
> warning **Warning**This chapter applies only to the code first approach.
4
4
5
-
To manually generate (without running an application, connecting to the database, hooking up resolvers, etc.) a GraphQL SDL, use the `GraphQLSchemaBuilderModule`.
5
+
To manually generate a GraphQL SDL schema (i.e., without running an application, connecting to the database, hooking up resolvers, etc.), use the `GraphQLSchemaBuilderModule`.
6
6
7
7
```typescript
8
8
asyncfunction generateSchema() {
@@ -15,9 +15,9 @@ async function generateSchema() {
15
15
}
16
16
```
17
17
18
-
> info **Hint** The `GraphQLSchemaBuilderModule` and `GraphQLSchemaFactory` are imported the `@nestjs/graphql` package, while the`printSchema` function is imported from the `graphql` package.
18
+
> info **Hint** The `GraphQLSchemaBuilderModule` and `GraphQLSchemaFactory` are imported from the `@nestjs/graphql` package. The`printSchema` function is imported from the `graphql` package.
19
19
20
-
Multiple resolvers:
20
+
The `gqlSchemaFactory.create()` method takes an array of resolver class references. For example:
0 commit comments