Skip to content

Commit 405d23c

Browse files
authored
Handle empty selection array (#46)
1 parent 47ea613 commit 405d23c

File tree

3 files changed

+45
-1
lines changed

3 files changed

+45
-1
lines changed

.changeset/soft-penguins-switch.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@0no-co/graphql.web': patch
3+
---
4+
5+
Fix printing when a manually created AST node with an empty selection set array is passed to the printer

src/__tests__/printer.test.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import { describe, it, expect } from 'vitest';
22
import * as graphql16 from 'graphql16';
33

4+
import type { DocumentNode } from '../ast';
45
import { parse } from '../parser';
56
import { print, printString, printBlockString } from '../printer';
67
import kitchenSinkAST from './fixtures/kitchen_sink.json';
8+
import { Kind, OperationTypeNode } from 'src/kind';
79

810
function dedentString(string: string) {
911
const trimmedStr = string
@@ -179,4 +181,39 @@ describe('print', () => {
179181
`
180182
);
181183
});
184+
185+
it('Handles empty array selections', () => {
186+
const document: DocumentNode = {
187+
kind: Kind.DOCUMENT,
188+
definitions: [
189+
{
190+
kind: Kind.OPERATION_DEFINITION,
191+
operation: OperationTypeNode.QUERY,
192+
name: undefined,
193+
selectionSet: {
194+
kind: Kind.SELECTION_SET,
195+
selections: [
196+
{
197+
kind: Kind.FIELD,
198+
name: { kind: Kind.NAME, value: 'id' },
199+
alias: undefined,
200+
arguments: [],
201+
directives: [],
202+
selectionSet: { kind: Kind.SELECTION_SET, selections: [] },
203+
},
204+
],
205+
},
206+
variableDefinitions: [],
207+
},
208+
],
209+
};
210+
211+
expect(print(document)).toBe(
212+
dedent`
213+
{
214+
id
215+
}
216+
`
217+
);
218+
});
182219
});

src/printer.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,9 @@ const nodes = {
8585
}
8686
if (node.directives && node.directives.length)
8787
out += ' ' + mapJoin(node.directives, ' ', nodes.Directive);
88-
if (node.selectionSet) out += ' ' + nodes.SelectionSet(node.selectionSet);
88+
if (node.selectionSet && node.selectionSet.selections.length) {
89+
out += ' ' + nodes.SelectionSet(node.selectionSet);
90+
}
8991
return out;
9092
},
9193
StringValue(node: StringValueNode): string {

0 commit comments

Comments
 (0)