From fb9b624ab80ff39658c9ecd45b55d10d906e15e7 Mon Sep 17 00:00:00 2001 From: Dmitry Til Date: Tue, 25 Oct 2022 16:27:26 +0200 Subject: [PATCH] (processVariables: true) do not collect input if variable is missing (#456) --- .changeset/slow-pets-scream.md | 5 ++++ .../libraries/client/src/internal/usage.ts | 4 +-- .../client/tests/usage-collector.spec.ts | 26 +++++++++++++++++++ .../src/components/target/explorer/common.tsx | 5 ++++ .../target/explorer/input-object-type.tsx | 8 +++++- 5 files changed, 45 insertions(+), 3 deletions(-) create mode 100644 .changeset/slow-pets-scream.md diff --git a/.changeset/slow-pets-scream.md b/.changeset/slow-pets-scream.md new file mode 100644 index 000000000..380cae895 --- /dev/null +++ b/.changeset/slow-pets-scream.md @@ -0,0 +1,5 @@ +--- +'@graphql-hive/client': patch +--- + +(processVariables: true) do not collect input when corresponding variable is missing diff --git a/packages/libraries/client/src/internal/usage.ts b/packages/libraries/client/src/internal/usage.ts index 7ea459aff..162c852ca 100644 --- a/packages/libraries/client/src/internal/usage.ts +++ b/packages/libraries/client/src/internal/usage.ts @@ -318,8 +318,8 @@ export function createCollector({ } } } else { - // Collect the entire type - collectInputType(namedType.name); + // Collect type without fields + markAsUsed(makeId(namedType.name)); } }); } else { diff --git a/packages/libraries/client/tests/usage-collector.spec.ts b/packages/libraries/client/tests/usage-collector.spec.ts index f688ada2f..2f414225d 100644 --- a/packages/libraries/client/tests/usage-collector.spec.ts +++ b/packages/libraries/client/tests/usage-collector.spec.ts @@ -336,6 +336,32 @@ test('(processVariables: true) collect used-only input fields', async () => { expect(info.fields).not.toContain(`PaginationInput.offset`); }); +test('(processVariables: true) should collect input object without fields when corresponding variable is not provided', async () => { + const collect = createCollector({ + schema, + max: 1, + processVariables: true, + }); + const info = collect( + parse(/* GraphQL */ ` + query getProjects($pagination: PaginationInput, $type: ProjectType!) { + projects(filter: { pagination: $pagination, type: $type }) { + id + } + } + `), + { + type: 'STITCHING', + } + ).value; + + expect(info.fields).toContain(`FilterInput.type`); + expect(info.fields).toContain(`FilterInput.pagination`); + expect(info.fields).toContain(`PaginationInput`); + expect(info.fields).not.toContain(`PaginationInput.limit`); + expect(info.fields).not.toContain(`PaginationInput.offset`); +}); + test('(processVariables: true) collect used-only input type fields from an array', async () => { const collect = createCollector({ schema, diff --git a/packages/web/app/src/components/target/explorer/common.tsx b/packages/web/app/src/components/target/explorer/common.tsx index dd20fbd60..d4b21c296 100644 --- a/packages/web/app/src/components/target/explorer/common.tsx +++ b/packages/web/app/src/components/target/explorer/common.tsx @@ -134,6 +134,8 @@ export function GraphQLTypeCard( name: string; description?: string | null; implements?: string[]; + totalRequests?: number; + usage?: DocumentType; }> ) { return ( @@ -156,6 +158,9 @@ export function GraphQLTypeCard( ) : null} + {props.usage && typeof props.totalRequests !== 'undefined' ? ( + + ) : null}
{props.children}
diff --git a/packages/web/app/src/components/target/explorer/input-object-type.tsx b/packages/web/app/src/components/target/explorer/input-object-type.tsx index 9522c5f95..55ddbf691 100644 --- a/packages/web/app/src/components/target/explorer/input-object-type.tsx +++ b/packages/web/app/src/components/target/explorer/input-object-type.tsx @@ -19,7 +19,13 @@ export function GraphQLInputObjectTypeComponent(props: { totalRequests: number; }) { return ( - + );