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(metadata): restore original metadata to enable repeated buildSchema executions #1691

Closed
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
9 changes: 9 additions & 0 deletions src/metadata/metadata-storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ import {
} from "./utils";

export class MetadataStorage {
original: MetadataStorage | null = null;

queries: ResolverMetadata[] = [];

mutations: ResolverMetadata[] = [];
Expand Down Expand Up @@ -160,6 +162,12 @@ export class MetadataStorage {
}

build(options: SchemaGeneratorOptions) {
if (!this.original) {
this.original = Object.assign(new MetadataStorage(), this);
} else {
Object.assign(this, this.original);
}

this.classDirectives.reverse();
this.fieldDirectives.reverse();
this.argumentDirectives.reverse();
Expand All @@ -181,6 +189,7 @@ export class MetadataStorage {
}

clear() {
this.original = null;
this.queries = [];
this.mutations = [];
this.subscriptions = [];
Expand Down
54 changes: 54 additions & 0 deletions tests/functional/metadata-storage.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import "reflect-metadata";
import { createPubSub } from "@graphql-yoga/subscription";
import {
Args,
ArgsType,
type ClassType,
Field,
FieldResolver,
Int,
Mutation,
ObjectType,
Query,
Expand Down Expand Up @@ -121,4 +124,55 @@ describe("MetadataStorage", () => {
expect(getMetadataStorage().fieldResolvers).toHaveLength(2);
});
});

describe("immutability of resolver creation", () => {
beforeAll(async () => {
getMetadataStorage().clear();
});

it("should handle arguments correctly on multiple buildSchema runs", async () => {
@ObjectType()
class TestResponse {
@Field({ nullable: false })
data!: string;
}

@ArgsType()
class TestArgs {
@Field(() => Int, { defaultValue: 0 })
testField!: number;
}

function makeResolverClass() {
@Resolver(() => TestResponse)
abstract class TestResolver {
@Query(() => TestResponse)
async exampleQuery(@Args() args: TestArgs): Promise<TestResponse> {
return {
data: `resolver ${args.testField}`,
};
}
}

return TestResolver;
}

@Resolver()
class TestResolver extends makeResolverClass() {}

await buildSchema({
resolvers: [TestResolver],
});

expect(getMetadataStorage().queries).toHaveLength(1);
expect(getMetadataStorage().queries[0].params).toHaveLength(1);

await buildSchema({
resolvers: [TestResolver],
});

expect(getMetadataStorage().queries).toHaveLength(1);
expect(getMetadataStorage().queries[0].params).toHaveLength(1);
});
});
});