new Deps
This commit is contained in:
+73
-54
@@ -28,6 +28,7 @@ use GraphQL\Language\AST\IntValueNode;
|
||||
use GraphQL\Language\AST\ListTypeNode;
|
||||
use GraphQL\Language\AST\ListValueNode;
|
||||
use GraphQL\Language\AST\NamedTypeNode;
|
||||
use GraphQL\Language\AST\NameNode;
|
||||
use GraphQL\Language\AST\Node;
|
||||
use GraphQL\Language\AST\NodeKind;
|
||||
use GraphQL\Language\AST\NonNullTypeNode;
|
||||
@@ -47,6 +48,7 @@ use GraphQL\Language\AST\StringValueNode;
|
||||
use GraphQL\Language\AST\UnionTypeDefinitionNode;
|
||||
use GraphQL\Language\AST\UnionTypeExtensionNode;
|
||||
use GraphQL\Language\AST\VariableDefinitionNode;
|
||||
use GraphQL\Language\AST\VariableNode;
|
||||
use GraphQL\Utils\Utils;
|
||||
use function count;
|
||||
use function implode;
|
||||
@@ -54,6 +56,7 @@ use function json_encode;
|
||||
use function preg_replace;
|
||||
use function sprintf;
|
||||
use function str_replace;
|
||||
use function strlen;
|
||||
use function strpos;
|
||||
|
||||
/**
|
||||
@@ -82,7 +85,7 @@ class Printer
|
||||
public static function doPrint($ast)
|
||||
{
|
||||
static $instance;
|
||||
$instance = $instance ?: new static();
|
||||
$instance = $instance ?? new static();
|
||||
|
||||
return $instance->printAST($ast);
|
||||
}
|
||||
@@ -91,25 +94,31 @@ class Printer
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* Traverse an AST bottom-up, converting all nodes to strings.
|
||||
*
|
||||
* That means the AST is manipulated in such a way that it no longer
|
||||
* resembles the well-formed result of parsing.
|
||||
*/
|
||||
public function printAST($ast)
|
||||
{
|
||||
return Visitor::visit(
|
||||
$ast,
|
||||
[
|
||||
'leave' => [
|
||||
NodeKind::NAME => static function (Node $node) {
|
||||
return '' . $node->value;
|
||||
NodeKind::NAME => static function (NameNode $node) : string {
|
||||
return $node->value;
|
||||
},
|
||||
|
||||
NodeKind::VARIABLE => static function ($node) {
|
||||
NodeKind::VARIABLE => static function (VariableNode $node) : string {
|
||||
return '$' . $node->name;
|
||||
},
|
||||
|
||||
NodeKind::DOCUMENT => function (DocumentNode $node) {
|
||||
NodeKind::DOCUMENT => function (DocumentNode $node) : string {
|
||||
return $this->join($node->definitions, "\n\n") . "\n";
|
||||
},
|
||||
|
||||
NodeKind::OPERATION_DEFINITION => function (OperationDefinitionNode $node) {
|
||||
NodeKind::OPERATION_DEFINITION => function (OperationDefinitionNode $node) : string {
|
||||
$op = $node->operation;
|
||||
$name = $node->name;
|
||||
$varDefs = $this->wrap('(', $this->join($node->variableDefinitions, ', '), ')');
|
||||
@@ -118,20 +127,24 @@ class Printer
|
||||
|
||||
// Anonymous queries with no directives or variable definitions can use
|
||||
// the query short form.
|
||||
return ! $name && ! $directives && ! $varDefs && $op === 'query'
|
||||
return $name === null && strlen($directives ?? '') === 0 && ! $varDefs && $op === 'query'
|
||||
? $selectionSet
|
||||
: $this->join([$op, $this->join([$name, $varDefs]), $directives, $selectionSet], ' ');
|
||||
},
|
||||
|
||||
NodeKind::VARIABLE_DEFINITION => function (VariableDefinitionNode $node) {
|
||||
return $node->variable . ': ' . $node->type . $this->wrap(' = ', $node->defaultValue);
|
||||
NodeKind::VARIABLE_DEFINITION => function (VariableDefinitionNode $node) : string {
|
||||
return $node->variable
|
||||
. ': '
|
||||
. $node->type
|
||||
. $this->wrap(' = ', $node->defaultValue)
|
||||
. $this->wrap(' ', $this->join($node->directives, ' '));
|
||||
},
|
||||
|
||||
NodeKind::SELECTION_SET => function (SelectionSetNode $node) {
|
||||
return $this->block($node->selections);
|
||||
},
|
||||
|
||||
NodeKind::FIELD => function (FieldNode $node) {
|
||||
NodeKind::FIELD => function (FieldNode $node) : string {
|
||||
return $this->join(
|
||||
[
|
||||
$this->wrap('', $node->alias, ': ') . $node->name . $this->wrap(
|
||||
@@ -146,15 +159,17 @@ class Printer
|
||||
);
|
||||
},
|
||||
|
||||
NodeKind::ARGUMENT => static function (ArgumentNode $node) {
|
||||
NodeKind::ARGUMENT => static function (ArgumentNode $node) : string {
|
||||
return $node->name . ': ' . $node->value;
|
||||
},
|
||||
|
||||
NodeKind::FRAGMENT_SPREAD => function (FragmentSpreadNode $node) {
|
||||
return '...' . $node->name . $this->wrap(' ', $this->join($node->directives, ' '));
|
||||
NodeKind::FRAGMENT_SPREAD => function (FragmentSpreadNode $node) : string {
|
||||
return '...'
|
||||
. $node->name
|
||||
. $this->wrap(' ', $this->join($node->directives, ' '));
|
||||
},
|
||||
|
||||
NodeKind::INLINE_FRAGMENT => function (InlineFragmentNode $node) {
|
||||
NodeKind::INLINE_FRAGMENT => function (InlineFragmentNode $node) : string {
|
||||
return $this->join(
|
||||
[
|
||||
'...',
|
||||
@@ -166,7 +181,7 @@ class Printer
|
||||
);
|
||||
},
|
||||
|
||||
NodeKind::FRAGMENT_DEFINITION => function (FragmentDefinitionNode $node) {
|
||||
NodeKind::FRAGMENT_DEFINITION => function (FragmentDefinitionNode $node) : string {
|
||||
// Note: fragment variable definitions are experimental and may be changed or removed in the future.
|
||||
return sprintf('fragment %s', $node->name)
|
||||
. $this->wrap('(', $this->join($node->variableDefinitions, ', '), ')')
|
||||
@@ -175,15 +190,15 @@ class Printer
|
||||
. $node->selectionSet;
|
||||
},
|
||||
|
||||
NodeKind::INT => static function (IntValueNode $node) {
|
||||
NodeKind::INT => static function (IntValueNode $node) : string {
|
||||
return $node->value;
|
||||
},
|
||||
|
||||
NodeKind::FLOAT => static function (FloatValueNode $node) {
|
||||
NodeKind::FLOAT => static function (FloatValueNode $node) : string {
|
||||
return $node->value;
|
||||
},
|
||||
|
||||
NodeKind::STRING => function (StringValueNode $node, $key) {
|
||||
NodeKind::STRING => function (StringValueNode $node, $key) : string {
|
||||
if ($node->block) {
|
||||
return $this->printBlockString($node->value, $key === 'description');
|
||||
}
|
||||
@@ -191,47 +206,48 @@ class Printer
|
||||
return json_encode($node->value);
|
||||
},
|
||||
|
||||
NodeKind::BOOLEAN => static function (BooleanValueNode $node) {
|
||||
NodeKind::BOOLEAN => static function (BooleanValueNode $node) : string {
|
||||
return $node->value ? 'true' : 'false';
|
||||
},
|
||||
|
||||
NodeKind::NULL => static function (NullValueNode $node) {
|
||||
NodeKind::NULL => static function (NullValueNode $node) : string {
|
||||
return 'null';
|
||||
},
|
||||
|
||||
NodeKind::ENUM => static function (EnumValueNode $node) {
|
||||
NodeKind::ENUM => static function (EnumValueNode $node) : string {
|
||||
return $node->value;
|
||||
},
|
||||
|
||||
NodeKind::LST => function (ListValueNode $node) {
|
||||
NodeKind::LST => function (ListValueNode $node) : string {
|
||||
return '[' . $this->join($node->values, ', ') . ']';
|
||||
},
|
||||
|
||||
NodeKind::OBJECT => function (ObjectValueNode $node) {
|
||||
NodeKind::OBJECT => function (ObjectValueNode $node) : string {
|
||||
return '{' . $this->join($node->fields, ', ') . '}';
|
||||
},
|
||||
|
||||
NodeKind::OBJECT_FIELD => static function (ObjectFieldNode $node) {
|
||||
NodeKind::OBJECT_FIELD => static function (ObjectFieldNode $node) : string {
|
||||
return $node->name . ': ' . $node->value;
|
||||
},
|
||||
|
||||
NodeKind::DIRECTIVE => function (DirectiveNode $node) {
|
||||
NodeKind::DIRECTIVE => function (DirectiveNode $node) : string {
|
||||
return '@' . $node->name . $this->wrap('(', $this->join($node->arguments, ', '), ')');
|
||||
},
|
||||
|
||||
NodeKind::NAMED_TYPE => static function (NamedTypeNode $node) {
|
||||
NodeKind::NAMED_TYPE => static function (NamedTypeNode $node) : string {
|
||||
// @phpstan-ignore-next-line the printer works bottom up, so this is already a string here
|
||||
return $node->name;
|
||||
},
|
||||
|
||||
NodeKind::LIST_TYPE => static function (ListTypeNode $node) {
|
||||
NodeKind::LIST_TYPE => static function (ListTypeNode $node) : string {
|
||||
return '[' . $node->type . ']';
|
||||
},
|
||||
|
||||
NodeKind::NON_NULL_TYPE => static function (NonNullTypeNode $node) {
|
||||
NodeKind::NON_NULL_TYPE => static function (NonNullTypeNode $node) : string {
|
||||
return $node->type . '!';
|
||||
},
|
||||
|
||||
NodeKind::SCHEMA_DEFINITION => function (SchemaDefinitionNode $def) {
|
||||
NodeKind::SCHEMA_DEFINITION => function (SchemaDefinitionNode $def) : string {
|
||||
return $this->join(
|
||||
[
|
||||
'schema',
|
||||
@@ -242,15 +258,15 @@ class Printer
|
||||
);
|
||||
},
|
||||
|
||||
NodeKind::OPERATION_TYPE_DEFINITION => static function (OperationTypeDefinitionNode $def) {
|
||||
NodeKind::OPERATION_TYPE_DEFINITION => static function (OperationTypeDefinitionNode $def) : string {
|
||||
return $def->operation . ': ' . $def->type;
|
||||
},
|
||||
|
||||
NodeKind::SCALAR_TYPE_DEFINITION => $this->addDescription(function (ScalarTypeDefinitionNode $def) {
|
||||
NodeKind::SCALAR_TYPE_DEFINITION => $this->addDescription(function (ScalarTypeDefinitionNode $def) : string {
|
||||
return $this->join(['scalar', $def->name, $this->join($def->directives, ' ')], ' ');
|
||||
}),
|
||||
|
||||
NodeKind::OBJECT_TYPE_DEFINITION => $this->addDescription(function (ObjectTypeDefinitionNode $def) {
|
||||
NodeKind::OBJECT_TYPE_DEFINITION => $this->addDescription(function (ObjectTypeDefinitionNode $def) : string {
|
||||
return $this->join(
|
||||
[
|
||||
'type',
|
||||
@@ -263,8 +279,8 @@ class Printer
|
||||
);
|
||||
}),
|
||||
|
||||
NodeKind::FIELD_DEFINITION => $this->addDescription(function (FieldDefinitionNode $def) {
|
||||
$noIndent = Utils::every($def->arguments, static function (string $arg) {
|
||||
NodeKind::FIELD_DEFINITION => $this->addDescription(function (FieldDefinitionNode $def) : string {
|
||||
$noIndent = Utils::every($def->arguments, static function (string $arg) : bool {
|
||||
return strpos($arg, "\n") === false;
|
||||
});
|
||||
|
||||
@@ -276,7 +292,7 @@ class Printer
|
||||
. $this->wrap(' ', $this->join($def->directives, ' '));
|
||||
}),
|
||||
|
||||
NodeKind::INPUT_VALUE_DEFINITION => $this->addDescription(function (InputValueDefinitionNode $def) {
|
||||
NodeKind::INPUT_VALUE_DEFINITION => $this->addDescription(function (InputValueDefinitionNode $def) : string {
|
||||
return $this->join(
|
||||
[
|
||||
$def->name . ': ' . $def->type,
|
||||
@@ -288,11 +304,12 @@ class Printer
|
||||
}),
|
||||
|
||||
NodeKind::INTERFACE_TYPE_DEFINITION => $this->addDescription(
|
||||
function (InterfaceTypeDefinitionNode $def) {
|
||||
function (InterfaceTypeDefinitionNode $def) : string {
|
||||
return $this->join(
|
||||
[
|
||||
'interface',
|
||||
$def->name,
|
||||
$this->wrap('implements ', $this->join($def->interfaces, ' & ')),
|
||||
$this->join($def->directives, ' '),
|
||||
$this->block($def->fields),
|
||||
],
|
||||
@@ -301,13 +318,13 @@ class Printer
|
||||
}
|
||||
),
|
||||
|
||||
NodeKind::UNION_TYPE_DEFINITION => $this->addDescription(function (UnionTypeDefinitionNode $def) {
|
||||
NodeKind::UNION_TYPE_DEFINITION => $this->addDescription(function (UnionTypeDefinitionNode $def) : string {
|
||||
return $this->join(
|
||||
[
|
||||
'union',
|
||||
$def->name,
|
||||
$this->join($def->directives, ' '),
|
||||
$def->types
|
||||
count($def->types ?? []) > 0
|
||||
? '= ' . $this->join($def->types, ' | ')
|
||||
: '',
|
||||
],
|
||||
@@ -315,7 +332,7 @@ class Printer
|
||||
);
|
||||
}),
|
||||
|
||||
NodeKind::ENUM_TYPE_DEFINITION => $this->addDescription(function (EnumTypeDefinitionNode $def) {
|
||||
NodeKind::ENUM_TYPE_DEFINITION => $this->addDescription(function (EnumTypeDefinitionNode $def) : string {
|
||||
return $this->join(
|
||||
[
|
||||
'enum',
|
||||
@@ -327,13 +344,13 @@ class Printer
|
||||
);
|
||||
}),
|
||||
|
||||
NodeKind::ENUM_VALUE_DEFINITION => $this->addDescription(function (EnumValueDefinitionNode $def) {
|
||||
NodeKind::ENUM_VALUE_DEFINITION => $this->addDescription(function (EnumValueDefinitionNode $def) : string {
|
||||
return $this->join([$def->name, $this->join($def->directives, ' ')], ' ');
|
||||
}),
|
||||
|
||||
NodeKind::INPUT_OBJECT_TYPE_DEFINITION => $this->addDescription(function (
|
||||
InputObjectTypeDefinitionNode $def
|
||||
) {
|
||||
) : string {
|
||||
return $this->join(
|
||||
[
|
||||
'input',
|
||||
@@ -345,7 +362,7 @@ class Printer
|
||||
);
|
||||
}),
|
||||
|
||||
NodeKind::SCHEMA_EXTENSION => function (SchemaTypeExtensionNode $def) {
|
||||
NodeKind::SCHEMA_EXTENSION => function (SchemaTypeExtensionNode $def) : string {
|
||||
return $this->join(
|
||||
[
|
||||
'extend schema',
|
||||
@@ -356,7 +373,7 @@ class Printer
|
||||
);
|
||||
},
|
||||
|
||||
NodeKind::SCALAR_TYPE_EXTENSION => function (ScalarTypeExtensionNode $def) {
|
||||
NodeKind::SCALAR_TYPE_EXTENSION => function (ScalarTypeExtensionNode $def) : string {
|
||||
return $this->join(
|
||||
[
|
||||
'extend scalar',
|
||||
@@ -367,7 +384,7 @@ class Printer
|
||||
);
|
||||
},
|
||||
|
||||
NodeKind::OBJECT_TYPE_EXTENSION => function (ObjectTypeExtensionNode $def) {
|
||||
NodeKind::OBJECT_TYPE_EXTENSION => function (ObjectTypeExtensionNode $def) : string {
|
||||
return $this->join(
|
||||
[
|
||||
'extend type',
|
||||
@@ -380,11 +397,12 @@ class Printer
|
||||
);
|
||||
},
|
||||
|
||||
NodeKind::INTERFACE_TYPE_EXTENSION => function (InterfaceTypeExtensionNode $def) {
|
||||
NodeKind::INTERFACE_TYPE_EXTENSION => function (InterfaceTypeExtensionNode $def) : string {
|
||||
return $this->join(
|
||||
[
|
||||
'extend interface',
|
||||
$def->name,
|
||||
$this->wrap('implements ', $this->join($def->interfaces, ' & ')),
|
||||
$this->join($def->directives, ' '),
|
||||
$this->block($def->fields),
|
||||
],
|
||||
@@ -392,13 +410,13 @@ class Printer
|
||||
);
|
||||
},
|
||||
|
||||
NodeKind::UNION_TYPE_EXTENSION => function (UnionTypeExtensionNode $def) {
|
||||
NodeKind::UNION_TYPE_EXTENSION => function (UnionTypeExtensionNode $def) : string {
|
||||
return $this->join(
|
||||
[
|
||||
'extend union',
|
||||
$def->name,
|
||||
$this->join($def->directives, ' '),
|
||||
$def->types
|
||||
count($def->types ?? []) > 0
|
||||
? '= ' . $this->join($def->types, ' | ')
|
||||
: '',
|
||||
],
|
||||
@@ -406,7 +424,7 @@ class Printer
|
||||
);
|
||||
},
|
||||
|
||||
NodeKind::ENUM_TYPE_EXTENSION => function (EnumTypeExtensionNode $def) {
|
||||
NodeKind::ENUM_TYPE_EXTENSION => function (EnumTypeExtensionNode $def) : string {
|
||||
return $this->join(
|
||||
[
|
||||
'extend enum',
|
||||
@@ -418,7 +436,7 @@ class Printer
|
||||
);
|
||||
},
|
||||
|
||||
NodeKind::INPUT_OBJECT_TYPE_EXTENSION => function (InputObjectTypeExtensionNode $def) {
|
||||
NodeKind::INPUT_OBJECT_TYPE_EXTENSION => function (InputObjectTypeExtensionNode $def) : string {
|
||||
return $this->join(
|
||||
[
|
||||
'extend input',
|
||||
@@ -430,8 +448,8 @@ class Printer
|
||||
);
|
||||
},
|
||||
|
||||
NodeKind::DIRECTIVE_DEFINITION => $this->addDescription(function (DirectiveDefinitionNode $def) {
|
||||
$noIndent = Utils::every($def->arguments, static function (string $arg) {
|
||||
NodeKind::DIRECTIVE_DEFINITION => $this->addDescription(function (DirectiveDefinitionNode $def) : string {
|
||||
$noIndent = Utils::every($def->arguments, static function (string $arg) : bool {
|
||||
return strpos($arg, "\n") === false;
|
||||
});
|
||||
|
||||
@@ -440,6 +458,7 @@ class Printer
|
||||
. ($noIndent
|
||||
? $this->wrap('(', $this->join($def->arguments, ', '), ')')
|
||||
: $this->wrap("(\n", $this->indent($this->join($def->arguments, "\n")), "\n"))
|
||||
. ($def->repeatable ? ' repeatable' : '')
|
||||
. ' on ' . $this->join($def->locations, ' | ');
|
||||
}),
|
||||
],
|
||||
@@ -449,7 +468,7 @@ class Printer
|
||||
|
||||
public function addDescription(callable $cb)
|
||||
{
|
||||
return function ($node) use ($cb) {
|
||||
return function ($node) use ($cb) : string {
|
||||
return $this->join([$node->description, $cb($node)], "\n");
|
||||
};
|
||||
}
|
||||
@@ -489,14 +508,14 @@ class Printer
|
||||
return $maybeArray ? count($maybeArray) : 0;
|
||||
}
|
||||
|
||||
public function join($maybeArray, $separator = '')
|
||||
public function join($maybeArray, $separator = '') : string
|
||||
{
|
||||
return $maybeArray
|
||||
? implode(
|
||||
$separator,
|
||||
Utils::filter(
|
||||
$maybeArray,
|
||||
static function ($x) {
|
||||
static function ($x) : bool {
|
||||
return (bool) $x;
|
||||
}
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user