diff --git a/packages/query-graphql/src/decorators/reference.decorator.ts b/packages/query-graphql/src/decorators/reference.decorator.ts index 174d75e44..a7610061f 100644 --- a/packages/query-graphql/src/decorators/reference.decorator.ts +++ b/packages/query-graphql/src/decorators/reference.decorator.ts @@ -6,14 +6,14 @@ import { BaseResolverOptions } from './resolver-method.decorator'; import { mergeBaseResolverOpts } from '../common'; const reflector = new ArrayReflector(REFERENCE_KEY); -export type ReferenceDecoratorOpts = Omit, 'DTO'>; +export type ReferenceDecoratorOpts = Omit, 'DTO' | 'keys'>; export type ReferenceTypeFunc = () => Class; interface ReferenceDescriptor { name: string; keys: ReferencesKeys; relationTypeFunc: () => Class; - relationOpts?: Omit, 'DTO'>; + relationOpts?: Omit, 'DTO' | 'keys'>; } function getReferenceDescriptors(DTOClass: Class): ReferenceDescriptor[] { diff --git a/packages/query-graphql/src/resolvers/relations/references-relation.resolver.ts b/packages/query-graphql/src/resolvers/relations/references-relation.resolver.ts index dcb952754..0fa2add90 100644 --- a/packages/query-graphql/src/resolvers/relations/references-relation.resolver.ts +++ b/packages/query-graphql/src/resolvers/relations/references-relation.resolver.ts @@ -16,6 +16,10 @@ const pluckFields = (dto: DTO, fieldMap: ReferencesKeys; }; +const allFieldsAreNull = (fields: Partial): boolean => { + return Object.entries(fields).reduce((previousNull, [, value]) => previousNull && value === null, true); +}; + const ReferencesMixin = (DTOClass: Class, reference: ResolverRelationReference) => < B extends Class>> >( @@ -33,9 +37,15 @@ const ReferencesMixin = (DTOClass: Class, reference: Resolve { nullable: reference.nullable, complexity: reference.complexity }, commonResolverOpts, ) - [`${baseNameLower}Reference`](@Parent() dto: DTO): RepresentationType { + [`${baseNameLower}Reference`](@Parent() dto: DTO): RepresentationType | null { + const fields = pluckFields(dto, reference.keys); + + if (allFieldsAreNull(fields)) { + return null; + } + // eslint-disable-next-line @typescript-eslint/naming-convention - return { __typename: baseName, ...pluckFields(dto, reference.keys) }; + return { __typename: baseName, ...fields }; } } return ReadOneMixin;