mirror of
https://github.com/retailcrm/graphql-php.git
synced 2024-11-25 06:16:05 +03:00
Reverted several breaking changes from #75; tests are green
This commit is contained in:
parent
b2c8d8eeb1
commit
5d889ccacd
@ -41,7 +41,17 @@ use GraphQL\Language\AST\VariableDefinition;
|
||||
|
||||
class Printer
|
||||
{
|
||||
public function doPrint($ast)
|
||||
public static function doPrint($ast)
|
||||
{
|
||||
static $instance;
|
||||
$instance = $instance ?: new static();
|
||||
return $instance->printAST($ast);
|
||||
}
|
||||
|
||||
protected function __construct()
|
||||
{}
|
||||
|
||||
public function printAST($ast)
|
||||
{
|
||||
return Visitor::visit($ast, [
|
||||
'leave' => [
|
||||
|
@ -5,6 +5,7 @@ use GraphQL\Language\AST\Field;
|
||||
use GraphQL\Language\AST\ListType;
|
||||
use GraphQL\Language\AST\NamedType;
|
||||
use GraphQL\Language\AST\Node;
|
||||
use GraphQL\Language\AST\NodeType;
|
||||
use GraphQL\Language\AST\NonNullType;
|
||||
use GraphQL\Schema;
|
||||
use GraphQL\Type\Definition\AbstractType;
|
||||
@ -309,7 +310,7 @@ class TypeInfo
|
||||
$schema = $this->schema;
|
||||
|
||||
switch ($node->kind) {
|
||||
case Node::SELECTION_SET:
|
||||
case NodeType::SELECTION_SET:
|
||||
$namedType = Type::getNamedType($this->getType());
|
||||
$compositeType = null;
|
||||
if (Type::isCompositeType($namedType)) {
|
||||
@ -319,7 +320,7 @@ class TypeInfo
|
||||
$this->parentTypeStack[] = $compositeType; // push
|
||||
break;
|
||||
|
||||
case Node::FIELD:
|
||||
case NodeType::FIELD:
|
||||
$parentType = $this->getParentType();
|
||||
$fieldDef = null;
|
||||
if ($parentType) {
|
||||
@ -329,11 +330,11 @@ class TypeInfo
|
||||
$this->typeStack[] = $fieldDef ? $fieldDef->getType() : null; // push
|
||||
break;
|
||||
|
||||
case Node::DIRECTIVE:
|
||||
case NodeType::DIRECTIVE:
|
||||
$this->directive = $schema->getDirective($node->name->value);
|
||||
break;
|
||||
|
||||
case Node::OPERATION_DEFINITION:
|
||||
case NodeType::OPERATION_DEFINITION:
|
||||
$type = null;
|
||||
if ($node->operation === 'query') {
|
||||
$type = $schema->getQueryType();
|
||||
@ -345,19 +346,19 @@ class TypeInfo
|
||||
$this->typeStack[] = $type; // push
|
||||
break;
|
||||
|
||||
case Node::INLINE_FRAGMENT:
|
||||
case Node::FRAGMENT_DEFINITION:
|
||||
case NodeType::INLINE_FRAGMENT:
|
||||
case NodeType::FRAGMENT_DEFINITION:
|
||||
$typeConditionAST = $node->typeCondition;
|
||||
$outputType = $typeConditionAST ? self::typeFromAST($schema, $typeConditionAST) : $this->getType();
|
||||
$this->typeStack[] = $outputType; // push
|
||||
break;
|
||||
|
||||
case Node::VARIABLE_DEFINITION:
|
||||
case NodeType::VARIABLE_DEFINITION:
|
||||
$inputType = self::typeFromAST($schema, $node->type);
|
||||
$this->inputTypeStack[] = $inputType; // push
|
||||
break;
|
||||
|
||||
case Node::ARGUMENT:
|
||||
case NodeType::ARGUMENT:
|
||||
$fieldOrDirective = $this->getDirective() ?: $this->getFieldDef();
|
||||
$argDef = $argType = null;
|
||||
if ($fieldOrDirective) {
|
||||
@ -370,12 +371,12 @@ class TypeInfo
|
||||
$this->inputTypeStack[] = $argType; // push
|
||||
break;
|
||||
|
||||
case Node::LST:
|
||||
case NodeType::LST:
|
||||
$listType = Type::getNullableType($this->getInputType());
|
||||
$this->inputTypeStack[] = ($listType instanceof ListOfType ? $listType->getWrappedType() : null); // push
|
||||
break;
|
||||
|
||||
case Node::OBJECT_FIELD:
|
||||
case NodeType::OBJECT_FIELD:
|
||||
$objectType = Type::getNamedType($this->getInputType());
|
||||
$fieldType = null;
|
||||
if ($objectType instanceof InputObjectType) {
|
||||
@ -394,33 +395,33 @@ class TypeInfo
|
||||
function leave(Node $node)
|
||||
{
|
||||
switch ($node->kind) {
|
||||
case Node::SELECTION_SET:
|
||||
case NodeType::SELECTION_SET:
|
||||
array_pop($this->parentTypeStack);
|
||||
break;
|
||||
|
||||
case Node::FIELD:
|
||||
case NodeType::FIELD:
|
||||
array_pop($this->fieldDefStack);
|
||||
array_pop($this->typeStack);
|
||||
break;
|
||||
|
||||
case Node::DIRECTIVE:
|
||||
case NodeType::DIRECTIVE:
|
||||
$this->directive = null;
|
||||
break;
|
||||
|
||||
case Node::OPERATION_DEFINITION:
|
||||
case Node::INLINE_FRAGMENT:
|
||||
case Node::FRAGMENT_DEFINITION:
|
||||
case NodeType::OPERATION_DEFINITION:
|
||||
case NodeType::INLINE_FRAGMENT:
|
||||
case NodeType::FRAGMENT_DEFINITION:
|
||||
array_pop($this->typeStack);
|
||||
break;
|
||||
case Node::VARIABLE_DEFINITION:
|
||||
case NodeType::VARIABLE_DEFINITION:
|
||||
array_pop($this->inputTypeStack);
|
||||
break;
|
||||
case Node::ARGUMENT:
|
||||
case NodeType::ARGUMENT:
|
||||
$this->argument = null;
|
||||
array_pop($this->inputTypeStack);
|
||||
break;
|
||||
case Node::LST:
|
||||
case Node::OBJECT_FIELD:
|
||||
case NodeType::LST:
|
||||
case NodeType::OBJECT_FIELD:
|
||||
array_pop($this->inputTypeStack);
|
||||
break;
|
||||
}
|
||||
|
@ -3,11 +3,9 @@ namespace GraphQL\Validator\Rules;
|
||||
|
||||
|
||||
use GraphQL\Error\Error;
|
||||
use GraphQL\Language\AST\Name;
|
||||
use GraphQL\Language\AST\NamedType;
|
||||
use GraphQL\Language\AST\Node;
|
||||
use GraphQL\Language\AST\NodeType;
|
||||
use GraphQL\Language\Visitor;
|
||||
use GraphQL\Validator\Messages;
|
||||
use GraphQL\Validator\ValidationContext;
|
||||
|
||||
class KnownTypeNames
|
||||
|
@ -6,6 +6,7 @@ use GraphQL\Language\AST\Field;
|
||||
use GraphQL\Language\AST\FragmentSpread;
|
||||
use GraphQL\Language\AST\InlineFragment;
|
||||
use GraphQL\Language\AST\Node;
|
||||
use GraphQL\Language\AST\NodeType;
|
||||
use GraphQL\Language\AST\OperationDefinition;
|
||||
use GraphQL\Language\AST\SelectionSet;
|
||||
use GraphQL\Validator\ValidationContext;
|
||||
|
@ -26,8 +26,7 @@ class PrinterTest extends \PHPUnit_Framework_TestCase
|
||||
$astCopy = $ast->cloneDeep();
|
||||
$this->assertEquals($astCopy, $ast);
|
||||
|
||||
$printer = new Printer();
|
||||
$printer->doPrint($ast);
|
||||
Printer::doPrint($ast);
|
||||
$this->assertEquals($astCopy, $ast);
|
||||
}
|
||||
|
||||
@ -36,10 +35,8 @@ class PrinterTest extends \PHPUnit_Framework_TestCase
|
||||
*/
|
||||
public function testPrintsMinimalAst()
|
||||
{
|
||||
$printer = new Printer();
|
||||
|
||||
$ast = new Field(['name' => new Name(['value' => 'foo'])]);
|
||||
$this->assertEquals('foo', $printer->doPrint($ast));
|
||||
$this->assertEquals('foo', Printer::doPrint($ast));
|
||||
}
|
||||
|
||||
/**
|
||||
@ -49,8 +46,7 @@ class PrinterTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
$badAst1 = new \ArrayObject(array('random' => 'Data'));
|
||||
try {
|
||||
$printer = new Printer();
|
||||
$printer->doPrint($badAst1);
|
||||
Printer::doPrint($badAst1);
|
||||
$this->fail('Expected exception not thrown');
|
||||
} catch (\Exception $e) {
|
||||
$this->assertEquals('Invalid AST Node: {"random":"Data"}', $e->getMessage());
|
||||
@ -64,14 +60,12 @@ class PrinterTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
$queryAstShorthanded = Parser::parse('query { id, name }');
|
||||
|
||||
$printer = new Printer();
|
||||
|
||||
$expected = '{
|
||||
id
|
||||
name
|
||||
}
|
||||
';
|
||||
$this->assertEquals($expected, $printer->doPrint($queryAstShorthanded));
|
||||
$this->assertEquals($expected, Printer::doPrint($queryAstShorthanded));
|
||||
|
||||
$mutationAst = Parser::parse('mutation { id, name }');
|
||||
$expected = 'mutation {
|
||||
@ -79,7 +73,7 @@ class PrinterTest extends \PHPUnit_Framework_TestCase
|
||||
name
|
||||
}
|
||||
';
|
||||
$this->assertEquals($expected, $printer->doPrint($mutationAst));
|
||||
$this->assertEquals($expected, Printer::doPrint($mutationAst));
|
||||
|
||||
$queryAstWithArtifacts = Parser::parse(
|
||||
'query ($foo: TestType) @testDirective { id, name }'
|
||||
@ -89,7 +83,7 @@ class PrinterTest extends \PHPUnit_Framework_TestCase
|
||||
name
|
||||
}
|
||||
';
|
||||
$this->assertEquals($expected, $printer->doPrint($queryAstWithArtifacts));
|
||||
$this->assertEquals($expected, Printer::doPrint($queryAstWithArtifacts));
|
||||
|
||||
$mutationAstWithArtifacts = Parser::parse(
|
||||
'mutation ($foo: TestType) @testDirective { id, name }'
|
||||
@ -99,7 +93,7 @@ class PrinterTest extends \PHPUnit_Framework_TestCase
|
||||
name
|
||||
}
|
||||
';
|
||||
$this->assertEquals($expected, $printer->doPrint($mutationAstWithArtifacts));
|
||||
$this->assertEquals($expected, Printer::doPrint($mutationAstWithArtifacts));
|
||||
}
|
||||
|
||||
/**
|
||||
@ -110,8 +104,7 @@ class PrinterTest extends \PHPUnit_Framework_TestCase
|
||||
$kitchenSink = file_get_contents(__DIR__ . '/kitchen-sink.graphql');
|
||||
$ast = Parser::parse($kitchenSink);
|
||||
|
||||
$printer = new Printer();
|
||||
$printed = $printer->doPrint($ast);
|
||||
$printed = Printer::doPrint($ast);
|
||||
|
||||
$expected = <<<'EOT'
|
||||
query queryName($foo: ComplexType, $site: Site = MOBILE) {
|
||||
|
@ -5,6 +5,7 @@ use GraphQL\Language\AST\Document;
|
||||
use GraphQL\Language\AST\Field;
|
||||
use GraphQL\Language\AST\Name;
|
||||
use GraphQL\Language\AST\Node;
|
||||
use GraphQL\Language\AST\NodeType;
|
||||
use GraphQL\Language\AST\OperationDefinition;
|
||||
use GraphQL\Language\AST\SelectionSet;
|
||||
use GraphQL\Language\Parser;
|
||||
@ -25,7 +26,7 @@ class VisitorTest extends \PHPUnit_Framework_TestCase
|
||||
|
||||
$selectionSet = null;
|
||||
$editedAst = Visitor::visit($ast, [
|
||||
Node::OPERATION_DEFINITION => [
|
||||
NodeType::OPERATION_DEFINITION => [
|
||||
'enter' => function(OperationDefinition $node) use (&$selectionSet) {
|
||||
$selectionSet = $node->selectionSet;
|
||||
|
||||
@ -63,7 +64,7 @@ class VisitorTest extends \PHPUnit_Framework_TestCase
|
||||
$definitions = $ast->definitions;
|
||||
|
||||
$editedAst = Visitor::visit($ast, [
|
||||
Node::DOCUMENT => [
|
||||
NodeType::DOCUMENT => [
|
||||
'enter' => function (Document $node) {
|
||||
$tmp = clone $node;
|
||||
$tmp->definitions = [];
|
||||
@ -264,7 +265,7 @@ class VisitorTest extends \PHPUnit_Framework_TestCase
|
||||
'leave' => function($node) use (&$visited) {
|
||||
$visited[] = ['leave', $node->kind, isset($node->value) ? $node->value : null];
|
||||
|
||||
if ($node->kind === Node::NAME && $node->value === 'x') {
|
||||
if ($node->kind === NodeType::NAME && $node->value === 'x') {
|
||||
return Visitor::stop();
|
||||
}
|
||||
}
|
||||
@ -297,10 +298,10 @@ class VisitorTest extends \PHPUnit_Framework_TestCase
|
||||
$ast = Parser::parse('{ a, b { x }, c }');
|
||||
|
||||
Visitor::visit($ast, [
|
||||
Node::NAME => function(Name $node) use (&$visited) {
|
||||
NodeType::NAME => function(Name $node) use (&$visited) {
|
||||
$visited[] = ['enter', $node->kind, $node->value];
|
||||
},
|
||||
Node::SELECTION_SET => [
|
||||
NodeType::SELECTION_SET => [
|
||||
'enter' => function(SelectionSet $node) use (&$visited) {
|
||||
$visited[] = ['enter', $node->kind, null];
|
||||
},
|
||||
|
@ -309,18 +309,16 @@ abstract class TestCase extends \PHPUnit_Framework_TestCase
|
||||
|
||||
function expectValid($schema, $rules, $queryString)
|
||||
{
|
||||
$parser = new Parser(new Lexer());
|
||||
$this->assertEquals(
|
||||
[],
|
||||
DocumentValidator::validate($schema, $parser->parse($queryString), $rules),
|
||||
DocumentValidator::validate($schema, Parser::parse($queryString), $rules),
|
||||
'Should validate'
|
||||
);
|
||||
}
|
||||
|
||||
function expectInvalid($schema, $rules, $queryString, $expectedErrors)
|
||||
{
|
||||
$parser = new Parser(new Lexer());
|
||||
$errors = DocumentValidator::validate($schema, $parser->parse($queryString), $rules);
|
||||
$errors = DocumentValidator::validate($schema, Parser::parse($queryString), $rules);
|
||||
|
||||
$this->assertNotEmpty($errors, 'GraphQL should not validate');
|
||||
$this->assertEquals($expectedErrors, array_map(['GraphQL\Error\Error', 'formatError'], $errors));
|
||||
|
Loading…
Reference in New Issue
Block a user