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

Handle empty selection array (#46)

authored by Jovi De Croock and committed by GitHub 405d23c2 47ea6137

Changed files
+45 -1
.changeset
src
+5
.changeset/soft-penguins-switch.md
··· 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
+37
src/__tests__/printer.test.ts
··· 1 1 import { describe, it, expect } from 'vitest'; 2 2 import * as graphql16 from 'graphql16'; 3 3 4 + import type { DocumentNode } from '../ast'; 4 5 import { parse } from '../parser'; 5 6 import { print, printString, printBlockString } from '../printer'; 6 7 import kitchenSinkAST from './fixtures/kitchen_sink.json'; 8 + import { Kind, OperationTypeNode } from 'src/kind'; 7 9 8 10 function dedentString(string: string) { 9 11 const trimmedStr = string ··· 176 178 dateTime 177 179 } 178 180 } 181 + ` 182 + ); 183 + }); 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 + } 179 216 ` 180 217 ); 181 218 });
+3 -1
src/printer.ts
··· 85 85 } 86 86 if (node.directives && node.directives.length) 87 87 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 + } 89 91 return out; 90 92 }, 91 93 StringValue(node: StringValueNode): string {