Skip to content

Commit

Permalink
Fix race condition in interface builder adding to queries before inte…
Browse files Browse the repository at this point in the history
…rfaces are ready
  • Loading branch information
Aaron Carlino committed Aug 4, 2021
1 parent 614bd77 commit b4c64c4
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 13 deletions.
2 changes: 0 additions & 2 deletions _config/schema-global.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@ SilverStripe\GraphQL\Schema\Schema:
before: scalarDBField
inheritance:
useUnionQueries: false
hideAncestors:
- SilverStripe\CMS\Model\SiteTree
after: 'versioning'
scalarDBField:
after: dbFieldArgs
Expand Down
22 changes: 20 additions & 2 deletions src/Schema/DataObject/InterfaceBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,20 @@ class InterfaceBuilder
*/
private $schema;

/**
* @var array
*/
private $hideAncestors = [];

/**
* InterfaceBuilderTest constructor.
* @param Schema $schema
* @param array $hideAncestors
*/
public function __construct(Schema $schema)
public function __construct(Schema $schema, array $hideAncestors = [])
{
$this->setSchema($schema);
$this->hideAncestors = $hideAncestors;
}

/**
Expand Down Expand Up @@ -86,7 +93,9 @@ public function createInterfaces(ModelType $modelType, array $interfaceStack = [
$interfaceStack[] = $interface;
$modelType->addInterface($interface->getName());

$chain = InheritanceChain::create($modelType->getModel()->getSourceClass());
$chain = InheritanceChain::create($modelType->getModel()->getSourceClass())
->hideAncestors($this->hideAncestors);

foreach ($chain->getDirectDescendants() as $class) {
if ($childType = $this->getSchema()->getModelByClassName($class)) {
$this->createInterfaces($childType, $interfaceStack);
Expand Down Expand Up @@ -130,6 +139,7 @@ public function applyBaseInterface(): InterfaceBuilder

/**
* @param ModelType $type
* @throws ReflectionException
* @throws SchemaBuilderException
* @return $this
*/
Expand Down Expand Up @@ -157,6 +167,14 @@ public function applyInterfacesToQueries(ModelType $type): InterfaceBuilder
$this->schema->eagerLoad($modelType->getName());
}
}
$chain = InheritanceChain::create($type->getModel()->getSourceClass())
->hideAncestors($this->hideAncestors);

foreach ($chain->getDirectDescendants() as $class) {
if ($modelType = $schema->getModelByClassName($class)) {
$this->applyInterfacesToQueries($modelType);
}
}

return $this;
}
Expand Down
21 changes: 12 additions & 9 deletions src/Schema/DataObject/Plugin/Inheritance.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,23 +57,26 @@ public function apply(ModelType $type, Schema $schema, array $config = []): void
$useUnions = $config['useUnionQueries'] ?? false;
$hideAncestors = $config['hideAncestors'] ?? [];

if (in_array($type->getModel()->getSourceClass(), $hideAncestors)) {
return;
}

$inheritance = InheritanceBuilder::create($schema, $hideAncestors);
$interfaces = InterfaceBuilder::create($schema);
$interfaces = InterfaceBuilder::create($schema, $hideAncestors);

$class = $type->getModel()->getSourceClass();
if ($inheritance->isLeafModel($class)) {
$inheritance->fillAncestry($type);
} elseif ($inheritance->isBaseModel($class)) {
$inheritance->fillDescendants($type);
$interfaces->createInterfaces($type);
}

if ($useUnions) {
InheritanceUnionBuilder::create($schema)
->createUnions($type)
->applyUnionsToQueries($type);
} else {
$interfaces->applyInterfacesToQueries($type);
if ($useUnions) {
InheritanceUnionBuilder::create($schema)
->createUnions($type)
->applyUnionsToQueries($type);
} else {
$interfaces->applyInterfacesToQueries($type);
}
}
}
}

0 comments on commit b4c64c4

Please sign in to comment.