Skip to content

Commit 1ea0fea

Browse files
committed
fix: Field Middlewares do not work on interfaces of interfaces
The problem - If you have a class Foo, which implements a GQL interface IBar, and IBar implements GQL interface IBaz, field middlewares defined on IBar would work, but on IBaz they would get ignored. object-type-definition.factory.ts#generateFields will create a custom field resolver if a middleware definition exists on that field. Before the fix, the method would get a class; if it implements a (GQL) interface it will only get its interface and merge the interface fields with the class' fields, ignoring any interfaces implemented by the class' first interface. The fix creates a new private method which will recursively get the interfaces of a class, and the interfaces of the returned interfaces, and merge all the fields into an array.
1 parent 0bf8de6 commit 1ea0fea

File tree

1 file changed

+21
-7
lines changed

1 file changed

+21
-7
lines changed

packages/graphql/lib/schema-builder/factories/object-type-definition.factory.ts

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Injectable } from '@nestjs/common';
1+
import { Injectable, Type } from '@nestjs/common';
22
import { isUndefined } from '@nestjs/common/utils/shared.utils';
33
import {
44
GraphQLFieldConfigMap,
@@ -94,6 +94,23 @@ export class ObjectTypeDefinitionFactory {
9494
};
9595
}
9696

97+
private getRecursiveInterfaces(
98+
metadatas: ObjectTypeMetadata[],
99+
): ObjectTypeMetadata[] {
100+
if (!metadatas || !metadatas.length) return [];
101+
102+
const interfaces = metadatas.reduce<ObjectTypeMetadata[]>((prev, curr) => {
103+
return [
104+
...prev,
105+
...getInterfacesArray(curr.interfaces).map((it) =>
106+
TypeMetadataStorage.getInterfaceMetadataByTarget(it as Type<unknown>),
107+
),
108+
];
109+
}, []);
110+
111+
return [...interfaces, ...this.getRecursiveInterfaces(interfaces)];
112+
}
113+
97114
private generateFields(
98115
metadata: ObjectTypeMetadata,
99116
options: BuildSchemaOptions,
@@ -109,12 +126,9 @@ export class ObjectTypeDefinitionFactory {
109126

110127
let properties = [];
111128
if (metadata.interfaces) {
112-
const implementedInterfaces =
113-
TypeMetadataStorage.getInterfacesMetadata()
114-
.filter((it) =>
115-
getInterfacesArray(metadata.interfaces).includes(it.target),
116-
)
117-
.map((it) => it.properties);
129+
const implementedInterfaces = this.getRecursiveInterfaces([
130+
metadata,
131+
]).map((it) => it.properties);
118132

119133
implementedInterfaces.forEach((fields) =>
120134
properties.push(...(fields || [])),

0 commit comments

Comments
 (0)