mirror of
https://github.com/retailcrm/graphql-php.git
synced 2025-02-06 15:59:24 +03:00
remove static functions from Printer
This commit is contained in:
parent
399a340973
commit
3c98963f72
@ -40,35 +40,35 @@ use GraphQL\Language\AST\VariableDefinition;
|
|||||||
|
|
||||||
class Printer
|
class Printer
|
||||||
{
|
{
|
||||||
public static function doPrint($ast)
|
public function doPrint($ast)
|
||||||
{
|
{
|
||||||
return Visitor::visit($ast, array(
|
return Visitor::visit($ast, array(
|
||||||
'leave' => array(
|
'leave' => array(
|
||||||
Node::NAME => function($node) {return '' . $node->value;},
|
Node::NAME => function($node) {return '' . $node->value;},
|
||||||
Node::VARIABLE => function($node) {return '$' . $node->name;},
|
Node::VARIABLE => function($node) {return '$' . $node->name;},
|
||||||
Node::DOCUMENT => function(Document $node) {return self::join($node->definitions, "\n\n") . "\n";},
|
Node::DOCUMENT => function(Document $node) {return $this->join($node->definitions, "\n\n") . "\n";},
|
||||||
Node::OPERATION_DEFINITION => function(OperationDefinition $node) {
|
Node::OPERATION_DEFINITION => function(OperationDefinition $node) {
|
||||||
$op = $node->operation;
|
$op = $node->operation;
|
||||||
$name = $node->name;
|
$name = $node->name;
|
||||||
$varDefs = self::wrap('(', self::join($node->variableDefinitions, ', '), ')');
|
$varDefs = $this->wrap('(', $this->join($node->variableDefinitions, ', '), ')');
|
||||||
$directives = self::join($node->directives, ' ');
|
$directives = $this->join($node->directives, ' ');
|
||||||
$selectionSet = $node->selectionSet;
|
$selectionSet = $node->selectionSet;
|
||||||
// Anonymous queries with no directives or variable definitions can use
|
// Anonymous queries with no directives or variable definitions can use
|
||||||
// the query short form.
|
// the query short form.
|
||||||
return !$name && !$directives && !$varDefs && $op === 'query'
|
return !$name && !$directives && !$varDefs && $op === 'query'
|
||||||
? $selectionSet
|
? $selectionSet
|
||||||
: self::join([$op, self::join([$name, $varDefs]), $directives, $selectionSet], ' ');
|
: $this->join([$op, $this->join([$name, $varDefs]), $directives, $selectionSet], ' ');
|
||||||
},
|
},
|
||||||
Node::VARIABLE_DEFINITION => function(VariableDefinition $node) {
|
Node::VARIABLE_DEFINITION => function(VariableDefinition $node) {
|
||||||
return $node->variable . ': ' . $node->type . self::wrap(' = ', $node->defaultValue);
|
return $node->variable . ': ' . $node->type . $this->wrap(' = ', $node->defaultValue);
|
||||||
},
|
},
|
||||||
Node::SELECTION_SET => function(SelectionSet $node) {
|
Node::SELECTION_SET => function(SelectionSet $node) {
|
||||||
return self::block($node->selections);
|
return $this->block($node->selections);
|
||||||
},
|
},
|
||||||
Node::FIELD => function(Field $node) {
|
Node::FIELD => function(Field $node) {
|
||||||
return self::join([
|
return $this->join([
|
||||||
self::wrap('', $node->alias, ': ') . $node->name . self::wrap('(', self::join($node->arguments, ', '), ')'),
|
$this->wrap('', $node->alias, ': ') . $node->name . $this->wrap('(', $this->join($node->arguments, ', '), ')'),
|
||||||
self::join($node->directives, ' '),
|
$this->join($node->directives, ' '),
|
||||||
$node->selectionSet
|
$node->selectionSet
|
||||||
], ' ');
|
], ' ');
|
||||||
},
|
},
|
||||||
@ -78,19 +78,19 @@ class Printer
|
|||||||
|
|
||||||
// Fragments
|
// Fragments
|
||||||
Node::FRAGMENT_SPREAD => function(FragmentSpread $node) {
|
Node::FRAGMENT_SPREAD => function(FragmentSpread $node) {
|
||||||
return '...' . $node->name . self::wrap(' ', self::join($node->directives, ' '));
|
return '...' . $node->name . $this->wrap(' ', $this->join($node->directives, ' '));
|
||||||
},
|
},
|
||||||
Node::INLINE_FRAGMENT => function(InlineFragment $node) {
|
Node::INLINE_FRAGMENT => function(InlineFragment $node) {
|
||||||
return self::join([
|
return $this->join([
|
||||||
"...",
|
"...",
|
||||||
self::wrap('on ', $node->typeCondition),
|
$this->wrap('on ', $node->typeCondition),
|
||||||
self::join($node->directives, ' '),
|
$this->join($node->directives, ' '),
|
||||||
$node->selectionSet
|
$node->selectionSet
|
||||||
], ' ');
|
], ' ');
|
||||||
},
|
},
|
||||||
Node::FRAGMENT_DEFINITION => function(FragmentDefinition $node) {
|
Node::FRAGMENT_DEFINITION => function(FragmentDefinition $node) {
|
||||||
return "fragment {$node->name} on {$node->typeCondition} "
|
return "fragment {$node->name} on {$node->typeCondition} "
|
||||||
. self::wrap('', self::join($node->directives, ' '), ' ')
|
. $this->wrap('', $this->join($node->directives, ' '), ' ')
|
||||||
. $node->selectionSet;
|
. $node->selectionSet;
|
||||||
},
|
},
|
||||||
|
|
||||||
@ -100,13 +100,13 @@ class Printer
|
|||||||
Node::STRING => function(StringValue $node) {return json_encode($node->value);},
|
Node::STRING => function(StringValue $node) {return json_encode($node->value);},
|
||||||
Node::BOOLEAN => function(BooleanValue $node) {return $node->value ? 'true' : 'false';},
|
Node::BOOLEAN => function(BooleanValue $node) {return $node->value ? 'true' : 'false';},
|
||||||
Node::ENUM => function(EnumValue $node) {return $node->value;},
|
Node::ENUM => function(EnumValue $node) {return $node->value;},
|
||||||
Node::LST => function(ListValue $node) {return '[' . self::join($node->values, ', ') . ']';},
|
Node::LST => function(ListValue $node) {return '[' . $this->join($node->values, ', ') . ']';},
|
||||||
Node::OBJECT => function(ObjectValue $node) {return '{' . self::join($node->fields, ', ') . '}';},
|
Node::OBJECT => function(ObjectValue $node) {return '{' . $this->join($node->fields, ', ') . '}';},
|
||||||
Node::OBJECT_FIELD => function(ObjectField $node) {return $node->name . ': ' . $node->value;},
|
Node::OBJECT_FIELD => function(ObjectField $node) {return $node->name . ': ' . $node->value;},
|
||||||
|
|
||||||
// Directive
|
// Directive
|
||||||
Node::DIRECTIVE => function(Directive $node) {
|
Node::DIRECTIVE => function(Directive $node) {
|
||||||
return '@' . $node->name . self::wrap('(', self::join($node->arguments, ', '), ')');
|
return '@' . $node->name . $this->wrap('(', $this->join($node->arguments, ', '), ')');
|
||||||
},
|
},
|
||||||
|
|
||||||
// Type
|
// Type
|
||||||
@ -116,81 +116,81 @@ class Printer
|
|||||||
|
|
||||||
// Type System Definitions
|
// Type System Definitions
|
||||||
Node::SCHEMA_DEFINITION => function(SchemaDefinition $def) {
|
Node::SCHEMA_DEFINITION => function(SchemaDefinition $def) {
|
||||||
return self::join([
|
return $this->join([
|
||||||
'schema',
|
'schema',
|
||||||
self::join($def->directives, ' '),
|
$this->join($def->directives, ' '),
|
||||||
self::block($def->operationTypes)
|
$this->block($def->operationTypes)
|
||||||
], ' ');
|
], ' ');
|
||||||
},
|
},
|
||||||
Node::OPERATION_TYPE_DEFINITION => function(OperationTypeDefinition $def) {return $def->operation . ': ' . $def->type;},
|
Node::OPERATION_TYPE_DEFINITION => function(OperationTypeDefinition $def) {return $def->operation . ': ' . $def->type;},
|
||||||
|
|
||||||
Node::SCALAR_TYPE_DEFINITION => function(ScalarTypeDefinition $def) {
|
Node::SCALAR_TYPE_DEFINITION => function(ScalarTypeDefinition $def) {
|
||||||
return self::join(['scalar', $def->name, self::join($def->directives, ' ')], ' ');
|
return $this->join(['scalar', $def->name, $this->join($def->directives, ' ')], ' ');
|
||||||
},
|
},
|
||||||
Node::OBJECT_TYPE_DEFINITION => function(ObjectTypeDefinition $def) {
|
Node::OBJECT_TYPE_DEFINITION => function(ObjectTypeDefinition $def) {
|
||||||
return self::join([
|
return $this->join([
|
||||||
'type',
|
'type',
|
||||||
$def->name,
|
$def->name,
|
||||||
self::wrap('implements ', self::join($def->interfaces, ', ')),
|
$this->wrap('implements ', $this->join($def->interfaces, ', ')),
|
||||||
self::join($def->directives, ' '),
|
$this->join($def->directives, ' '),
|
||||||
self::block($def->fields)
|
$this->block($def->fields)
|
||||||
], ' ');
|
], ' ');
|
||||||
},
|
},
|
||||||
Node::FIELD_DEFINITION => function(FieldDefinition $def) {
|
Node::FIELD_DEFINITION => function(FieldDefinition $def) {
|
||||||
return $def->name
|
return $def->name
|
||||||
. self::wrap('(', self::join($def->arguments, ', '), ')')
|
. $this->wrap('(', $this->join($def->arguments, ', '), ')')
|
||||||
. ': ' . $def->type
|
. ': ' . $def->type
|
||||||
. self::wrap(' ', self::join($def->directives, ' '));
|
. $this->wrap(' ', $this->join($def->directives, ' '));
|
||||||
},
|
},
|
||||||
Node::INPUT_VALUE_DEFINITION => function(InputValueDefinition $def) {
|
Node::INPUT_VALUE_DEFINITION => function(InputValueDefinition $def) {
|
||||||
return self::join([
|
return $this->join([
|
||||||
$def->name . ': ' . $def->type,
|
$def->name . ': ' . $def->type,
|
||||||
self::wrap('= ', $def->defaultValue),
|
$this->wrap('= ', $def->defaultValue),
|
||||||
self::join($def->directives, ' ')
|
$this->join($def->directives, ' ')
|
||||||
], ' ');
|
], ' ');
|
||||||
},
|
},
|
||||||
Node::INTERFACE_TYPE_DEFINITION => function(InterfaceTypeDefinition $def) {
|
Node::INTERFACE_TYPE_DEFINITION => function(InterfaceTypeDefinition $def) {
|
||||||
return self::join([
|
return $this->join([
|
||||||
'interface',
|
'interface',
|
||||||
$def->name,
|
$def->name,
|
||||||
self::join($def->directives, ' '),
|
$this->join($def->directives, ' '),
|
||||||
self::block($def->fields)
|
$this->block($def->fields)
|
||||||
], ' ');
|
], ' ');
|
||||||
},
|
},
|
||||||
Node::UNION_TYPE_DEFINITION => function(UnionTypeDefinition $def) {
|
Node::UNION_TYPE_DEFINITION => function(UnionTypeDefinition $def) {
|
||||||
return self::join([
|
return $this->join([
|
||||||
'union',
|
'union',
|
||||||
$def->name,
|
$def->name,
|
||||||
self::join($def->directives, ' '),
|
$this->join($def->directives, ' '),
|
||||||
'= ' . self::join($def->types, ' | ')
|
'= ' . $this->join($def->types, ' | ')
|
||||||
], ' ');
|
], ' ');
|
||||||
},
|
},
|
||||||
Node::ENUM_TYPE_DEFINITION => function(EnumTypeDefinition $def) {
|
Node::ENUM_TYPE_DEFINITION => function(EnumTypeDefinition $def) {
|
||||||
return self::join([
|
return $this->join([
|
||||||
'enum',
|
'enum',
|
||||||
$def->name,
|
$def->name,
|
||||||
self::join($def->directives, ' '),
|
$this->join($def->directives, ' '),
|
||||||
self::block($def->values)
|
$this->block($def->values)
|
||||||
], ' ');
|
], ' ');
|
||||||
},
|
},
|
||||||
Node::ENUM_VALUE_DEFINITION => function(EnumValueDefinition $def) {
|
Node::ENUM_VALUE_DEFINITION => function(EnumValueDefinition $def) {
|
||||||
return self::join([
|
return $this->join([
|
||||||
$def->name,
|
$def->name,
|
||||||
self::join($def->directives, ' ')
|
$this->join($def->directives, ' ')
|
||||||
], ' ');
|
], ' ');
|
||||||
},
|
},
|
||||||
Node::INPUT_OBJECT_TYPE_DEFINITION => function(InputObjectTypeDefinition $def) {
|
Node::INPUT_OBJECT_TYPE_DEFINITION => function(InputObjectTypeDefinition $def) {
|
||||||
return self::join([
|
return $this->join([
|
||||||
'input',
|
'input',
|
||||||
$def->name,
|
$def->name,
|
||||||
self::join($def->directives, ' '),
|
$this->join($def->directives, ' '),
|
||||||
self::block($def->fields)
|
$this->block($def->fields)
|
||||||
], ' ');
|
], ' ');
|
||||||
},
|
},
|
||||||
Node::TYPE_EXTENSION_DEFINITION => function(TypeExtensionDefinition $def) {return "extend {$def->definition}";},
|
Node::TYPE_EXTENSION_DEFINITION => function(TypeExtensionDefinition $def) {return "extend {$def->definition}";},
|
||||||
Node::DIRECTIVE_DEFINITION => function(DirectiveDefinition $def) {
|
Node::DIRECTIVE_DEFINITION => function(DirectiveDefinition $def) {
|
||||||
return 'directive @' . $def->name . self::wrap('(', self::join($def->arguments, ', '), ')')
|
return 'directive @' . $def->name . $this->wrap('(', $this->join($def->arguments, ', '), ')')
|
||||||
. ' on ' . self::join($def->locations, ' | ');
|
. ' on ' . $this->join($def->locations, ' | ');
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
));
|
));
|
||||||
@ -200,7 +200,7 @@ class Printer
|
|||||||
* If maybeString is not null or empty, then wrap with start and end, otherwise
|
* If maybeString is not null or empty, then wrap with start and end, otherwise
|
||||||
* print an empty string.
|
* print an empty string.
|
||||||
*/
|
*/
|
||||||
public static function wrap($start, $maybeString, $end = '')
|
public function wrap($start, $maybeString, $end = '')
|
||||||
{
|
{
|
||||||
return $maybeString ? ($start . $maybeString . $end) : '';
|
return $maybeString ? ($start . $maybeString . $end) : '';
|
||||||
}
|
}
|
||||||
@ -209,27 +209,27 @@ class Printer
|
|||||||
* Given array, print each item on its own line, wrapped in an
|
* Given array, print each item on its own line, wrapped in an
|
||||||
* indented "{ }" block.
|
* indented "{ }" block.
|
||||||
*/
|
*/
|
||||||
public static function block($array)
|
public function block($array)
|
||||||
{
|
{
|
||||||
return $array && self::length($array) ? self::indent("{\n" . self::join($array, "\n")) . "\n}" : '{}';
|
return $array && $this->length($array) ? $this->indent("{\n" . $this->join($array, "\n")) . "\n}" : '{}';
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function indent($maybeString)
|
public function indent($maybeString)
|
||||||
{
|
{
|
||||||
return $maybeString ? str_replace("\n", "\n ", $maybeString) : '';
|
return $maybeString ? str_replace("\n", "\n ", $maybeString) : '';
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function manyList($start, $list, $separator, $end)
|
public function manyList($start, $list, $separator, $end)
|
||||||
{
|
{
|
||||||
return self::length($list) === 0 ? null : ($start . self::join($list, $separator) . $end);
|
return $this->length($list) === 0 ? null : ($start . $this->join($list, $separator) . $end);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function length($maybeArray)
|
public function length($maybeArray)
|
||||||
{
|
{
|
||||||
return $maybeArray ? count($maybeArray) : 0;
|
return $maybeArray ? count($maybeArray) : 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function join($maybeArray, $separator = '')
|
public function join($maybeArray, $separator = '')
|
||||||
{
|
{
|
||||||
return $maybeArray
|
return $maybeArray
|
||||||
? implode(
|
? implode(
|
||||||
|
@ -26,7 +26,8 @@ class PrinterTest extends \PHPUnit_Framework_TestCase
|
|||||||
$astCopy = $ast->cloneDeep();
|
$astCopy = $ast->cloneDeep();
|
||||||
$this->assertEquals($astCopy, $ast);
|
$this->assertEquals($astCopy, $ast);
|
||||||
|
|
||||||
Printer::doPrint($ast);
|
$printer = new Printer();
|
||||||
|
$printer->doPrint($ast);
|
||||||
$this->assertEquals($astCopy, $ast);
|
$this->assertEquals($astCopy, $ast);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -35,8 +36,10 @@ class PrinterTest extends \PHPUnit_Framework_TestCase
|
|||||||
*/
|
*/
|
||||||
public function testPrintsMinimalAst()
|
public function testPrintsMinimalAst()
|
||||||
{
|
{
|
||||||
|
$printer = new Printer();
|
||||||
|
|
||||||
$ast = new Field(['name' => new Name(['value' => 'foo'])]);
|
$ast = new Field(['name' => new Name(['value' => 'foo'])]);
|
||||||
$this->assertEquals('foo', Printer::doPrint($ast));
|
$this->assertEquals('foo', $printer->doPrint($ast));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -46,7 +49,8 @@ class PrinterTest extends \PHPUnit_Framework_TestCase
|
|||||||
{
|
{
|
||||||
$badAst1 = new \ArrayObject(array('random' => 'Data'));
|
$badAst1 = new \ArrayObject(array('random' => 'Data'));
|
||||||
try {
|
try {
|
||||||
Printer::doPrint($badAst1);
|
$printer = new Printer();
|
||||||
|
$printer->doPrint($badAst1);
|
||||||
$this->fail('Expected exception not thrown');
|
$this->fail('Expected exception not thrown');
|
||||||
} catch (\Exception $e) {
|
} catch (\Exception $e) {
|
||||||
$this->assertEquals('Invalid AST Node: {"random":"Data"}', $e->getMessage());
|
$this->assertEquals('Invalid AST Node: {"random":"Data"}', $e->getMessage());
|
||||||
@ -60,12 +64,14 @@ class PrinterTest extends \PHPUnit_Framework_TestCase
|
|||||||
{
|
{
|
||||||
$queryAstShorthanded = Parser::parse('query { id, name }');
|
$queryAstShorthanded = Parser::parse('query { id, name }');
|
||||||
|
|
||||||
|
$printer = new Printer();
|
||||||
|
|
||||||
$expected = '{
|
$expected = '{
|
||||||
id
|
id
|
||||||
name
|
name
|
||||||
}
|
}
|
||||||
';
|
';
|
||||||
$this->assertEquals($expected, Printer::doPrint($queryAstShorthanded));
|
$this->assertEquals($expected, $printer->doPrint($queryAstShorthanded));
|
||||||
|
|
||||||
$mutationAst = Parser::parse('mutation { id, name }');
|
$mutationAst = Parser::parse('mutation { id, name }');
|
||||||
$expected = 'mutation {
|
$expected = 'mutation {
|
||||||
@ -73,7 +79,7 @@ class PrinterTest extends \PHPUnit_Framework_TestCase
|
|||||||
name
|
name
|
||||||
}
|
}
|
||||||
';
|
';
|
||||||
$this->assertEquals($expected, Printer::doPrint($mutationAst));
|
$this->assertEquals($expected, $printer->doPrint($mutationAst));
|
||||||
|
|
||||||
$queryAstWithArtifacts = Parser::parse(
|
$queryAstWithArtifacts = Parser::parse(
|
||||||
'query ($foo: TestType) @testDirective { id, name }'
|
'query ($foo: TestType) @testDirective { id, name }'
|
||||||
@ -83,7 +89,7 @@ class PrinterTest extends \PHPUnit_Framework_TestCase
|
|||||||
name
|
name
|
||||||
}
|
}
|
||||||
';
|
';
|
||||||
$this->assertEquals($expected, Printer::doPrint($queryAstWithArtifacts));
|
$this->assertEquals($expected, $printer->doPrint($queryAstWithArtifacts));
|
||||||
|
|
||||||
$mutationAstWithArtifacts = Parser::parse(
|
$mutationAstWithArtifacts = Parser::parse(
|
||||||
'mutation ($foo: TestType) @testDirective { id, name }'
|
'mutation ($foo: TestType) @testDirective { id, name }'
|
||||||
@ -93,7 +99,7 @@ class PrinterTest extends \PHPUnit_Framework_TestCase
|
|||||||
name
|
name
|
||||||
}
|
}
|
||||||
';
|
';
|
||||||
$this->assertEquals($expected, Printer::doPrint($mutationAstWithArtifacts));
|
$this->assertEquals($expected, $printer->doPrint($mutationAstWithArtifacts));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -104,7 +110,8 @@ class PrinterTest extends \PHPUnit_Framework_TestCase
|
|||||||
$kitchenSink = file_get_contents(__DIR__ . '/kitchen-sink.graphql');
|
$kitchenSink = file_get_contents(__DIR__ . '/kitchen-sink.graphql');
|
||||||
$ast = Parser::parse($kitchenSink);
|
$ast = Parser::parse($kitchenSink);
|
||||||
|
|
||||||
$printed = Printer::doPrint($ast);
|
$printer = new Printer();
|
||||||
|
$printed = $printer->doPrint($ast);
|
||||||
|
|
||||||
$expected = <<<'EOT'
|
$expected = <<<'EOT'
|
||||||
query queryName($foo: ComplexType, $site: Site = MOBILE) {
|
query queryName($foo: ComplexType, $site: Site = MOBILE) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user