Skip to content

Commit beade62

Browse files
committed
fix(resolvers): resolve #1142 name issue and add relevant tests
1 parent 3b641c9 commit beade62

File tree

2 files changed

+56
-4
lines changed

2 files changed

+56
-4
lines changed

src/utils/createResolversMap.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import { ResolversMap, EnumResolver, ResolverObject } from "../interfaces";
1515
export function createResolversMap(schema: GraphQLSchema): ResolversMap {
1616
const typeMap = schema.getTypeMap();
1717
return Object.keys(typeMap)
18-
.filter(typeName => !typeName.includes("__"))
18+
.filter(typeName => !typeName.startsWith("__"))
1919
.reduce<ResolversMap>((resolversMap, typeName) => {
2020
const type = typeMap[typeName];
2121
if (type instanceof GraphQLObjectType) {

tests/functional/typedefs-resolvers.ts

Lines changed: 55 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ import {
4545
ResolversMap,
4646
ResolverObject,
4747
ResolverOptions,
48+
FieldResolver,
4849
} from "../../src";
4950

5051
describe("typeDefs and resolvers", () => {
@@ -104,6 +105,14 @@ describe("typeDefs and resolvers", () => {
104105
sampleType3StringField: string;
105106
}
106107

108+
@ObjectType("SampleType__4")
109+
class SampleType4 {
110+
@Field()
111+
sampleInterfaceStringField: string;
112+
@Field()
113+
sampleType4StringField: string;
114+
}
115+
107116
@InputType()
108117
class SampleInput {
109118
@Field()
@@ -236,9 +245,26 @@ describe("typeDefs and resolvers", () => {
236245
}
237246
}
238247

248+
@Service()
249+
@Resolver(of => SampleType4)
250+
class SampleObjectTypeWithDoubleUnderscoreInNameResolver {
251+
@FieldResolver(returns => String)
252+
sampleResolvedField(): string {
253+
return "sampleResolvedField";
254+
}
255+
256+
@Query(returns => SampleType4)
257+
async sampleQueryOnObjectTypeWithDoubleUnderScore(): Promise<SampleType4> {
258+
const type4 = new SampleType4();
259+
type4.sampleInterfaceStringField = "sampleInterfaceStringField";
260+
type4.sampleType4StringField = "sampleType4StringField";
261+
return type4;
262+
}
263+
}
264+
239265
pubSub = new PubSub();
240266
({ typeDefs, resolvers } = await buildTypeDefsAndResolvers({
241-
resolvers: [SampleResolver],
267+
resolvers: [SampleResolver, SampleObjectTypeWithDoubleUnderscoreInNameResolver],
242268
authChecker: () => false,
243269
pubSub,
244270
container: Container,
@@ -281,6 +307,10 @@ describe("typeDefs and resolvers", () => {
281307
const sampleType2 = schemaIntrospection.types.find(
282308
it => it.name === "SampleType2",
283309
) as IntrospectionObjectType;
310+
const sampleType4 = schemaIntrospection.types.find(
311+
it => it.name === "SampleType__4",
312+
) as IntrospectionObjectType;
313+
284314
const sampleType1StringField = sampleType1.fields.find(
285315
it => it.name === "sampleType1StringField",
286316
)!;
@@ -294,6 +324,7 @@ describe("typeDefs and resolvers", () => {
294324
expect(sampleType1.interfaces).toHaveLength(1);
295325
expect(sampleType1.interfaces[0].name).toBe("SampleInterface");
296326
expect(sampleType2StringField.deprecationReason).toBe("sampleType2StringFieldDeprecation");
327+
expect(sampleType4.fields).toHaveLength(3);
297328
});
298329

299330
it("should generate input type", async () => {
@@ -303,7 +334,8 @@ describe("typeDefs and resolvers", () => {
303334
const sampleInputDefaultStringField = sampleInput.inputFields.find(
304335
it => it.name === "sampleInputDefaultStringField",
305336
)!;
306-
const sampleInputDefaultStringFieldType = sampleInputDefaultStringField.type as IntrospectionNamedTypeRef;
337+
const sampleInputDefaultStringFieldType =
338+
sampleInputDefaultStringField.type as IntrospectionNamedTypeRef;
307339

308340
expect(sampleInput.kind).toBe(TypeKind.INPUT_OBJECT);
309341
expect(sampleInput.inputFields).toHaveLength(2);
@@ -350,7 +382,7 @@ describe("typeDefs and resolvers", () => {
350382
it => it.name === schemaIntrospection.queryType.name,
351383
) as IntrospectionObjectType;
352384

353-
expect(queryType.fields).toHaveLength(8);
385+
expect(queryType.fields).toHaveLength(9);
354386
});
355387

356388
it("should generate mutations", async () => {
@@ -568,6 +600,26 @@ describe("typeDefs and resolvers", () => {
568600
expect(enumValue).toBe("OptionTwoString");
569601
});
570602

603+
it("should properly execute field resolver for object type with two underscores NOT in the beginning", async () => {
604+
const document = gql`
605+
query {
606+
sampleQueryOnObjectTypeWithDoubleUnderScore {
607+
sampleResolvedField
608+
sampleInterfaceStringField
609+
sampleType4StringField
610+
}
611+
}
612+
`;
613+
614+
const { data } = await execute(schema, document);
615+
616+
expect(data!.sampleQueryOnObjectTypeWithDoubleUnderScore).toEqual({
617+
sampleResolvedField: "sampleResolvedField",
618+
sampleInterfaceStringField: "sampleInterfaceStringField",
619+
sampleType4StringField: "sampleType4StringField",
620+
});
621+
});
622+
571623
it("should properly run subscriptions", async () => {
572624
const document = gql`
573625
subscription {

0 commit comments

Comments
 (0)