From 824b99c8c84a97516c44c996f426e6cbbe7967fb Mon Sep 17 00:00:00 2001 From: Matthias Leutenegger Date: Mon, 1 Mar 2021 09:35:10 +0100 Subject: [PATCH 01/10] Update NestedInputBuilderTest.php --- .../Services/NestedInputBuilderTest.php | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/tests/Schema/Services/NestedInputBuilderTest.php b/tests/Schema/Services/NestedInputBuilderTest.php index 88d1540bf..d501b73d0 100644 --- a/tests/Schema/Services/NestedInputBuilderTest.php +++ b/tests/Schema/Services/NestedInputBuilderTest.php @@ -75,6 +75,46 @@ public function testNestedInputBuilder() ], ], $schema); } + + /** + * @throws SchemaBuilderException + */ + public function testNestedInputBuilderBuildsCyclic() + { + $schema = (new TestSchemaBuilder())->boot('inputBuilderTest'); + $schema + ->addModelbyClassName(FakeProductPage::class, function (ModelType $model) { + $model->addField('title'); + $model->addField('products'); + $model->addAllOperations(); + }) + ->addModelbyClassName(FakeProduct::class, function (ModelType $model) { + $model->addField('title'); + $model->addField('parent'); + $model->addField('reviews'); + $model->addField('relatedProducts'); + }) + ->addModelbyClassName(FakeReview::class, function (ModelType $model) { + $model->addField('content'); + $model->addField('author'); + $model->addAllOperations(); + }) + ->addModelbyClassName(Member::class, function (ModelType $model) { + $model->addField('firstName'); + }); + $root = $schema->getModelByClassName(FakeProductPage::class); + $query = Query::create('myQuery', '[' . $root->getName() . ']'); + + $builder = NestedInputBuilder::create($query, $schema); + $builder->populateSchema(); + $this->assertSchema([ + 'FakeReviewFilterFieldsType' => [ + 'id' => 'QueryFilterIDComparator', + 'content' => 'QueryFilterStringComparator', + 'author' => 'MemberFilterFieldsType' + ] + ], $schema); + } private function assertSchema(array $graph, Schema $schema) { From ee2b896b8dfd3ccfd87551c1218044d847563a09 Mon Sep 17 00:00:00 2001 From: Matthias Leutenegger Date: Mon, 1 Mar 2021 10:54:59 +0100 Subject: [PATCH 02/10] CHANGE: test for field lists --- .../Services/NestedInputBuilderTest.php | 20 +++++++------------ 1 file changed, 7 insertions(+), 13 deletions(-) diff --git a/tests/Schema/Services/NestedInputBuilderTest.php b/tests/Schema/Services/NestedInputBuilderTest.php index d501b73d0..3c391e412 100644 --- a/tests/Schema/Services/NestedInputBuilderTest.php +++ b/tests/Schema/Services/NestedInputBuilderTest.php @@ -75,18 +75,17 @@ public function testNestedInputBuilder() ], ], $schema); } - + /** * @throws SchemaBuilderException */ - public function testNestedInputBuilderBuildsCyclic() + public function testNestedInputBuilderBuildsCyclicFilterFields() { - $schema = (new TestSchemaBuilder())->boot('inputBuilderTest'); + $schema = (new TestSchemaBuilder())->boot('filterfieldbuilder'); $schema ->addModelbyClassName(FakeProductPage::class, function (ModelType $model) { $model->addField('title'); - $model->addField('products'); - $model->addAllOperations(); + $model->addField('products', ['plugins' => ['filter' => true]]); }) ->addModelbyClassName(FakeProduct::class, function (ModelType $model) { $model->addField('title'); @@ -97,21 +96,16 @@ public function testNestedInputBuilderBuildsCyclic() ->addModelbyClassName(FakeReview::class, function (ModelType $model) { $model->addField('content'); $model->addField('author'); - $model->addAllOperations(); }) ->addModelbyClassName(Member::class, function (ModelType $model) { $model->addField('firstName'); }); - $root = $schema->getModelByClassName(FakeProductPage::class); - $query = Query::create('myQuery', '[' . $root->getName() . ']'); - - $builder = NestedInputBuilder::create($query, $schema); - $builder->populateSchema(); + $schema->createStoreableSchema(); $this->assertSchema([ - 'FakeReviewFilterFieldsType' => [ + 'FakeReviewFilterFields' => [ 'id' => 'QueryFilterIDComparator', 'content' => 'QueryFilterStringComparator', - 'author' => 'MemberFilterFieldsType' + 'author' => 'MemberFilterFields' ] ], $schema); } From 0c94cad6a23f72ca524b295f7228b608a3ae6700 Mon Sep 17 00:00:00 2001 From: Matthias Leutenegger Date: Mon, 1 Mar 2021 15:19:38 +0100 Subject: [PATCH 03/10] CHANGE: NestedInputBuilder builds all nodes --- src/Schema/Services/NestedInputBuilder.php | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/src/Schema/Services/NestedInputBuilder.php b/src/Schema/Services/NestedInputBuilder.php index 6a4f989e8..8f73944b6 100644 --- a/src/Schema/Services/NestedInputBuilder.php +++ b/src/Schema/Services/NestedInputBuilder.php @@ -123,12 +123,15 @@ public function populateSchema() * @return array * @throws SchemaBuilderException */ - protected function buildAllFieldsConfig(Type $type, int $level = 0): array - { + protected function buildAllFieldsConfig(Type $type, int $level = 0, array &$seenConnections = []): array + { $existing = $this->fetch($type->getName()); if ($existing) { return $existing; } + if (!isset($seenConnections[$type->getName()])) { + $seenConnections[$type->getName()] = []; + } $level++; $map = []; foreach ($type->getFields() as $fieldObj) { @@ -138,23 +141,25 @@ protected function buildAllFieldsConfig(Type $type, int $level = 0): array $namedType = $fieldObj->getNamedType(); $nestedType = $this->schema->getTypeOrModel($namedType); if ($nestedType) { - if ($level > $this->config()->get('max_nesting')) { - continue; - } + // if ($level > $this->config()->get('max_nesting')) { + // continue; + // } // Prevent stupid recursion in self-referential relationships, e.g. Parent if ($namedType === $type->getName()) { $map[$fieldObj->getName()] = self::SELF_REFERENTIAL; + } elseif (isset($seenConnections[$type->getName()][$fieldObj->getName()])) { + continue; } else { - $map[$fieldObj->getName()] = $this->buildAllFieldsConfig($nestedType, $level); + $seenConnections[$type->getName()][$fieldObj->getName()] = true; + $map[$fieldObj->getName()] = $this->buildAllFieldsConfig($nestedType, $level, $seenConnections); } } else { $map[$fieldObj->getName()] = true; } } $this->persist($type->getName(), $map); - return $map; - } + } /** * @param Type $type From b1320d221215d32f5366241d64a5fee73359dfe5 Mon Sep 17 00:00:00 2001 From: Matthias Leutenegger Date: Mon, 1 Mar 2021 15:36:59 +0100 Subject: [PATCH 04/10] CHANGE: Lint --- src/Schema/Services/NestedInputBuilder.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Schema/Services/NestedInputBuilder.php b/src/Schema/Services/NestedInputBuilder.php index 8f73944b6..5e9a6bb62 100644 --- a/src/Schema/Services/NestedInputBuilder.php +++ b/src/Schema/Services/NestedInputBuilder.php @@ -123,8 +123,8 @@ public function populateSchema() * @return array * @throws SchemaBuilderException */ - protected function buildAllFieldsConfig(Type $type, int $level = 0, array &$seenConnections = []): array - { + protected function buildAllFieldsConfig(Type $type, int $level = 0, array &$seenConnections = []): array + { $existing = $this->fetch($type->getName()); if ($existing) { return $existing; @@ -159,7 +159,7 @@ protected function buildAllFieldsConfig(Type $type, int $level = 0, array &$seen } $this->persist($type->getName(), $map); return $map; - } + } /** * @param Type $type From 4f4034ab9050ddaca6ad512c996c844f81096218 Mon Sep 17 00:00:00 2001 From: Matthias Leutenegger Date: Mon, 1 Mar 2021 23:27:57 +0100 Subject: [PATCH 05/10] Update NestedInputBuilder.php --- src/Schema/Services/NestedInputBuilder.php | 29 ++++++++++++---------- 1 file changed, 16 insertions(+), 13 deletions(-) diff --git a/src/Schema/Services/NestedInputBuilder.php b/src/Schema/Services/NestedInputBuilder.php index 5e9a6bb62..203e55e1b 100644 --- a/src/Schema/Services/NestedInputBuilder.php +++ b/src/Schema/Services/NestedInputBuilder.php @@ -119,20 +119,15 @@ public function populateSchema() /** * @param Type $type - * @param int $level * @return array * @throws SchemaBuilderException */ - protected function buildAllFieldsConfig(Type $type, int $level = 0, array &$seenConnections = []): array + protected function buildAllFieldsConfig(Type $type): array { $existing = $this->fetch($type->getName()); if ($existing) { return $existing; } - if (!isset($seenConnections[$type->getName()])) { - $seenConnections[$type->getName()] = []; - } - $level++; $map = []; foreach ($type->getFields() as $fieldObj) { if (!$this->shouldAddField($type, $fieldObj)) { @@ -141,17 +136,25 @@ protected function buildAllFieldsConfig(Type $type, int $level = 0, array &$seen $namedType = $fieldObj->getNamedType(); $nestedType = $this->schema->getTypeOrModel($namedType); if ($nestedType) { - // if ($level > $this->config()->get('max_nesting')) { - // continue; - // } + $seen = $this->schema->getState()->get([ + static::class, + 'seenConnections', + $type->getName(), + $fieldObj->getName() + ]); // Prevent stupid recursion in self-referential relationships, e.g. Parent if ($namedType === $type->getName()) { $map[$fieldObj->getName()] = self::SELF_REFERENTIAL; - } elseif (isset($seenConnections[$type->getName()][$fieldObj->getName()])) { + } elseif ($seen) { continue; } else { - $seenConnections[$type->getName()][$fieldObj->getName()] = true; - $map[$fieldObj->getName()] = $this->buildAllFieldsConfig($nestedType, $level, $seenConnections); + $this->schema->getState()->set([ + static::class, + 'seenConnections', + $type->getName(), + $fieldObj->getName() + ], true); + $map[$fieldObj->getName()] = $this->buildAllFieldsConfig($nestedType); } } else { $map[$fieldObj->getName()] = true; @@ -159,7 +162,7 @@ protected function buildAllFieldsConfig(Type $type, int $level = 0, array &$seen } $this->persist($type->getName(), $map); return $map; - } + } /** * @param Type $type From 84d3f3cf807cfe40b6a38d8ae288a38a813dcefe Mon Sep 17 00:00:00 2001 From: Matthias Leutenegger <2-mleutenegger@users.noreply.git.syntro.ch> Date: Tue, 2 Mar 2021 17:35:10 +0100 Subject: [PATCH 06/10] DELETE: max_nesting config on NestedInputBuilder --- src/Schema/Services/NestedInputBuilder.php | 6 ------ 1 file changed, 6 deletions(-) diff --git a/src/Schema/Services/NestedInputBuilder.php b/src/Schema/Services/NestedInputBuilder.php index 203e55e1b..b11c791d5 100644 --- a/src/Schema/Services/NestedInputBuilder.php +++ b/src/Schema/Services/NestedInputBuilder.php @@ -26,12 +26,6 @@ class NestedInputBuilder const SELF_REFERENTIAL = '--self--'; - /** - * @var int - * @config - */ - private static $max_nesting = 3; - /** * @var string * @config From 7941f5f7880e44b49d581819d4556892c766decc Mon Sep 17 00:00:00 2001 From: Matthias Leutenegger <2-mleutenegger@users.noreply.git.syntro.ch> Date: Tue, 2 Mar 2021 17:40:18 +0100 Subject: [PATCH 07/10] CHANGE: test only checks for type --- tests/Schema/Services/NestedInputBuilderTest.php | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/tests/Schema/Services/NestedInputBuilderTest.php b/tests/Schema/Services/NestedInputBuilderTest.php index 3c391e412..4a6bd1880 100644 --- a/tests/Schema/Services/NestedInputBuilderTest.php +++ b/tests/Schema/Services/NestedInputBuilderTest.php @@ -101,13 +101,11 @@ public function testNestedInputBuilderBuildsCyclicFilterFields() $model->addField('firstName'); }); $schema->createStoreableSchema(); - $this->assertSchema([ - 'FakeReviewFilterFields' => [ - 'id' => 'QueryFilterIDComparator', - 'content' => 'QueryFilterStringComparator', - 'author' => 'MemberFilterFields' - ] - ], $schema); + $filterType = $schema->getType('FakeReviewFilterFields'); + $this->assertNotNull($filterType, "Type FakeReviewFilterFields not found in schema"); + $filterFieldObj = $filterType->getFieldByName('author'); + $this->assertNotNull($filterFieldObj, "Field author not found on {$filterType->getName()}"); + $this->assertEquals('MemberFilterFields', $fieldObj->filterFieldObj()); } private function assertSchema(array $graph, Schema $schema) From 2a64f8a83675cf4add0f6256aec573614eba3756 Mon Sep 17 00:00:00 2001 From: Matthias Leutenegger <2-mleutenegger@users.noreply.git.syntro.ch> Date: Tue, 2 Mar 2021 17:48:48 +0100 Subject: [PATCH 08/10] FIX: test --- tests/Schema/Services/NestedInputBuilderTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Schema/Services/NestedInputBuilderTest.php b/tests/Schema/Services/NestedInputBuilderTest.php index 4a6bd1880..a4b2dcecc 100644 --- a/tests/Schema/Services/NestedInputBuilderTest.php +++ b/tests/Schema/Services/NestedInputBuilderTest.php @@ -105,7 +105,7 @@ public function testNestedInputBuilderBuildsCyclicFilterFields() $this->assertNotNull($filterType, "Type FakeReviewFilterFields not found in schema"); $filterFieldObj = $filterType->getFieldByName('author'); $this->assertNotNull($filterFieldObj, "Field author not found on {$filterType->getName()}"); - $this->assertEquals('MemberFilterFields', $fieldObj->filterFieldObj()); + $this->assertEquals('MemberFilterFields', $filterFieldObj->filterFieldObj()); } private function assertSchema(array $graph, Schema $schema) From 93a987d32d3c8f7c80097840af9d170ba894c562 Mon Sep 17 00:00:00 2001 From: Matthias Leutenegger <2-mleutenegger@users.noreply.git.syntro.ch> Date: Tue, 2 Mar 2021 17:58:18 +0100 Subject: [PATCH 09/10] fix --- tests/Schema/Services/NestedInputBuilderTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Schema/Services/NestedInputBuilderTest.php b/tests/Schema/Services/NestedInputBuilderTest.php index a4b2dcecc..ed7bb0408 100644 --- a/tests/Schema/Services/NestedInputBuilderTest.php +++ b/tests/Schema/Services/NestedInputBuilderTest.php @@ -105,7 +105,7 @@ public function testNestedInputBuilderBuildsCyclicFilterFields() $this->assertNotNull($filterType, "Type FakeReviewFilterFields not found in schema"); $filterFieldObj = $filterType->getFieldByName('author'); $this->assertNotNull($filterFieldObj, "Field author not found on {$filterType->getName()}"); - $this->assertEquals('MemberFilterFields', $filterFieldObj->filterFieldObj()); + $this->assertEquals('MemberFilterFields', $filterFieldObj->getType()); } private function assertSchema(array $graph, Schema $schema) From eab1deb3e32c33ce2b1f8f94eab301e2d299c57e Mon Sep 17 00:00:00 2001 From: Matthias Leutenegger <2-mleutenegger@users.noreply.git.syntro.ch> Date: Tue, 2 Mar 2021 18:11:02 +0100 Subject: [PATCH 10/10] lint --- src/Schema/Services/NestedInputBuilder.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Schema/Services/NestedInputBuilder.php b/src/Schema/Services/NestedInputBuilder.php index b11c791d5..cd8dc2d99 100644 --- a/src/Schema/Services/NestedInputBuilder.php +++ b/src/Schema/Services/NestedInputBuilder.php @@ -156,7 +156,7 @@ protected function buildAllFieldsConfig(Type $type): array } $this->persist($type->getName(), $map); return $map; - } + } /** * @param Type $type