diff --git a/src/Type/Definition/Config.php b/src/Type/Definition/Config.php index 024a9e5..07616a5 100644 --- a/src/Type/Definition/Config.php +++ b/src/Type/Definition/Config.php @@ -26,6 +26,7 @@ class Config const REQUIRED = 65536; const KEY_AS_NAME = 131072; const MAYBE_THUNK = 262144; + const MAYBE_TYPE = 524288; /** * @var bool @@ -155,7 +156,11 @@ class Config foreach ($value as $arrKey => $arrValue) { if (is_array($def->definition)) { - Utils::invariant(is_array($arrValue), $err, $arrKey, Utils::getVariableType($arrValue)); + if ($def->flags & self::MAYBE_TYPE) { + Utils::invariant(is_array($arrValue) || $arrValue instanceof Type, $err, $arrKey, Utils::getVariableType($arrValue)); + } else { + Utils::invariant(is_array($arrValue), $err, $arrKey, Utils::getVariableType($arrValue)); + } if ($def->flags & self::KEY_AS_NAME) { $arrValue += ['name' => $arrKey]; diff --git a/src/Type/Definition/ObjectType.php b/src/Type/Definition/ObjectType.php index 7d84199..6836e89 100644 --- a/src/Type/Definition/ObjectType.php +++ b/src/Type/Definition/ObjectType.php @@ -86,7 +86,7 @@ class ObjectType extends Type implements OutputType, CompositeType 'name' => Config::STRING | Config::REQUIRED, 'fields' => Config::arrayOf( FieldDefinition::getDefinition(), - Config::KEY_AS_NAME | Config::MAYBE_THUNK + Config::KEY_AS_NAME | Config::MAYBE_THUNK | Config::MAYBE_TYPE ), 'description' => Config::STRING, 'interfaces' => Config::arrayOf( diff --git a/tests/Executor/ExecutorTest.php b/tests/Executor/ExecutorTest.php index 85a7339..bcd6720 100644 --- a/tests/Executor/ExecutorTest.php +++ b/tests/Executor/ExecutorTest.php @@ -404,7 +404,7 @@ class ExecutorTest extends \PHPUnit_Framework_TestCase $result = Executor::execute($schema, $docAst, $data); - $this->assertEquals($expected, $result->toArray()); + $this->assertArraySubset($expected, $result->toArray()); } /** diff --git a/tests/Executor/ListsTest.php b/tests/Executor/ListsTest.php index 1588034..6ec9a59 100644 --- a/tests/Executor/ListsTest.php +++ b/tests/Executor/ListsTest.php @@ -41,7 +41,7 @@ class ListsTest extends \PHPUnit_Framework_TestCase $ast = Parser::parse('{ nest { test } }'); $result = Executor::execute($schema, $ast, $data); - $this->assertEquals($expected, $result->toArray()); + $this->assertArraySubset($expected, $result->toArray()); } // Describe: Execute: Handles list nullability diff --git a/tests/Executor/MutationsTest.php b/tests/Executor/MutationsTest.php index 5f04302..07f67be 100644 --- a/tests/Executor/MutationsTest.php +++ b/tests/Executor/MutationsTest.php @@ -114,7 +114,7 @@ class MutationsTest extends \PHPUnit_Framework_TestCase ) ] ]; - $this->assertEquals($expected, $mutationResult->toArray()); + $this->assertArraySubset($expected, $mutationResult->toArray()); } private function schema() diff --git a/tests/Executor/NonNullTest.php b/tests/Executor/NonNullTest.php index c0b67ef..74f4604 100644 --- a/tests/Executor/NonNullTest.php +++ b/tests/Executor/NonNullTest.php @@ -99,7 +99,7 @@ class NonNullTest extends \PHPUnit_Framework_TestCase ) ] ]; - $this->assertEquals($expected, Executor::execute($this->schema, $ast, $this->throwingData, null, [], 'Q')->toArray()); + $this->assertArraySubset($expected, Executor::execute($this->schema, $ast, $this->throwingData, null, [], 'Q')->toArray()); } public function testNullsASynchronouslyReturnedObjectThatContainsANonNullableFieldThatThrowsSynchronously() @@ -123,7 +123,7 @@ class NonNullTest extends \PHPUnit_Framework_TestCase FormattedError::create($this->nonNullSyncError->getMessage(), [new SourceLocation(4, 11)]) ] ]; - $this->assertEquals($expected, Executor::execute($this->schema, $ast, $this->throwingData, null, [], 'Q')->toArray()); + $this->assertArraySubset($expected, Executor::execute($this->schema, $ast, $this->throwingData, null, [], 'Q')->toArray()); } public function testNullsAComplexTreeOfNullableFieldsThatThrow() @@ -155,7 +155,7 @@ class NonNullTest extends \PHPUnit_Framework_TestCase FormattedError::create($this->syncError->getMessage(), [new SourceLocation(6, 13)]), ] ]; - $this->assertEquals($expected, Executor::execute($this->schema, $ast, $this->throwingData, null, [], 'Q')->toArray()); + $this->assertArraySubset($expected, Executor::execute($this->schema, $ast, $this->throwingData, null, [], 'Q')->toArray()); } public function testNullsANullableFieldThatSynchronouslyReturnsNull() @@ -197,7 +197,7 @@ class NonNullTest extends \PHPUnit_Framework_TestCase FormattedError::create('Cannot return null for non-nullable field DataType.nonNullSync.', [new SourceLocation(4, 11)]) ] ]; - $this->assertEquals($expected, Executor::execute($this->schema, $ast, $this->nullingData, null, [], 'Q')->toArray()); + $this->assertArraySubset($expected, Executor::execute($this->schema, $ast, $this->nullingData, null, [], 'Q')->toArray()); } public function test5() @@ -247,7 +247,7 @@ class NonNullTest extends \PHPUnit_Framework_TestCase FormattedError::create($this->nonNullSyncError->getMessage(), [new SourceLocation(2, 17)]) ] ]; - $this->assertEquals($expected, Executor::execute($this->schema, Parser::parse($doc), $this->throwingData)->toArray()); + $this->assertArraySubset($expected, Executor::execute($this->schema, Parser::parse($doc), $this->throwingData)->toArray()); } public function testNullsTheTopLevelIfSyncNonNullableFieldReturnsNull() @@ -262,6 +262,6 @@ class NonNullTest extends \PHPUnit_Framework_TestCase FormattedError::create('Cannot return null for non-nullable field DataType.nonNullSync.', [new SourceLocation(2, 17)]), ] ]; - $this->assertEquals($expected, Executor::execute($this->schema, Parser::parse($doc), $this->nullingData)->toArray()); + $this->assertArraySubset($expected, Executor::execute($this->schema, Parser::parse($doc), $this->nullingData)->toArray()); } } diff --git a/tests/Type/EnumTypeTest.php b/tests/Type/EnumTypeTest.php index e15a4ee..ef32c1c 100644 --- a/tests/Type/EnumTypeTest.php +++ b/tests/Type/EnumTypeTest.php @@ -423,7 +423,7 @@ class EnumTypeTest extends \PHPUnit_Framework_TestCase ]] ]; - $this->assertEquals($expected, $result); + $this->assertArraySubset($expected, $result); } /** @@ -443,7 +443,7 @@ class EnumTypeTest extends \PHPUnit_Framework_TestCase third: simpleEnum(fromValue: "WRONG") }'; - $this->assertEquals( + $this->assertArraySubset( [ 'data' => ['first' => 'ONE', 'second' => 'TWO', 'third' => null], 'errors' => [[