Skip to content

Commit

Permalink
BUGFIX: Throw on duplicate derived model names
Browse files Browse the repository at this point in the history
  • Loading branch information
Aaron Carlino committed Dec 20, 2021
1 parent 6f5c752 commit ba3de50
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 2 deletions.
10 changes: 10 additions & 0 deletions src/Schema/Schema.php
Original file line number Diff line number Diff line change
Expand Up @@ -860,6 +860,16 @@ public function removeScalar(string $name): self
public function addModel(ModelType $modelType, ?callable $callback = null): Schema
{
$existing = $this->models[$modelType->getName()] ?? null;
if ($existing) {
Schema::invariant(
$existing->getModel()->getSourceClass() === $modelType->getModel()->getSourceClass(),
'Name collision for model "%s". It is used for both %s and %s. Use the type_mapping setting in your schema
config to provide a custom name for the model.',
$modelType->getName(),
$existing->getModel()->getSourceClass(),
$modelType->getModel()->getSourceClass()
);
}
$typeObj = $existing
? $existing->mergeWith($modelType)
: $modelType;
Expand Down
14 changes: 14 additions & 0 deletions tests/Fake/SubFake/FakePage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace SilverStripe\GraphQL\Tests\Fake\SubFake;

use SilverStripe\GraphQL\Tests\Fake\FakeSiteTree;

class FakePage extends FakeSiteTree
{
private static $table_name = "GraphQL_SubFakePage";

private static $db = [
'SubFakePageField' => 'Varchar',
];
}
22 changes: 20 additions & 2 deletions tests/Schema/SchemaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,10 @@

use SilverStripe\Core\Config\Config;
use SilverStripe\Dev\SapphireTest;
use SilverStripe\GraphQL\Schema\DataObject\DataObjectModel;
use SilverStripe\GraphQL\Schema\DataObject\ModelCreator;
use SilverStripe\GraphQL\Schema\Exception\SchemaBuilderException;
use SilverStripe\GraphQL\Schema\Field\Mutation;
use SilverStripe\GraphQL\Schema\Field\Query;
use SilverStripe\GraphQL\Schema\Interfaces\SchemaStorageInterface;
use SilverStripe\GraphQL\Schema\Schema;
use SilverStripe\GraphQL\Schema\SchemaConfig;
use SilverStripe\GraphQL\Schema\Type\Enum;
Expand All @@ -19,7 +17,9 @@
use SilverStripe\GraphQL\Schema\Type\Type;
use SilverStripe\GraphQL\Schema\Type\UnionType;
use SilverStripe\GraphQL\Tests\Fake\DataObjectFake;
use SilverStripe\GraphQL\Tests\Fake\FakePage;
use SilverStripe\GraphQL\Tests\Fake\FakeSiteTree;
use SilverStripe\GraphQL\Tests\Fake\SubFake\FakePage as SubFakePage;

class SchemaTest extends SapphireTest
{
Expand Down Expand Up @@ -259,6 +259,24 @@ public function testValidConfig()
Schema::assertValidConfig([]);
}

public function testDuplicateModels()
{
$schema = $this->buildSchema();
$schema->addModelbyClassName(FakePage::class);
$this->expectException(SchemaBuilderException::class);
$schema->addModelbyClassName(SubFakePage::class);

$schema = $this->buildSchema();
$schema->getConfig()->getModelConfiguration()->set('type_mapping', [
SubFakePage::class => 'SubFakePage',
]);
$schema->addModelbyClassName(FakePage::class);
$schema->addModelbyClassName(SubFakePage::class);

$this->assertInstanceOf(ModelType::class, $schema->getModel('FakePage'));
$this->assertInstanceOf(ModelType::class, $schema->getModel('SubFakePage'));
}

public static function noop()
{
}
Expand Down
File renamed without changes.

0 comments on commit ba3de50

Please sign in to comment.