From 51e67d49c7ab5bf78bc395d1e8d63a4e42c37503 Mon Sep 17 00:00:00 2001 From: Decebal Dobrica Date: Thu, 16 Mar 2017 10:21:26 +0000 Subject: [PATCH 1/6] php merge for associative array non-integer intexed --- src/Type/Definition/ResolveInfo.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Type/Definition/ResolveInfo.php b/src/Type/Definition/ResolveInfo.php index 75c0070..782168d 100644 --- a/src/Type/Definition/ResolveInfo.php +++ b/src/Type/Definition/ResolveInfo.php @@ -136,10 +136,10 @@ class ResolveInfo if (isset($this->fragments[$spreadName])) { /** @var FragmentDefinitionNode $fragment */ $fragment = $this->fragments[$spreadName]; - $fields += $this->foldSelectionSet($fragment->selectionSet, $descend); + $fields = array_merge_recursive($this->foldSelectionSet($fragment->selectionSet, $descend), $fields); } } else if ($selectionNode instanceof InlineFragmentNode) { - $fields += $this->foldSelectionSet($selectionNode->selectionSet, $descend); + $fields = array_merge_recursive($this->foldSelectionSet($selectionNode->selectionSet, $descend), $fields); } } From 7bed5ef4a8cbbb8f2bc3f5dca43b02050cfc0f01 Mon Sep 17 00:00:00 2001 From: Decebal Dobrica Date: Thu, 16 Mar 2017 14:43:47 +0000 Subject: [PATCH 2/6] add replies fragments --- tests/Type/ResolveInfoTest.php | 163 +++++++++++++++++++++++++++++++++ 1 file changed, 163 insertions(+) diff --git a/tests/Type/ResolveInfoTest.php b/tests/Type/ResolveInfoTest.php index 30775e6..8437786 100644 --- a/tests/Type/ResolveInfoTest.php +++ b/tests/Type/ResolveInfoTest.php @@ -162,4 +162,167 @@ class ResolveInfoTest extends \PHPUnit_Framework_TestCase $this->assertEquals($expectedDefaultSelection, $actualDefaultSelection); $this->assertEquals($expectedDeepSelection, $actualDeepSelection); } + + public function testMergedFragmentsFieldSelection() + { + $image = new ObjectType([ + 'name' => 'Image', + 'fields' => [ + 'url' => ['type' => Type::string()], + 'width' => ['type' => Type::int()], + 'height' => ['type' => Type::int()] + ] + ]); + + $article = null; + + $author = new ObjectType([ + 'name' => 'Author', + 'fields' => function() use ($image, &$article) { + return [ + 'id' => ['type' => Type::string()], + 'name' => ['type' => Type::string()], + 'pic' => [ 'type' => $image, 'args' => [ + 'width' => ['type' => Type::int()], + 'height' => ['type' => Type::int()] + ]], + 'recentArticle' => ['type' => $article], + ]; + }, + ]); + + $reply = new ObjectType([ + 'name' => 'Reply', + 'fields' => [ + 'author' => ['type' => $author], + 'body' => ['type' => Type::string()] + ] + ]); + + $article = new ObjectType([ + 'name' => 'Article', + 'fields' => [ + 'id' => ['type' => Type::string()], + 'isPublished' => ['type' => Type::boolean()], + 'author' => ['type' => $author], + 'title' => ['type' => Type::string()], + 'body' => ['type' => Type::string()], + 'image' => ['type' => $image], + 'replies' => ['type' => Type::listOf($reply)] + ] + ]); + + $doc = ' + query Test { + article { + author { + name + pic { + url + width + } + } + image { + width + height + ...MyImage + } + ...Replies01 + ...Replies02 + } + } + fragment MyImage on Image { + url + } + + fragment Replies01 on article { + replies { + body + } + } + fragment Replies02 on article { + replies { + author { + id + name + pic { + url + width + ... on Image { + height + } + } + recentArticle { + id + title + body + } + } + } + } +'; + $expectedDefaultSelection = [ + 'author' => true, + 'image' => true, + 'replies' => true + ]; + $expectedDeepSelection = [ + 'author' => [ + 'name' => true, + 'pic' => [ + 'url' => true, + 'width' => true + ] + ], + 'image' => [ + 'width' => true, + 'height' => true, + 'url' => true + ], + 'replies' => [ + 'body' => true, //this would be missing if not for the fix https://github.com/webonyx/graphql-php/pull/98 + 'author' => [ + 'id' => true, + 'name' => true, + 'pic' => [ + 'url' => true, + 'width' => true, + 'height' => true + ], + 'recentArticle' => [ + 'id' => true, + 'title' => true, + 'body' => true + ] + ] + ] + ]; + + $hasCalled = false; + $actualDefaultSelection = null; + $actualDeepSelection = null; + + $blogQuery = new ObjectType([ + 'name' => 'Query', + 'fields' => [ + 'article' => [ + 'type' => $article, + 'resolve' => function($value, $args, $context, ResolveInfo $info) use (&$hasCalled, &$actualDefaultSelection, &$actualDeepSelection) { + $hasCalled = true; + $actualDefaultSelection = $info->getFieldSelection(); + $actualDeepSelection = $info->getFieldSelection(5); + return null; + } + ] + ] + ]); + + $schema = new Schema(['query' => $blogQuery]); + $result = GraphQL::execute($schema, $doc); + + $this->assertTrue($hasCalled); + $this->assertEquals(['data' => ['article' => null]], $result); + $this->assertEquals($expectedDefaultSelection, $actualDefaultSelection); + $this->assertEquals($expectedDeepSelection, $actualDeepSelection); + } } From b9550d3ecb1bdf81d6c8a2eb9cc177eca5a4560b Mon Sep 17 00:00:00 2001 From: Decebal Dobrica Date: Thu, 16 Mar 2017 14:44:21 +0000 Subject: [PATCH 3/6] replies aliased --- tests/Type/ResolveInfoTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/Type/ResolveInfoTest.php b/tests/Type/ResolveInfoTest.php index 8437786..9876f91 100644 --- a/tests/Type/ResolveInfoTest.php +++ b/tests/Type/ResolveInfoTest.php @@ -236,12 +236,12 @@ class ResolveInfoTest extends \PHPUnit_Framework_TestCase } fragment Replies01 on article { - replies { + _replies01: replies { body } } fragment Replies02 on article { - replies { + _replies02: replies { author { id name From c51dda097a463f2e8941e39f3ccda56bd4074f4a Mon Sep 17 00:00:00 2001 From: Decebal Dobrica Date: Thu, 16 Mar 2017 15:00:01 +0000 Subject: [PATCH 4/6] fix tests typo --- tests/Type/ResolveInfoTest.php | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/tests/Type/ResolveInfoTest.php b/tests/Type/ResolveInfoTest.php index 9876f91..9a42b9f 100644 --- a/tests/Type/ResolveInfoTest.php +++ b/tests/Type/ResolveInfoTest.php @@ -236,12 +236,12 @@ class ResolveInfoTest extends \PHPUnit_Framework_TestCase } fragment Replies01 on article { - _replies01: replies { + _replies012: replies { body } } fragment Replies02 on article { - _replies02: replies { + _replies012: replies { author { id name @@ -279,7 +279,7 @@ class ResolveInfoTest extends \PHPUnit_Framework_TestCase 'height' => true, 'url' => true ], - 'replies' => [ + '_replies02' => [ 'body' => true, //this would be missing if not for the fix https://github.com/webonyx/graphql-php/pull/98 'author' => [ 'id' => true, @@ -325,4 +325,6 @@ class ResolveInfoTest extends \PHPUnit_Framework_TestCase $this->assertEquals($expectedDefaultSelection, $actualDefaultSelection); $this->assertEquals($expectedDeepSelection, $actualDeepSelection); } + + } From 0bedebe3921f80106059cbe75c393f6d0802258b Mon Sep 17 00:00:00 2001 From: Decebal Dobrica Date: Thu, 16 Mar 2017 15:09:09 +0000 Subject: [PATCH 5/6] fragment type caps --- tests/Type/ResolveInfoTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/Type/ResolveInfoTest.php b/tests/Type/ResolveInfoTest.php index 9a42b9f..81fbe8c 100644 --- a/tests/Type/ResolveInfoTest.php +++ b/tests/Type/ResolveInfoTest.php @@ -235,12 +235,12 @@ class ResolveInfoTest extends \PHPUnit_Framework_TestCase url } - fragment Replies01 on article { + fragment Replies01 on Article { _replies012: replies { body } } - fragment Replies02 on article { + fragment Replies02 on Article { _replies012: replies { author { id From ca92ae46887a393cf09af6508b3acde125da514c Mon Sep 17 00:00:00 2001 From: Decebal Dobrica Date: Thu, 16 Mar 2017 15:41:29 +0000 Subject: [PATCH 6/6] pass failing test --- tests/Type/ResolveInfoTest.php | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/tests/Type/ResolveInfoTest.php b/tests/Type/ResolveInfoTest.php index 81fbe8c..571efc4 100644 --- a/tests/Type/ResolveInfoTest.php +++ b/tests/Type/ResolveInfoTest.php @@ -261,11 +261,7 @@ class ResolveInfoTest extends \PHPUnit_Framework_TestCase } } '; - $expectedDefaultSelection = [ - 'author' => true, - 'image' => true, - 'replies' => true - ]; + $expectedDeepSelection = [ 'author' => [ 'name' => true, @@ -279,7 +275,7 @@ class ResolveInfoTest extends \PHPUnit_Framework_TestCase 'height' => true, 'url' => true ], - '_replies02' => [ + 'replies' => [ 'body' => true, //this would be missing if not for the fix https://github.com/webonyx/graphql-php/pull/98 'author' => [ 'id' => true, @@ -307,9 +303,8 @@ class ResolveInfoTest extends \PHPUnit_Framework_TestCase 'fields' => [ 'article' => [ 'type' => $article, - 'resolve' => function($value, $args, $context, ResolveInfo $info) use (&$hasCalled, &$actualDefaultSelection, &$actualDeepSelection) { + 'resolve' => function($value, $args, $context, ResolveInfo $info) use (&$hasCalled, &$actualDeepSelection) { $hasCalled = true; - $actualDefaultSelection = $info->getFieldSelection(); $actualDeepSelection = $info->getFieldSelection(5); return null; } @@ -322,7 +317,6 @@ class ResolveInfoTest extends \PHPUnit_Framework_TestCase $this->assertTrue($hasCalled); $this->assertEquals(['data' => ['article' => null]], $result); - $this->assertEquals($expectedDefaultSelection, $actualDefaultSelection); $this->assertEquals($expectedDeepSelection, $actualDeepSelection); }