Skip to content

Commit

Permalink
Merge pull request #2276 from exarus/fix-list-field-in-intersection
Browse files Browse the repository at this point in the history
fix(graphql): Support @field of list type in IntersectionType
  • Loading branch information
kamilmysliwiec committed Jul 8, 2022
2 parents 3bc379f + d36caa7 commit 4ba6e19
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
9 changes: 9 additions & 0 deletions packages/graphql/lib/type-helpers/intersection-type.helper.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Type } from '@nestjs/common';
import { isFunction } from '@nestjs/common/utils/shared.utils';
import {
inheritPropertyInitializers,
inheritTransformationMetadata,
Expand Down Expand Up @@ -37,6 +38,14 @@ export function IntersectionType<A, B>(
inheritTransformationMetadata(classBRef, IntersectionObjectType);

fields.forEach((item) => {
if (isFunction(item.typeFn)) {
/**
* Execute type function eagarly to update the type options object (before "clone" operation)
* when the passed function (e.g., @Field(() => Type)) lazily returns an array.
*/
item.typeFn();
}

Field(item.typeFn, { ...item.options })(
IntersectionObjectType.prototype,
item.name,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ describe('IntersectionType', () => {

firstName?: string;

@Field(() => [String])
hobbies?: string[];

static [METADATA_FACTORY_NAME]() {
return {
firstName: { nullable: true, type: () => String },
Expand All @@ -41,11 +44,13 @@ describe('IntersectionType', () => {
it('should inherit all fields from two types', () => {
const prototype = Object.getPrototypeOf(UpdateUserDto);
const { fields } = getFieldsAndDecoratorForType(prototype);
expect(fields.length).toEqual(4);
expect(fields.length).toEqual(5);
expect(fields[0].name).toEqual('login');
expect(fields[1].name).toEqual('password');
expect(fields[2].name).toEqual('lastName');
expect(fields[3].name).toEqual('firstName');
expect(fields[3].name).toEqual('hobbies');
expect(fields[3].options).toEqual({ isArray: true, arrayDepth: 1 });
expect(fields[4].name).toEqual('firstName');
expect(fields[0].directives.length).toEqual(1);
expect(fields[0].directives).toContainEqual({
fieldName: 'login',
Expand Down

0 comments on commit 4ba6e19

Please sign in to comment.