Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: use schemaName instead of methodName when building field resolve… #1284

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/metadata/metadata-storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,9 @@ export class MetadataStorage {
);
}

const typeField = typeMetadata.fields!.find(fieldDef => fieldDef.name === def.methodName)!;
const typeField = typeMetadata.fields!.find(
fieldDef => fieldDef.schemaName === def.schemaName,
)!;
if (!typeField) {
const shouldCollectFieldMetadata =
!options.resolvers ||
Expand Down
65 changes: 65 additions & 0 deletions tests/functional/resolvers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2479,5 +2479,70 @@ describe("Resolvers", () => {

expect(self).toBeInstanceOf(childResolver);
});

it("should allow duplicate fieldResolver methods with different schema names for inherited resolvers", async () => {
getMetadataStorage().clear();
const INHERITED_DYNAMIC_FIELD_NAME_1 = "dynamicallyNamedMethod1";
const INHERITED_DYNAMIC_FIELD_NAME_2 = "dynamicallyNamedMethod2";

const withDynamicallyNamedFieldResolver = (
classType: ClassType,
BaseResolverClass: ClassType,
name: string,
) => {
@Resolver(() => classType)
class DynamicallyNamedFieldResolver extends BaseResolverClass {
@FieldResolver({ name })
dynamicallyNamedField(): boolean {
return true;
}
}
return DynamicallyNamedFieldResolver;
};

@ObjectType()
class SampleObject {
@Field()
sampleField!: string;
}

@Resolver()
class SampleResolver {
@Query(() => SampleObject)
sampleObject(): SampleObject {
return { sampleField: "sampleText" };
}
}

const DynamicallyNamedFieldResolver1 = withDynamicallyNamedFieldResolver(
SampleObject,
SampleResolver,
INHERITED_DYNAMIC_FIELD_NAME_1,
);
const DynamicallyNamedFieldResolver2 = withDynamicallyNamedFieldResolver(
SampleObject,
DynamicallyNamedFieldResolver1,
INHERITED_DYNAMIC_FIELD_NAME_2,
);

const schemaInfo = await getSchemaInfo({
resolvers: [DynamicallyNamedFieldResolver2],
});
schemaIntrospection = schemaInfo.schemaIntrospection;
const sampleObjectType = schemaIntrospection.types.find(
type => type.name === "SampleObject",
) as IntrospectionObjectType;

const dynamicField1 = sampleObjectType.fields.find(
field => field.name === INHERITED_DYNAMIC_FIELD_NAME_1,
)!;

const dynamicField2 = sampleObjectType.fields.find(
field => field.name === INHERITED_DYNAMIC_FIELD_NAME_2,
)!;

expect(dynamicField1).toBeDefined();
expect(dynamicField2).toBeDefined();
});
});
});