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: NestedInputBuilder correctly builds filters for models with multiple relations of the same type #363

Merged
merged 16 commits into from
Mar 4, 2021
4 changes: 4 additions & 0 deletions src/Schema/Services/NestedInputBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,10 @@ protected function addInputTypesToSchema(
}
// If we've already seen this input, it can only be added to, not mutated.
if ($inputType->getFieldByName($fieldName)) {
if ($inputType->exists() && $parentType && $parentField) {
$this->schema->addType($inputType);
$parentType->addField($parentField, $inputType->getName());
}
continue;
}

Expand Down
1 change: 1 addition & 0 deletions tests/Fake/FakeProduct.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class FakeProduct extends DataObject implements TestOnly

private static $has_one = [
'Parent' => FakeProductPage::class,
'FeaturedOn' => FakeProductPage::class,
];

private static $has_many = [
Expand Down
3 changes: 2 additions & 1 deletion tests/Fake/FakeProductPage.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ class FakeProductPage extends DataObject implements TestOnly
private static $table_name = 'FakeProductPage_Test';

private static $has_many = [
'Products' => FakeProduct::class,
'Products' => FakeProduct::class . '.Parent',
'FeaturedProducts' => FakeProduct::class . '.FeaturedOn',
];

private static $owns = [
Expand Down
28 changes: 28 additions & 0 deletions tests/Schema/Services/NestedInputBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,34 @@ public function testNestedInputBuilderBuildsCyclicFilterFields()
$this->assertEquals('MemberFilterFields', $filterFieldObj->getType());
}

/**
* @throws SchemaBuilderException
*/
public function testNestedInputBuilderBuildsRepeatedRelationsFilter()
{
$schema = (new TestSchemaBuilder())->boot('multiConnectionBuilder');
$schema
->addModelbyClassName(FakeProductPage::class, function (ModelType $model) {
$model->addField('title');
$model->addField('products', ['plugins' => ['filter' => true]]);
$model->addField('featuredProducts', ['plugins' => ['filter' => true]]);
})
->addModelbyClassName(FakeProduct::class, function (ModelType $model) {
$model->addField('title');
$model->addField('parent');
$model->addField('featuredOn');
});
$schema->createStoreableSchema();
$filterType = $schema->getType('FakeProductPageFilterFields');
$this->assertNotNull($filterType, "Type FakeProductPageFilterFields not found in schema");
$productsFilter = $filterType->getFieldByName('products');
$this->assertNotNull($productsFilter, "Field products not found on {$filterType->getName()}");
$this->assertEquals('FakeProductFilterFields', $productsFilter->getType());
$featuredFilter = $filterType->getFieldByName('featuredProducts');
$this->assertNotNull($featuredFilter, "Field featuredProducts not found on {$filterType->getName()}");
$this->assertEquals('FakeProductFilterFields', $featuredFilter->getType());
}

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