From 14ce39e41dfee38c652be736664177fa2b1df421 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Victor=20Magalh=C3=A3es?= Date: Mon, 24 Jun 2024 05:35:57 -0300 Subject: [PATCH] Only compute `ClientSideBaseVisitor`'s `fragmentsGraph` once, at instantiation time (#10019) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * feat: Pre-compute ClientSideBaseVisitor's fragmentsGraph * chore: changeset * Update .changeset/rotten-eels-eat.md --------- Co-authored-by: Victor Magalhães Co-authored-by: Laurin Quast --- .changeset/rotten-eels-eat.md | 5 +++++ .../visitor-plugin-common/src/client-side-base-visitor.ts | 6 ++++-- 2 files changed, 9 insertions(+), 2 deletions(-) create mode 100644 .changeset/rotten-eels-eat.md diff --git a/.changeset/rotten-eels-eat.md b/.changeset/rotten-eels-eat.md new file mode 100644 index 00000000000..7edd8743d33 --- /dev/null +++ b/.changeset/rotten-eels-eat.md @@ -0,0 +1,5 @@ +--- +'@graphql-codegen/visitor-plugin-common': patch +--- + +Improve code generation performance by computing `ClientSideBaseVisitor`'s `fragmentsGraph` once at instantiation time. diff --git a/packages/plugins/other/visitor-plugin-common/src/client-side-base-visitor.ts b/packages/plugins/other/visitor-plugin-common/src/client-side-base-visitor.ts index 5dbad1f4536..696d7b14382 100644 --- a/packages/plugins/other/visitor-plugin-common/src/client-side-base-visitor.ts +++ b/packages/plugins/other/visitor-plugin-common/src/client-side-base-visitor.ts @@ -251,7 +251,8 @@ export class ClientSideBaseVisitor< private _onExecutableDocumentNode?: Unstable_OnExecutableDocumentNode; private _omitDefinitions?: boolean; - private _fragments: Map; + private readonly _fragments: ReadonlyMap; + private readonly fragmentsGraph: DepGraph; constructor( protected _schema: GraphQLSchema, @@ -289,6 +290,7 @@ export class ClientSideBaseVisitor< this._onExecutableDocumentNode = (rawConfig as any).unstable_onExecutableDocumentNode; this._omitDefinitions = (rawConfig as any).unstable_omitDefinitions; this._fragments = new Map(fragments.map(fragment => [fragment.name, fragment])); + this.fragmentsGraph = this._getFragmentsGraph(); autoBind(this); } @@ -519,7 +521,7 @@ export class ClientSideBaseVisitor< )};`; } - private get fragmentsGraph(): DepGraph { + private _getFragmentsGraph(): DepGraph { const graph = new DepGraph({ circular: true }); for (const fragment of this._fragments.values()) {