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

refactor: Improve `print` performance (#24)

* Fix incorrect AST alias

* Update printer with new indent logic

* Simplify OperationDefinition printer

* Replace selection set generic printers

* Fix unrelated bench name

* Replace map + join

* Remove variableDefinitions that were accidentally copied over

* Add changeset

Changed files
+133 -78
.changeset
src
+5
.changeset/three-buttons-eat.md
···
···
+
---
+
'@0no-co/graphql.web': patch
+
---
+
+
Improve printer performance.
+1 -1
src/__tests__/visitor.bench.ts
···
import kitchenSinkAST from './fixtures/kitchen_sink.json';
import { visit } from '../visitor';
-
describe('print (kitchen sink AST)', () => {
bench('@0no-co/graphql.web', () => {
visit(kitchenSinkAST, {
Field: formatNode,
···
import kitchenSinkAST from './fixtures/kitchen_sink.json';
import { visit } from '../visitor';
+
describe('visit (kitchen sink AST)', () => {
bench('@0no-co/graphql.web', () => {
visit(kitchenSinkAST, {
Field: formatNode,
+1 -1
src/ast.ts
···
>;
export type StringValueNode = Or<
-
GraphQL.FloatValueNode,
{
readonly kind: Kind.STRING;
readonly value: string;
···
>;
export type StringValueNode = Or<
+
GraphQL.StringValueNode,
{
readonly kind: Kind.STRING;
readonly value: string;
+126 -76
src/printer.ts
···
-
import type { ASTNode } from './ast';
-
export function printString(string: string) {
return JSON.stringify(string);
}
-
export function printBlockString(string: string) {
return '"""\n' + string.replace(/"""/g, '\\"""') + '\n"""';
}
-
const hasItems = <T>(array: ReadonlyArray<T> | undefined | null): array is ReadonlyArray<T> =>
-
!!(array && array.length);
-
const MAX_LINE_LENGTH = 80;
-
const nodes: {
-
[NodeT in ASTNode as NodeT['kind']]?: (node: NodeT) => string;
-
} = {
-
OperationDefinition(node) {
-
if (
-
node.operation === 'query' &&
-
!node.name &&
-
!hasItems(node.variableDefinitions) &&
-
!hasItems(node.directives)
-
) {
-
return nodes.SelectionSet!(node.selectionSet);
-
}
let out: string = node.operation;
if (node.name) out += ' ' + node.name.value;
-
if (hasItems(node.variableDefinitions)) {
if (!node.name) out += ' ';
-
out += '(' + node.variableDefinitions.map(nodes.VariableDefinition!).join(', ') + ')';
}
-
if (hasItems(node.directives)) out += ' ' + node.directives.map(nodes.Directive!).join(' ');
-
return out + ' ' + nodes.SelectionSet!(node.selectionSet);
},
-
VariableDefinition(node) {
-
let out = nodes.Variable!(node.variable) + ': ' + print(node.type);
-
if (node.defaultValue) out += ' = ' + print(node.defaultValue);
-
if (hasItems(node.directives)) out += ' ' + node.directives.map(nodes.Directive!).join(' ');
return out;
},
-
Field(node) {
-
let out = (node.alias ? node.alias.value + ': ' : '') + node.name.value;
-
if (hasItems(node.arguments)) {
-
const args = node.arguments.map(nodes.Argument!);
-
const argsLine = out + '(' + args.join(', ') + ')';
-
out =
-
argsLine.length > MAX_LINE_LENGTH
-
? out + '(\n ' + args.join('\n').replace(/\n/g, '\n ') + '\n)'
-
: argsLine;
}
-
if (hasItems(node.directives)) out += ' ' + node.directives.map(nodes.Directive!).join(' ');
-
return node.selectionSet ? out + ' ' + nodes.SelectionSet!(node.selectionSet) : out;
},
-
StringValue(node) {
-
return node.block ? printBlockString(node.value) : printString(node.value);
},
-
BooleanValue(node) {
return '' + node.value;
},
-
NullValue(_node) {
return 'null';
},
-
IntValue(node) {
return node.value;
},
-
FloatValue(node) {
return node.value;
},
-
EnumValue(node) {
return node.value;
},
-
Name(node) {
return node.value;
},
-
Variable(node) {
return '$' + node.name.value;
},
-
ListValue(node) {
-
return '[' + node.values.map(print).join(', ') + ']';
},
-
ObjectValue(node) {
-
return '{' + node.fields.map(nodes.ObjectField!).join(', ') + '}';
},
-
ObjectField(node) {
-
return node.name.value + ': ' + print(node.value);
},
-
Document(node) {
-
return hasItems(node.definitions) ? node.definitions.map(print).join('\n\n') : '';
},
-
SelectionSet(node) {
-
return '{\n ' + node.selections.map(print).join('\n').replace(/\n/g, '\n ') + '\n}';
},
-
Argument(node) {
-
return node.name.value + ': ' + print(node.value);
},
-
FragmentSpread(node) {
let out = '...' + node.name.value;
-
if (hasItems(node.directives)) out += ' ' + node.directives.map(nodes.Directive!).join(' ');
return out;
},
-
InlineFragment(node) {
let out = '...';
if (node.typeCondition) out += ' on ' + node.typeCondition.name.value;
-
if (hasItems(node.directives)) out += ' ' + node.directives.map(nodes.Directive!).join(' ');
-
return out + ' ' + print(node.selectionSet);
},
-
FragmentDefinition(node) {
let out = 'fragment ' + node.name.value;
out += ' on ' + node.typeCondition.name.value;
-
if (hasItems(node.directives)) out += ' ' + node.directives.map(nodes.Directive!).join(' ');
-
return out + ' ' + print(node.selectionSet);
},
-
Directive(node) {
let out = '@' + node.name.value;
-
if (hasItems(node.arguments)) out += '(' + node.arguments.map(nodes.Argument!).join(', ') + ')';
return out;
},
-
NamedType(node) {
return node.name.value;
},
-
ListType(node) {
-
return '[' + print(node.type) + ']';
},
-
NonNullType(node) {
-
return print(node.type) + '!';
},
-
};
-
export function print(node: ASTNode): string {
-
return nodes[node.kind] ? (nodes as any)[node.kind]!(node) : '';
}
···
+
import type {
+
ASTNode,
+
NameNode,
+
DocumentNode,
+
VariableNode,
+
SelectionSetNode,
+
FieldNode,
+
ArgumentNode,
+
FragmentSpreadNode,
+
InlineFragmentNode,
+
VariableDefinitionNode,
+
OperationDefinitionNode,
+
FragmentDefinitionNode,
+
IntValueNode,
+
FloatValueNode,
+
StringValueNode,
+
BooleanValueNode,
+
NullValueNode,
+
EnumValueNode,
+
ListValueNode,
+
ObjectValueNode,
+
ObjectFieldNode,
+
DirectiveNode,
+
NamedTypeNode,
+
ListTypeNode,
+
NonNullTypeNode,
+
} from './ast';
+
+
function mapJoin<T>(value: readonly T[], joiner: string, mapper: (value: T) => string): string {
+
let out = '';
+
for (let index = 0; index < value.length; index++) {
+
if (index) out += joiner;
+
out += mapper(value[index]);
+
}
+
return out;
+
}
+
function printString(string: string) {
return JSON.stringify(string);
}
+
function printBlockString(string: string) {
return '"""\n' + string.replace(/"""/g, '\\"""') + '\n"""';
}
const MAX_LINE_LENGTH = 80;
+
let LF = '\n';
+
+
const nodes = {
+
OperationDefinition(node: OperationDefinitionNode): string {
let out: string = node.operation;
if (node.name) out += ' ' + node.name.value;
+
if (node.variableDefinitions && node.variableDefinitions.length) {
if (!node.name) out += ' ';
+
out += '(' + mapJoin(node.variableDefinitions, ', ', nodes.VariableDefinition) + ')';
}
+
if (node.directives && node.directives.length)
+
out += ' ' + mapJoin(node.directives, ' ', nodes.Directive);
+
return out !== 'query'
+
? out + ' ' + nodes.SelectionSet(node.selectionSet)
+
: nodes.SelectionSet(node.selectionSet);
},
+
VariableDefinition(node: VariableDefinitionNode): string {
+
let out = nodes.Variable!(node.variable) + ': ' + _print(node.type);
+
if (node.defaultValue) out += ' = ' + _print(node.defaultValue);
+
if (node.directives && node.directives.length)
+
out += ' ' + mapJoin(node.directives, ' ', nodes.Directive);
return out;
},
+
Field(node: FieldNode): string {
+
let out = node.alias ? node.alias.value + ': ' + node.name.value : node.name.value;
+
if (node.arguments && node.arguments.length) {
+
const args = mapJoin(node.arguments, ', ', nodes.Argument);
+
if (out.length + args.length + 2 > MAX_LINE_LENGTH) {
+
out +=
+
'(' +
+
(LF += ' ') +
+
mapJoin(node.arguments, LF, nodes.Argument) +
+
(LF = LF.slice(0, -2)) +
+
')';
+
} else {
+
out += '(' + args + ')';
+
}
}
+
if (node.directives && node.directives.length)
+
out += ' ' + mapJoin(node.directives, ' ', nodes.Directive);
+
if (node.selectionSet) out += ' ' + nodes.SelectionSet(node.selectionSet);
+
return out;
},
+
StringValue(node: StringValueNode): string {
+
if (node.block) {
+
return printBlockString(node.value).replace(/\n/g, LF);
+
} else {
+
return printString(node.value);
+
}
},
+
BooleanValue(node: BooleanValueNode): string {
return '' + node.value;
},
+
NullValue(_node: NullValueNode): string {
return 'null';
},
+
IntValue(node: IntValueNode): string {
return node.value;
},
+
FloatValue(node: FloatValueNode): string {
return node.value;
},
+
EnumValue(node: EnumValueNode): string {
return node.value;
},
+
Name(node: NameNode): string {
return node.value;
},
+
Variable(node: VariableNode): string {
return '$' + node.name.value;
},
+
ListValue(node: ListValueNode): string {
+
return '[' + mapJoin(node.values, ', ', _print) + ']';
},
+
ObjectValue(node: ObjectValueNode): string {
+
return '{' + mapJoin(node.fields, ', ', nodes.ObjectField) + '}';
},
+
ObjectField(node: ObjectFieldNode): string {
+
return node.name.value + ': ' + _print(node.value);
},
+
Document(node: DocumentNode): string {
+
if (!node.definitions || !node.definitions.length) return '';
+
return mapJoin(node.definitions, '\n\n', _print);
},
+
SelectionSet(node: SelectionSetNode): string {
+
return '{' + (LF += ' ') + mapJoin(node.selections, LF, _print) + (LF = LF.slice(0, -2)) + '}';
},
+
Argument(node: ArgumentNode): string {
+
return node.name.value + ': ' + _print(node.value);
},
+
FragmentSpread(node: FragmentSpreadNode): string {
let out = '...' + node.name.value;
+
if (node.directives && node.directives.length)
+
out += ' ' + mapJoin(node.directives, ' ', nodes.Directive);
return out;
},
+
InlineFragment(node: InlineFragmentNode): string {
let out = '...';
if (node.typeCondition) out += ' on ' + node.typeCondition.name.value;
+
if (node.directives && node.directives.length)
+
out += ' ' + mapJoin(node.directives, ' ', nodes.Directive);
+
out += ' ' + nodes.SelectionSet(node.selectionSet);
+
return out;
},
+
FragmentDefinition(node: FragmentDefinitionNode): string {
let out = 'fragment ' + node.name.value;
out += ' on ' + node.typeCondition.name.value;
+
if (node.directives && node.directives.length)
+
out += ' ' + mapJoin(node.directives, ' ', nodes.Directive);
+
return out + ' ' + nodes.SelectionSet(node.selectionSet);
},
+
Directive(node: DirectiveNode): string {
let out = '@' + node.name.value;
+
if (node.arguments && node.arguments.length)
+
out += '(' + mapJoin(node.arguments, ', ', nodes.Argument) + ')';
return out;
},
+
NamedType(node: NamedTypeNode): string {
return node.name.value;
},
+
ListType(node: ListTypeNode): string {
+
return '[' + _print(node.type) + ']';
},
+
NonNullType(node: NonNullTypeNode): string {
+
return _print(node.type) + '!';
},
+
} as const;
+
const _print = (node: ASTNode): string => nodes[node.kind](node);
+
+
function print(node: ASTNode): string {
+
LF = '\n';
+
return nodes[node.kind] ? nodes[node.kind](node) : '';
}
+
+
export { print, printString, printBlockString };