Skip to content

Commit

Permalink
fix(type-defs): don't omit types of name with __ inside (#1309)
Browse files Browse the repository at this point in the history
  • Loading branch information
AvKat committed Jan 6, 2024
1 parent 51ff280 commit e7b4e09
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/utils/createResolversMap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ function generateFieldsResolvers(fields: GraphQLFieldMap<any, any>): ResolverObj
export function createResolversMap(schema: GraphQLSchema): ResolversMap {
const typeMap = schema.getTypeMap();
return Object.keys(typeMap)
.filter(typeName => !typeName.includes("__"))
.filter(typeName => !typeName.startsWith("__"))
.reduce<ResolversMap>((resolversMap, typeName) => {
const type = typeMap[typeName];
if (type instanceof GraphQLObjectType) {
Expand Down
57 changes: 55 additions & 2 deletions tests/functional/typedefs-resolvers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import {
Arg,
Authorized,
Field,
FieldResolver,
InputType,
InterfaceType,
Mutation,
Expand Down Expand Up @@ -107,6 +108,15 @@ describe("typeDefs and resolvers", () => {
sampleType3StringField!: string;
}

@ObjectType("SampleType__4")
class SampleType4 {
@Field()
sampleInterfaceStringField!: string;

@Field()
sampleType4StringField!: string;
}

@InputType()
class SampleInput {
@Field()
Expand Down Expand Up @@ -241,8 +251,26 @@ describe("typeDefs and resolvers", () => {
}

pubSub = createPubSub();

@Service()
@Resolver(() => SampleType4)
class SampleObjectTypeWithDoubleUnderscoreInNameResolver {
@FieldResolver(() => String)
sampleResolvedField(): string {
return "sampleResolvedField";
}

@Query(() => SampleType4)
async sampleQueryOnObjectTypeWithDoubleUnderScore(): Promise<SampleType4> {
const type4 = new SampleType4();
type4.sampleInterfaceStringField = "sampleInterfaceStringField";
type4.sampleType4StringField = "sampleType4StringField";
return type4;
}
}

({ typeDefs, resolvers } = await buildTypeDefsAndResolvers({
resolvers: [SampleResolver],
resolvers: [SampleResolver, SampleObjectTypeWithDoubleUnderscoreInNameResolver],
authChecker: () => false,
pubSub,
container: Container,
Expand Down Expand Up @@ -286,6 +314,10 @@ describe("typeDefs and resolvers", () => {
const sampleType2 = schemaIntrospection.types.find(
it => it.name === "SampleType2",
) as IntrospectionObjectType;
const sampleType4 = schemaIntrospection.types.find(
it => it.name === "SampleType__4",
) as IntrospectionObjectType;

const sampleType1StringField = sampleType1.fields.find(
it => it.name === "sampleType1StringField",
)!;
Expand All @@ -299,6 +331,7 @@ describe("typeDefs and resolvers", () => {
expect(sampleType1.interfaces).toHaveLength(1);
expect(sampleType1.interfaces[0].name).toBe("SampleInterface");
expect(sampleType2StringField.deprecationReason).toBe("sampleType2StringFieldDeprecation");
expect(sampleType4.fields).toHaveLength(3);
});

it("should generate input type", async () => {
Expand Down Expand Up @@ -356,7 +389,7 @@ describe("typeDefs and resolvers", () => {
it => it.name === schemaIntrospection.queryType.name,
) as IntrospectionObjectType;

expect(queryType.fields).toHaveLength(8);
expect(queryType.fields).toHaveLength(9);
});

it("should generate mutations", async () => {
Expand Down Expand Up @@ -574,6 +607,26 @@ describe("typeDefs and resolvers", () => {
expect(enumValue).toBe("OptionTwoString");
});

it("should properly execute field resolver for object type with two underscores NOT in the beginning", async () => {
const document = gql`
query {
sampleQueryOnObjectTypeWithDoubleUnderScore {
sampleResolvedField
sampleInterfaceStringField
sampleType4StringField
}
}
`;

const { data } = await execute({ schema, document });

expect(data!.sampleQueryOnObjectTypeWithDoubleUnderScore).toEqual({
sampleResolvedField: "sampleResolvedField",
sampleInterfaceStringField: "sampleInterfaceStringField",
sampleType4StringField: "sampleType4StringField",
});
});

it("should properly run subscriptions", async () => {
const document = gql`
subscription {
Expand Down

0 comments on commit e7b4e09

Please sign in to comment.