Skip to content

Commit

Permalink
Handle empty selection array (#46)
Browse files Browse the repository at this point in the history
  • Loading branch information
JoviDeCroock authored Dec 7, 2024
1 parent 47ea613 commit 405d23c
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 1 deletion.
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
---

Fix printing when a manually created AST node with an empty selection set array is passed to the printer
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

0 comments on commit 405d23c

Please sign in to comment.