Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FIX: input building in cyclic graphs #371

Merged
merged 10 commits into from
Mar 3, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 16 additions & 14 deletions src/Schema/Services/NestedInputBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,6 @@ class NestedInputBuilder

const SELF_REFERENTIAL = '--self--';

/**
* @var int
* @config
*/
private static $max_nesting = 3;

/**
* @var string
* @config
Expand Down Expand Up @@ -119,17 +113,15 @@ public function populateSchema()

/**
* @param Type $type
* @param int $level
* @return array
* @throws SchemaBuilderException
*/
protected function buildAllFieldsConfig(Type $type, int $level = 0): array
protected function buildAllFieldsConfig(Type $type): array
{
$existing = $this->fetch($type->getName());
if ($existing) {
return $existing;
}
$level++;
$map = [];
foreach ($type->getFields() as $fieldObj) {
if (!$this->shouldAddField($type, $fieldObj)) {
Expand All @@ -138,21 +130,31 @@ 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;
}
$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 ($seen) {
continue;
} else {
$map[$fieldObj->getName()] = $this->buildAllFieldsConfig($nestedType, $level);
$this->schema->getState()->set([
static::class,
'seenConnections',
$type->getName(),
$fieldObj->getName()
], true);
$map[$fieldObj->getName()] = $this->buildAllFieldsConfig($nestedType);
}
} else {
$map[$fieldObj->getName()] = true;
}
}
$this->persist($type->getName(), $map);

return $map;
}

Expand Down
32 changes: 32 additions & 0 deletions tests/Schema/Services/NestedInputBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,38 @@ public function testNestedInputBuilder()
], $schema);
}

/**
* @throws SchemaBuilderException
*/
public function testNestedInputBuilderBuildsCyclicFilterFields()
{
$schema = (new TestSchemaBuilder())->boot('filterfieldbuilder');
$schema
->addModelbyClassName(FakeProductPage::class, function (ModelType $model) {
$model->addField('title');
$model->addField('products', ['plugins' => ['filter' => true]]);
})
->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');
})
->addModelbyClassName(Member::class, function (ModelType $model) {
$model->addField('firstName');
});
$schema->createStoreableSchema();
$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', $filterFieldObj->getType());
}

private function assertSchema(array $graph, Schema $schema)
{
foreach ($graph as $typeName => $fields) {
Expand Down