Skip to content

Commit 9fe49fe

Browse files
committed
docs(@nestjs/graphql): add new type hepler
relate to nestjs/graphql#776
1 parent 3dd5e32 commit 9fe49fe

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed

content/graphql/mapped-types.md

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,50 @@ 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+
119+
@Field()
120+
nickname: string;
121+
122+
@Field()
123+
avatar: string;
124+
}
125+
```
126+
127+
We can generate a new type that combines all properties in both two types.
128+
129+
```typescript
130+
@InputType()
131+
export class UpdateUserInput extends IntersectionType(CreateUserInput, AdditionalUserInfo) {}
132+
```
133+
134+
> info **Hint** The `IntersectionType()` function is imported from the `@nestjs/graphql` package.
135+
136+
The `IntersectionType()` function takes an optional third argument that is a reference to the decorator factory of the type being extended. It will use the first one's decorator, if we didn't pass `InputType` as the third argument. If you want to extend a class decorated with `@ObjectType`, pass `ObjectType` as the third argument. For example:
137+
138+
```typescript
139+
@InputType()
140+
export class UpdateUserInput extends IntersectionType(CreateUserInput, AdditionalUserInfo, ObjectType) {}
141+
```
142+
99143
#### Composition
100144

101145
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)