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(resolvers): resolve #1142 name issue and add relevant tests #1309

Merged
merged 1 commit into from
Jan 6, 2024
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
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