Skip to content

Commit 4ba6e19

Browse files
Merge pull request #2276 from exarus/fix-list-field-in-intersection
fix(graphql): Support @field of list type in IntersectionType
2 parents 3bc379f + d36caa7 commit 4ba6e19

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

packages/graphql/lib/type-helpers/intersection-type.helper.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { Type } from '@nestjs/common';
2+
import { isFunction } from '@nestjs/common/utils/shared.utils';
23
import {
34
inheritPropertyInitializers,
45
inheritTransformationMetadata,
@@ -37,6 +38,14 @@ export function IntersectionType<A, B>(
3738
inheritTransformationMetadata(classBRef, IntersectionObjectType);
3839

3940
fields.forEach((item) => {
41+
if (isFunction(item.typeFn)) {
42+
/**
43+
* Execute type function eagarly to update the type options object (before "clone" operation)
44+
* when the passed function (e.g., @Field(() => Type)) lazily returns an array.
45+
*/
46+
item.typeFn();
47+
}
48+
4049
Field(item.typeFn, { ...item.options })(
4150
IntersectionObjectType.prototype,
4251
item.name,

packages/graphql/tests/plugin/type-helpers/intersection-type.helper.spec.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ describe('IntersectionType', () => {
2929

3030
firstName?: string;
3131

32+
@Field(() => [String])
33+
hobbies?: string[];
34+
3235
static [METADATA_FACTORY_NAME]() {
3336
return {
3437
firstName: { nullable: true, type: () => String },
@@ -41,11 +44,13 @@ describe('IntersectionType', () => {
4144
it('should inherit all fields from two types', () => {
4245
const prototype = Object.getPrototypeOf(UpdateUserDto);
4346
const { fields } = getFieldsAndDecoratorForType(prototype);
44-
expect(fields.length).toEqual(4);
47+
expect(fields.length).toEqual(5);
4548
expect(fields[0].name).toEqual('login');
4649
expect(fields[1].name).toEqual('password');
4750
expect(fields[2].name).toEqual('lastName');
48-
expect(fields[3].name).toEqual('firstName');
51+
expect(fields[3].name).toEqual('hobbies');
52+
expect(fields[3].options).toEqual({ isArray: true, arrayDepth: 1 });
53+
expect(fields[4].name).toEqual('firstName');
4954
expect(fields[0].directives.length).toEqual(1);
5055
expect(fields[0].directives).toContainEqual({
5156
fieldName: 'login',

0 commit comments

Comments
 (0)