Add more BuildSchema tests + cleanup

This commit is contained in:
Vladimir Razuvaev 2018-08-22 16:14:47 +07:00
parent f123e5c954
commit 804daa188e

View File

@ -32,7 +32,6 @@ class BuildSchemaTest extends TestCase
public function testUseBuiltSchemaForLimitedExecution() public function testUseBuiltSchemaForLimitedExecution()
{ {
$schema = BuildSchema::buildAST(Parser::parse(' $schema = BuildSchema::buildAST(Parser::parse('
schema { query: Query }
type Query { type Query {
str: String str: String
} }
@ -48,20 +47,21 @@ class BuildSchemaTest extends TestCase
public function testBuildSchemaDirectlyFromSource() public function testBuildSchemaDirectlyFromSource()
{ {
$schema = BuildSchema::build(" $schema = BuildSchema::build("
schema { query: Query }
type Query { type Query {
add(x: Int, y: Int): Int add(x: Int, y: Int): Int
} }
"); ");
$root = [
'add' => function ($root, $args) {
return $args['x'] + $args['y'];
}
];
$result = GraphQL::executeQuery( $result = GraphQL::executeQuery(
$schema, $schema,
'{ add(x: 34, y: 55) }', '{ add(x: 34, y: 55) }',
[ $root
'add' => function ($root, $args) {
return $args['x'] + $args['y'];
}
]
); );
$this->assertEquals(['data' => ['add' => 89]], $result->toArray(true)); $this->assertEquals(['data' => ['add' => 89]], $result->toArray(true));
} }
@ -72,10 +72,6 @@ class BuildSchemaTest extends TestCase
public function testSimpleType() public function testSimpleType()
{ {
$body = ' $body = '
schema {
query: HelloScalars
}
type HelloScalars { type HelloScalars {
str: String str: String
int: Int int: Int
@ -94,13 +90,9 @@ type HelloScalars {
public function testWithDirectives() public function testWithDirectives()
{ {
$body = ' $body = '
schema {
query: Hello
}
directive @foo(arg: Int) on FIELD directive @foo(arg: Int) on FIELD
type Hello { type Query {
str: String str: String
} }
'; ';
@ -114,10 +106,6 @@ type Hello {
public function testSupportsDescriptions() public function testSupportsDescriptions()
{ {
$body = ' $body = '
schema {
query: Hello
}
"""This is a directive""" """This is a directive"""
directive @foo( directive @foo(
"""It has an argument""" """It has an argument"""
@ -134,7 +122,7 @@ enum Color {
} }
"""What a great type""" """What a great type"""
type Hello { type Query {
"""And a field to boot""" """And a field to boot"""
str: String str: String
} }
@ -145,15 +133,11 @@ type Hello {
} }
/** /**
* @it Supports descriptions * @it Supports option for comment descriptions
*/ */
public function testSupportsOptionForCommentDescriptions() public function testSupportsOptionForCommentDescriptions()
{ {
$body = ' $body = '
schema {
query: Hello
}
# This is a directive # This is a directive
directive @foo( directive @foo(
# It has an argument # It has an argument
@ -170,7 +154,7 @@ enum Color {
} }
# What a great type # What a great type
type Hello { type Query {
# And a field to boot # And a field to boot
str: String str: String
} }
@ -185,11 +169,7 @@ type Hello {
public function testMaintainsSkipAndInclude() public function testMaintainsSkipAndInclude()
{ {
$body = ' $body = '
schema { type Query {
query: Hello
}
type Hello {
str: String str: String
} }
'; ';
@ -206,15 +186,11 @@ type Hello {
public function testOverridingDirectivesExcludesSpecified() public function testOverridingDirectivesExcludesSpecified()
{ {
$body = ' $body = '
schema {
query: Hello
}
directive @skip on FIELD directive @skip on FIELD
directive @include on FIELD directive @include on FIELD
directive @deprecated on FIELD_DEFINITION directive @deprecated on FIELD_DEFINITION
type Hello { type Query {
str: String str: String
} }
'; ';
@ -225,16 +201,31 @@ type Hello {
$this->assertNotEquals($schema->getDirective('deprecated'), Directive::deprecatedDirective()); $this->assertNotEquals($schema->getDirective('deprecated'), Directive::deprecatedDirective());
} }
/**
* @it Adding directives maintains @skip & @include
*/
public function testAddingDirectivesMaintainsSkipAndInclude()
{
$body = '
directive @foo(arg: Int) on FIELD
type Query {
str: String
}
';
$schema = BuildSchema::buildAST(Parser::parse($body));
$this->assertCount(4, $schema->getDirectives());
$this->assertNotEquals(null, $schema->getDirective('skip'));
$this->assertNotEquals(null, $schema->getDirective('include'));
$this->assertNotEquals(null, $schema->getDirective('deprecated'));
}
/** /**
* @it Type modifiers * @it Type modifiers
*/ */
public function testTypeModifiers() public function testTypeModifiers()
{ {
$body = ' $body = '
schema {
query: HelloScalars
}
type HelloScalars { type HelloScalars {
nonNullStr: String! nonNullStr: String!
listOfStrs: [String] listOfStrs: [String]
@ -253,13 +244,9 @@ type HelloScalars {
public function testRecursiveType() public function testRecursiveType()
{ {
$body = ' $body = '
schema { type Query {
query: Recurse
}
type Recurse {
str: String str: String
recurse: Recurse recurse: Query
} }
'; ';
$output = $this->cycleOutput($body); $output = $this->cycleOutput($body);
@ -296,11 +283,7 @@ type TypeTwo {
public function testSingleArgumentField() public function testSingleArgumentField()
{ {
$body = ' $body = '
schema { type Query {
query: Hello
}
type Hello {
str(int: Int): String str(int: Int): String
floatToStr(float: Float): String floatToStr(float: Float): String
idToStr(id: ID): String idToStr(id: ID): String
@ -318,11 +301,7 @@ type Hello {
public function testSimpleTypeWithMultipleArguments() public function testSimpleTypeWithMultipleArguments()
{ {
$body = ' $body = '
schema { type Query {
query: Hello
}
type Hello {
str(int: Int, bool: Boolean): String str(int: Int, bool: Boolean): String
} }
'; ';
@ -336,11 +315,7 @@ type Hello {
public function testSimpleTypeWithInterface() public function testSimpleTypeWithInterface()
{ {
$body = ' $body = '
schema { type Query implements WorldInterface {
query: Hello
}
type Hello implements WorldInterface {
str: String str: String
} }
@ -358,15 +333,11 @@ interface WorldInterface {
public function testSimpleOutputEnum() public function testSimpleOutputEnum()
{ {
$body = ' $body = '
schema {
query: OutputEnumRoot
}
enum Hello { enum Hello {
WORLD WORLD
} }
type OutputEnumRoot { type Query {
hello: Hello hello: Hello
} }
'; ';
@ -374,22 +345,36 @@ type OutputEnumRoot {
$this->assertEquals($output, $body); $this->assertEquals($output, $body);
} }
/**
* @it Simple input enum
*/
public function testSimpleInputEnum()
{
$body = '
enum Hello {
WORLD
}
type Query {
str(hello: Hello): String
}
';
$output = $this->cycleOutput($body);
$this->assertEquals($body, $output);
}
/** /**
* @it Multiple value enum * @it Multiple value enum
*/ */
public function testMultipleValueEnum() public function testMultipleValueEnum()
{ {
$body = ' $body = '
schema {
query: OutputEnumRoot
}
enum Hello { enum Hello {
WO WO
RLD RLD
} }
type OutputEnumRoot { type Query {
hello: Hello hello: Hello
} }
'; ';
@ -403,13 +388,9 @@ type OutputEnumRoot {
public function testSimpleUnion() public function testSimpleUnion()
{ {
$body = ' $body = '
schema {
query: Root
}
union Hello = World union Hello = World
type Root { type Query {
hello: Hello hello: Hello
} }
@ -427,13 +408,9 @@ type World {
public function testMultipleUnion() public function testMultipleUnion()
{ {
$body = ' $body = '
schema {
query: Root
}
union Hello = WorldOne | WorldTwo union Hello = WorldOne | WorldTwo
type Root { type Query {
hello: Hello hello: Hello
} }
@ -455,11 +432,7 @@ type WorldTwo {
public function testSpecifyingUnionTypeUsingTypename() public function testSpecifyingUnionTypeUsingTypename()
{ {
$schema = BuildSchema::buildAST(Parser::parse(' $schema = BuildSchema::buildAST(Parser::parse('
schema { type Query {
query: Root
}
type Root {
fruits: [Fruit] fruits: [Fruit]
} }
@ -516,11 +489,7 @@ type WorldTwo {
public function testSpecifyingInterfaceUsingTypename() public function testSpecifyingInterfaceUsingTypename()
{ {
$schema = BuildSchema::buildAST(Parser::parse(' $schema = BuildSchema::buildAST(Parser::parse('
schema { type Query {
query: Root
}
type Root {
characters: [Character] characters: [Character]
} }
@ -579,18 +548,14 @@ type WorldTwo {
} }
/** /**
* @it CustomScalar * @it Custom Scalar
*/ */
public function testCustomScalar() public function testCustomScalar()
{ {
$body = ' $body = '
schema {
query: Root
}
scalar CustomScalar scalar CustomScalar
type Root { type Query {
customScalar: CustomScalar customScalar: CustomScalar
} }
'; ';
@ -599,20 +564,16 @@ type Root {
} }
/** /**
* @it CustomScalar * @it Input Object
*/ */
public function testInputObject() public function testInputObject()
{ {
$body = ' $body = '
schema {
query: Root
}
input Input { input Input {
int: Int int: Int
} }
type Root { type Query {
field(in: Input): String field(in: Input): String
} }
'; ';
@ -626,11 +587,7 @@ type Root {
public function testSimpleArgumentFieldWithDefault() public function testSimpleArgumentFieldWithDefault()
{ {
$body = ' $body = '
schema { type Query {
query: Hello
}
type Hello {
str(int: Int = 2): String str(int: Int = 2): String
} }
'; ';
@ -644,13 +601,9 @@ type Hello {
public function testCustomScalarArgumentFieldWithDefault() public function testCustomScalarArgumentFieldWithDefault()
{ {
$body = ' $body = '
schema {
query: Hello
}
scalar CustomScalar scalar CustomScalar
type Hello { type Query {
str(int: CustomScalar = 2): String str(int: CustomScalar = 2): String
} }
'; ';
@ -801,8 +754,7 @@ type Query {
*/ */
public function testCorrectlyAssignASTNodes() public function testCorrectlyAssignASTNodes()
{ {
$schemaAST = Parser::parse('
$schema = BuildSchema::build('
schema { schema {
query: Query query: Query
} }
@ -829,8 +781,11 @@ type Query {
interfaceField: String interfaceField: String
} }
directive @test(arg: Int) on FIELD scalar TestScalar
directive @test(arg: TestScalar) on FIELD
'); ');
$schema = BuildSchema::buildAST($schemaAST);
/** @var ObjectType $query */ /** @var ObjectType $query */
$query = $schema->getType('Query'); $query = $schema->getType('Query');
$testInput = $schema->getType('TestInput'); $testInput = $schema->getType('TestInput');
@ -838,6 +793,7 @@ type Query {
$testUnion = $schema->getType('TestUnion'); $testUnion = $schema->getType('TestUnion');
$testInterface = $schema->getType('TestInterface'); $testInterface = $schema->getType('TestInterface');
$testType = $schema->getType('TestType'); $testType = $schema->getType('TestType');
$testScalar = $schema->getType('TestScalar');
$testDirective = $schema->getDirective('test'); $testDirective = $schema->getDirective('test');
$restoredIDL = SchemaPrinter::doPrint(BuildSchema::build( $restoredIDL = SchemaPrinter::doPrint(BuildSchema::build(
@ -848,6 +804,7 @@ type Query {
Printer::doPrint($testUnion->astNode) . "\n" . Printer::doPrint($testUnion->astNode) . "\n" .
Printer::doPrint($testInterface->astNode) . "\n" . Printer::doPrint($testInterface->astNode) . "\n" .
Printer::doPrint($testType->astNode) . "\n" . Printer::doPrint($testType->astNode) . "\n" .
Printer::doPrint($testScalar->astNode) . "\n" .
Printer::doPrint($testDirective->astNode) Printer::doPrint($testDirective->astNode)
)); ));
@ -860,7 +817,58 @@ type Query {
$this->assertEquals('TEST_VALUE', Printer::doPrint($testEnum->getValue('TEST_VALUE')->astNode)); $this->assertEquals('TEST_VALUE', Printer::doPrint($testEnum->getValue('TEST_VALUE')->astNode));
$this->assertEquals('interfaceField: String', Printer::doPrint($testInterface->getField('interfaceField')->astNode)); $this->assertEquals('interfaceField: String', Printer::doPrint($testInterface->getField('interfaceField')->astNode));
$this->assertEquals('interfaceField: String', Printer::doPrint($testType->getField('interfaceField')->astNode)); $this->assertEquals('interfaceField: String', Printer::doPrint($testType->getField('interfaceField')->astNode));
$this->assertEquals('arg: Int', Printer::doPrint($testDirective->args[0]->astNode)); $this->assertEquals('arg: TestScalar', Printer::doPrint($testDirective->args[0]->astNode));
}
/**
* @it Root operation types with custom names
*/
public function testRootOperationTypesWithCustomNames()
{
$schema = BuildSchema::build('
schema {
query: SomeQuery
mutation: SomeMutation
subscription: SomeSubscription
}
type SomeQuery { str: String }
type SomeMutation { str: String }
type SomeSubscription { str: String }
');
$this->assertEquals('SomeQuery', $schema->getQueryType()->name);
$this->assertEquals('SomeMutation', $schema->getMutationType()->name);
$this->assertEquals('SomeSubscription', $schema->getSubscriptionType()->name);
}
/**
* @it Default root operation type names
*/
public function testDefaultRootOperationTypeNames()
{
$schema = BuildSchema::build('
type Query { str: String }
type Mutation { str: String }
type Subscription { str: String }
');
$this->assertEquals('Query', $schema->getQueryType()->name);
$this->assertEquals('Mutation', $schema->getMutationType()->name);
$this->assertEquals('Subscription', $schema->getSubscriptionType()->name);
}
/**
* @it can build invalid schema
*/
public function testCanBuildInvalidSchema()
{
$schema = BuildSchema::build('
# Invalid schema, because it is missing query root type
type Mutation {
str: String
}
');
$errors = $schema->validate();
$this->assertGreaterThan(0, $errors);
} }
// Describe: Failures // Describe: Failures
@ -995,11 +1003,7 @@ type Hello {
$this->expectException(Error::class); $this->expectException(Error::class);
$this->expectExceptionMessage('Type "Bar" not found in document.'); $this->expectExceptionMessage('Type "Bar" not found in document.');
$body = ' $body = '
schema { type Query implements Bar {
query: Hello
}
type Hello implements Bar {
field: String field: String
} }
'; ';
@ -1016,12 +1020,8 @@ type Hello implements Bar {
$this->expectException(Error::class); $this->expectException(Error::class);
$this->expectExceptionMessage('Type "Bar" not found in document.'); $this->expectExceptionMessage('Type "Bar" not found in document.');
$body = ' $body = '
schema {
query: Hello
}
union TestUnion = Bar union TestUnion = Bar
type Hello { testUnion: TestUnion } type Query { testUnion: TestUnion }
'; ';
$doc = Parser::parse($body); $doc = Parser::parse($body);
$schema = BuildSchema::buildAST($doc); $schema = BuildSchema::buildAST($doc);