Skip to content

Commit 5f92b92

Browse files
Merge pull request #1163 from Diluka/graphql-type-helper
docs(@nestjs/graphql): add new type hepler
2 parents 54ec5d8 + 3fca935 commit 5f92b92

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

content/graphql/mapped-types.md

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,49 @@ export class UpdateUserInput extends OmitType(CreateUserInput, ['email']) {}
9696

9797
> info **Hint** The `OmitType()` function is imported from the `@nestjs/graphql` package.
9898
99+
#### Intersection
100+
101+
The `IntersectionType()` function combines two types into one new type (class). For example, suppose we start with two types like:
102+
103+
```typescript
104+
@InputType()
105+
class CreateUserInput {
106+
@Field()
107+
email: string;
108+
109+
@Field()
110+
password: string;
111+
112+
@Field()
113+
firstName: string;
114+
}
115+
116+
@ObjectType()
117+
export class AdditionalUserInfo {
118+
@Field()
119+
nickname: string;
120+
121+
@Field()
122+
avatar: string;
123+
}
124+
```
125+
126+
We can generate a new type that combines all properties in both types.
127+
128+
```typescript
129+
@InputType()
130+
export class UpdateUserInput extends IntersectionType(CreateUserInput, AdditionalUserInfo) {}
131+
```
132+
133+
> info **Hint** The `IntersectionType()` function is imported from the `@nestjs/graphql` package.
134+
135+
The `IntersectionType()` function takes an optional third argument that is a reference to the decorator factory of the type being extended. If you don't pass a type as the third argument, it will use the first argument's decorator factory type. If you want to extend a class decorated with `@ObjectType`, pass `ObjectType` as the third argument. For example:
136+
137+
```typescript
138+
@InputType()
139+
export class UpdateUserInput extends IntersectionType(CreateUserInput, AdditionalUserInfo, ObjectType) {}
140+
```
141+
99142
#### Composition
100143

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

0 commit comments

Comments
 (0)