Reverted several breaking changes from #75; tests are green

This commit is contained in:
vladar 2016-11-16 18:37:35 +07:00
parent b2c8d8eeb1
commit 5d889ccacd
7 changed files with 50 additions and 48 deletions

View File

@ -41,7 +41,17 @@ use GraphQL\Language\AST\VariableDefinition;
class Printer 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, [ return Visitor::visit($ast, [
'leave' => [ 'leave' => [

View File

@ -5,6 +5,7 @@ use GraphQL\Language\AST\Field;
use GraphQL\Language\AST\ListType; use GraphQL\Language\AST\ListType;
use GraphQL\Language\AST\NamedType; use GraphQL\Language\AST\NamedType;
use GraphQL\Language\AST\Node; use GraphQL\Language\AST\Node;
use GraphQL\Language\AST\NodeType;
use GraphQL\Language\AST\NonNullType; use GraphQL\Language\AST\NonNullType;
use GraphQL\Schema; use GraphQL\Schema;
use GraphQL\Type\Definition\AbstractType; use GraphQL\Type\Definition\AbstractType;
@ -309,7 +310,7 @@ class TypeInfo
$schema = $this->schema; $schema = $this->schema;
switch ($node->kind) { switch ($node->kind) {
case Node::SELECTION_SET: case NodeType::SELECTION_SET:
$namedType = Type::getNamedType($this->getType()); $namedType = Type::getNamedType($this->getType());
$compositeType = null; $compositeType = null;
if (Type::isCompositeType($namedType)) { if (Type::isCompositeType($namedType)) {
@ -319,7 +320,7 @@ class TypeInfo
$this->parentTypeStack[] = $compositeType; // push $this->parentTypeStack[] = $compositeType; // push
break; break;
case Node::FIELD: case NodeType::FIELD:
$parentType = $this->getParentType(); $parentType = $this->getParentType();
$fieldDef = null; $fieldDef = null;
if ($parentType) { if ($parentType) {
@ -329,11 +330,11 @@ class TypeInfo
$this->typeStack[] = $fieldDef ? $fieldDef->getType() : null; // push $this->typeStack[] = $fieldDef ? $fieldDef->getType() : null; // push
break; break;
case Node::DIRECTIVE: case NodeType::DIRECTIVE:
$this->directive = $schema->getDirective($node->name->value); $this->directive = $schema->getDirective($node->name->value);
break; break;
case Node::OPERATION_DEFINITION: case NodeType::OPERATION_DEFINITION:
$type = null; $type = null;
if ($node->operation === 'query') { if ($node->operation === 'query') {
$type = $schema->getQueryType(); $type = $schema->getQueryType();
@ -345,19 +346,19 @@ class TypeInfo
$this->typeStack[] = $type; // push $this->typeStack[] = $type; // push
break; break;
case Node::INLINE_FRAGMENT: case NodeType::INLINE_FRAGMENT:
case Node::FRAGMENT_DEFINITION: case NodeType::FRAGMENT_DEFINITION:
$typeConditionAST = $node->typeCondition; $typeConditionAST = $node->typeCondition;
$outputType = $typeConditionAST ? self::typeFromAST($schema, $typeConditionAST) : $this->getType(); $outputType = $typeConditionAST ? self::typeFromAST($schema, $typeConditionAST) : $this->getType();
$this->typeStack[] = $outputType; // push $this->typeStack[] = $outputType; // push
break; break;
case Node::VARIABLE_DEFINITION: case NodeType::VARIABLE_DEFINITION:
$inputType = self::typeFromAST($schema, $node->type); $inputType = self::typeFromAST($schema, $node->type);
$this->inputTypeStack[] = $inputType; // push $this->inputTypeStack[] = $inputType; // push
break; break;
case Node::ARGUMENT: case NodeType::ARGUMENT:
$fieldOrDirective = $this->getDirective() ?: $this->getFieldDef(); $fieldOrDirective = $this->getDirective() ?: $this->getFieldDef();
$argDef = $argType = null; $argDef = $argType = null;
if ($fieldOrDirective) { if ($fieldOrDirective) {
@ -370,12 +371,12 @@ class TypeInfo
$this->inputTypeStack[] = $argType; // push $this->inputTypeStack[] = $argType; // push
break; break;
case Node::LST: case NodeType::LST:
$listType = Type::getNullableType($this->getInputType()); $listType = Type::getNullableType($this->getInputType());
$this->inputTypeStack[] = ($listType instanceof ListOfType ? $listType->getWrappedType() : null); // push $this->inputTypeStack[] = ($listType instanceof ListOfType ? $listType->getWrappedType() : null); // push
break; break;
case Node::OBJECT_FIELD: case NodeType::OBJECT_FIELD:
$objectType = Type::getNamedType($this->getInputType()); $objectType = Type::getNamedType($this->getInputType());
$fieldType = null; $fieldType = null;
if ($objectType instanceof InputObjectType) { if ($objectType instanceof InputObjectType) {
@ -394,33 +395,33 @@ class TypeInfo
function leave(Node $node) function leave(Node $node)
{ {
switch ($node->kind) { switch ($node->kind) {
case Node::SELECTION_SET: case NodeType::SELECTION_SET:
array_pop($this->parentTypeStack); array_pop($this->parentTypeStack);
break; break;
case Node::FIELD: case NodeType::FIELD:
array_pop($this->fieldDefStack); array_pop($this->fieldDefStack);
array_pop($this->typeStack); array_pop($this->typeStack);
break; break;
case Node::DIRECTIVE: case NodeType::DIRECTIVE:
$this->directive = null; $this->directive = null;
break; break;
case Node::OPERATION_DEFINITION: case NodeType::OPERATION_DEFINITION:
case Node::INLINE_FRAGMENT: case NodeType::INLINE_FRAGMENT:
case Node::FRAGMENT_DEFINITION: case NodeType::FRAGMENT_DEFINITION:
array_pop($this->typeStack); array_pop($this->typeStack);
break; break;
case Node::VARIABLE_DEFINITION: case NodeType::VARIABLE_DEFINITION:
array_pop($this->inputTypeStack); array_pop($this->inputTypeStack);
break; break;
case Node::ARGUMENT: case NodeType::ARGUMENT:
$this->argument = null; $this->argument = null;
array_pop($this->inputTypeStack); array_pop($this->inputTypeStack);
break; break;
case Node::LST: case NodeType::LST:
case Node::OBJECT_FIELD: case NodeType::OBJECT_FIELD:
array_pop($this->inputTypeStack); array_pop($this->inputTypeStack);
break; break;
} }

View File

@ -3,11 +3,9 @@ namespace GraphQL\Validator\Rules;
use GraphQL\Error\Error; use GraphQL\Error\Error;
use GraphQL\Language\AST\Name;
use GraphQL\Language\AST\NamedType; use GraphQL\Language\AST\NamedType;
use GraphQL\Language\AST\Node; use GraphQL\Language\AST\NodeType;
use GraphQL\Language\Visitor; use GraphQL\Language\Visitor;
use GraphQL\Validator\Messages;
use GraphQL\Validator\ValidationContext; use GraphQL\Validator\ValidationContext;
class KnownTypeNames class KnownTypeNames

View File

@ -6,6 +6,7 @@ use GraphQL\Language\AST\Field;
use GraphQL\Language\AST\FragmentSpread; use GraphQL\Language\AST\FragmentSpread;
use GraphQL\Language\AST\InlineFragment; use GraphQL\Language\AST\InlineFragment;
use GraphQL\Language\AST\Node; use GraphQL\Language\AST\Node;
use GraphQL\Language\AST\NodeType;
use GraphQL\Language\AST\OperationDefinition; use GraphQL\Language\AST\OperationDefinition;
use GraphQL\Language\AST\SelectionSet; use GraphQL\Language\AST\SelectionSet;
use GraphQL\Validator\ValidationContext; use GraphQL\Validator\ValidationContext;

View File

@ -26,8 +26,7 @@ class PrinterTest extends \PHPUnit_Framework_TestCase
$astCopy = $ast->cloneDeep(); $astCopy = $ast->cloneDeep();
$this->assertEquals($astCopy, $ast); $this->assertEquals($astCopy, $ast);
$printer = new Printer(); Printer::doPrint($ast);
$printer->doPrint($ast);
$this->assertEquals($astCopy, $ast); $this->assertEquals($astCopy, $ast);
} }
@ -36,10 +35,8 @@ 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));
} }
/** /**
@ -49,8 +46,7 @@ class PrinterTest extends \PHPUnit_Framework_TestCase
{ {
$badAst1 = new \ArrayObject(array('random' => 'Data')); $badAst1 = new \ArrayObject(array('random' => 'Data'));
try { try {
$printer = new Printer(); Printer::doPrint($badAst1);
$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());
@ -64,14 +60,12 @@ 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 {
@ -79,7 +73,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 }'
@ -89,7 +83,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 }'
@ -99,7 +93,7 @@ class PrinterTest extends \PHPUnit_Framework_TestCase
name 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'); $kitchenSink = file_get_contents(__DIR__ . '/kitchen-sink.graphql');
$ast = Parser::parse($kitchenSink); $ast = Parser::parse($kitchenSink);
$printer = new Printer(); $printed = Printer::doPrint($ast);
$printed = $printer->doPrint($ast);
$expected = <<<'EOT' $expected = <<<'EOT'
query queryName($foo: ComplexType, $site: Site = MOBILE) { query queryName($foo: ComplexType, $site: Site = MOBILE) {

View File

@ -5,6 +5,7 @@ use GraphQL\Language\AST\Document;
use GraphQL\Language\AST\Field; use GraphQL\Language\AST\Field;
use GraphQL\Language\AST\Name; use GraphQL\Language\AST\Name;
use GraphQL\Language\AST\Node; use GraphQL\Language\AST\Node;
use GraphQL\Language\AST\NodeType;
use GraphQL\Language\AST\OperationDefinition; use GraphQL\Language\AST\OperationDefinition;
use GraphQL\Language\AST\SelectionSet; use GraphQL\Language\AST\SelectionSet;
use GraphQL\Language\Parser; use GraphQL\Language\Parser;
@ -25,7 +26,7 @@ class VisitorTest extends \PHPUnit_Framework_TestCase
$selectionSet = null; $selectionSet = null;
$editedAst = Visitor::visit($ast, [ $editedAst = Visitor::visit($ast, [
Node::OPERATION_DEFINITION => [ NodeType::OPERATION_DEFINITION => [
'enter' => function(OperationDefinition $node) use (&$selectionSet) { 'enter' => function(OperationDefinition $node) use (&$selectionSet) {
$selectionSet = $node->selectionSet; $selectionSet = $node->selectionSet;
@ -63,7 +64,7 @@ class VisitorTest extends \PHPUnit_Framework_TestCase
$definitions = $ast->definitions; $definitions = $ast->definitions;
$editedAst = Visitor::visit($ast, [ $editedAst = Visitor::visit($ast, [
Node::DOCUMENT => [ NodeType::DOCUMENT => [
'enter' => function (Document $node) { 'enter' => function (Document $node) {
$tmp = clone $node; $tmp = clone $node;
$tmp->definitions = []; $tmp->definitions = [];
@ -264,7 +265,7 @@ class VisitorTest extends \PHPUnit_Framework_TestCase
'leave' => function($node) use (&$visited) { 'leave' => function($node) use (&$visited) {
$visited[] = ['leave', $node->kind, isset($node->value) ? $node->value : null]; $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(); return Visitor::stop();
} }
} }
@ -297,10 +298,10 @@ class VisitorTest extends \PHPUnit_Framework_TestCase
$ast = Parser::parse('{ a, b { x }, c }'); $ast = Parser::parse('{ a, b { x }, c }');
Visitor::visit($ast, [ Visitor::visit($ast, [
Node::NAME => function(Name $node) use (&$visited) { NodeType::NAME => function(Name $node) use (&$visited) {
$visited[] = ['enter', $node->kind, $node->value]; $visited[] = ['enter', $node->kind, $node->value];
}, },
Node::SELECTION_SET => [ NodeType::SELECTION_SET => [
'enter' => function(SelectionSet $node) use (&$visited) { 'enter' => function(SelectionSet $node) use (&$visited) {
$visited[] = ['enter', $node->kind, null]; $visited[] = ['enter', $node->kind, null];
}, },

View File

@ -309,18 +309,16 @@ abstract class TestCase extends \PHPUnit_Framework_TestCase
function expectValid($schema, $rules, $queryString) function expectValid($schema, $rules, $queryString)
{ {
$parser = new Parser(new Lexer());
$this->assertEquals( $this->assertEquals(
[], [],
DocumentValidator::validate($schema, $parser->parse($queryString), $rules), DocumentValidator::validate($schema, Parser::parse($queryString), $rules),
'Should validate' 'Should validate'
); );
} }
function expectInvalid($schema, $rules, $queryString, $expectedErrors) 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->assertNotEmpty($errors, 'GraphQL should not validate');
$this->assertEquals($expectedErrors, array_map(['GraphQL\Error\Error', 'formatError'], $errors)); $this->assertEquals($expectedErrors, array_map(['GraphQL\Error\Error', 'formatError'], $errors));