Mirror: The spec-compliant minimum of client-side GraphQL.

Handle empty selection array (#46)

Changed files
+45 -1
.changeset
src
+5
.changeset/soft-penguins-switch.md
···
+
---
+
'@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
src/__tests__/printer.test.ts
···
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
···
dateTime
}
}
+
`
+
);
+
});
+
+
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
+
}
`
);
});
+3 -1
src/printer.ts
···
}
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 {