From 5a212f61f206d7b73f8abf04667480851aa6066e Mon Sep 17 00:00:00 2001 From: Kamil Kisiela Date: Tue, 23 Aug 2022 15:49:10 +0200 Subject: [PATCH] Avoid marking the same type as used twice (#308) Fixes #305 --- .changeset/shiny-rats-rule.md | 5 +++++ packages/libraries/client/src/internal/usage.ts | 10 ++++++++++ 2 files changed, 15 insertions(+) create mode 100644 .changeset/shiny-rats-rule.md diff --git a/.changeset/shiny-rats-rule.md b/.changeset/shiny-rats-rule.md new file mode 100644 index 0000000000..3235c50f1f --- /dev/null +++ b/.changeset/shiny-rats-rule.md @@ -0,0 +1,5 @@ +--- +'@graphql-hive/client': patch +--- + +Avoid marking the same type as used twice diff --git a/packages/libraries/client/src/internal/usage.ts b/packages/libraries/client/src/internal/usage.ts index d8af14e333..bc744c3f2e 100644 --- a/packages/libraries/client/src/internal/usage.ts +++ b/packages/libraries/client/src/internal/usage.ts @@ -195,6 +195,7 @@ export function createCollector({ schema, max, ttl }: { schema: GraphQLSchema; m function collect(doc: DocumentNode): CacheResult { const entries = new Set(); + const collected_entire_named_types = new Set(); function markAsUsed(id: string) { if (!entries.has(id)) { @@ -244,6 +245,15 @@ export function createCollector({ schema, max, ttl }: { schema: GraphQLSchema; m function markEntireTypeAsUsed(type: GraphQLInputType): void { const namedType = unwrapType(type); + if (collected_entire_named_types.has(namedType.name)) { + // No need to mark this type as used again + return; + } else { + // Add this type to the set of types that have been marked as used + // to avoid infinite loops + collected_entire_named_types.add(namedType.name); + } + if (isScalarType(namedType)) { markAsUsed(makeId(namedType.name)); return;