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

(processVariables: true) do not collect input if variable is missing #456

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
5 changes: 5 additions & 0 deletions .changeset/slow-pets-scream.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@graphql-hive/client': patch
---

(processVariables: true) do not collect input when corresponding variable is missing
4 changes: 2 additions & 2 deletions packages/libraries/client/src/internal/usage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -318,8 +318,8 @@ export function createCollector({
}
}
} else {
// Collect the entire type
collectInputType(namedType.name);
// Collect type without fields
markAsUsed(makeId(namedType.name));
}
});
} else {
Expand Down
26 changes: 26 additions & 0 deletions packages/libraries/client/tests/usage-collector.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
5 changes: 5 additions & 0 deletions packages/web/app/src/components/target/explorer/common.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,8 @@ export function GraphQLTypeCard(
name: string;
description?: string | null;
implements?: string[];
totalRequests?: number;
usage?: DocumentType<typeof SchemaExplorerUsageStats_UsageFragment>;
}>
) {
return (
Expand All @@ -156,6 +158,9 @@ export function GraphQLTypeCard(
</div>
</div>
) : null}
{props.usage && typeof props.totalRequests !== 'undefined' ? (
<SchemaExplorerUsageStats totalRequests={props.totalRequests} usage={props.usage} />
) : null}
</div>
<div>{props.children}</div>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,13 @@ export function GraphQLInputObjectTypeComponent(props: {
totalRequests: number;
}) {
return (
<GraphQLTypeCard kind="input" name={props.type.name} description={props.type.description}>
<GraphQLTypeCard
kind="input"
name={props.type.name}
description={props.type.description}
totalRequests={props.totalRequests}
usage={props.type.usage}
>
<GraphQLInputFields fields={props.type.fields} totalRequests={props.totalRequests} />
</GraphQLTypeCard>
);
Expand Down