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

Handle empty selection array #46

Merged
merged 3 commits into from
Dec 7, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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/soft-penguins-switch.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@0no-co/graphql.web': patch
---

Handle selection-sets with an empty selections array in a `documentNode`.
JoviDeCroock marked this conversation as resolved.
Show resolved Hide resolved
37 changes: 37 additions & 0 deletions src/__tests__/printer.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { describe, it, expect } from 'vitest';
import * as graphql16 from 'graphql16';

import type { DocumentNode } from '../ast';
import { parse } from '../parser';
import { print, printString, printBlockString } from '../printer';
import kitchenSinkAST from './fixtures/kitchen_sink.json';
import { Kind, OperationTypeNode } from 'src/kind';

function dedentString(string: string) {
const trimmedStr = string
Expand Down Expand Up @@ -179,4 +181,39 @@ describe('print', () => {
`
);
});

it('Handles empty array selections', () => {
const document: DocumentNode = {
kind: Kind.DOCUMENT,
definitions: [
{
kind: Kind.OPERATION_DEFINITION,
operation: OperationTypeNode.QUERY,
name: undefined,
selectionSet: {
kind: Kind.SELECTION_SET,
selections: [
{
kind: Kind.FIELD,
name: { kind: Kind.NAME, value: 'id' },
alias: undefined,
arguments: [],
directives: [],
selectionSet: { kind: Kind.SELECTION_SET, selections: [] },
},
],
},
variableDefinitions: [],
},
],
};

expect(print(document)).toBe(
dedent`
{
id
}
`
);
});
});
4 changes: 3 additions & 1 deletion src/printer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,9 @@ const nodes = {
}
if (node.directives && node.directives.length)
out += ' ' + mapJoin(node.directives, ' ', nodes.Directive);
if (node.selectionSet) out += ' ' + nodes.SelectionSet(node.selectionSet);
if (node.selectionSet && node.selectionSet.selections.length) {
out += ' ' + nodes.SelectionSet(node.selectionSet);
}
return out;
},
StringValue(node: StringValueNode): string {
Expand Down
Loading