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

NEW: Stateless plugins, plugin registry as simple list #327

Merged
merged 1 commit into from
Dec 7, 2020
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
18 changes: 9 additions & 9 deletions _config/plugins.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ Name: graphql-plugins
SilverStripe\Core\Injector\Injector:
SilverStripe\GraphQL\Schema\Registry\PluginRegistry:
constructor:
paginator: '%$SilverStripe\GraphQL\Schema\Plugin\PaginationPlugin'
dataobjectPaginator: '%$SilverStripe\GraphQL\Schema\DataObject\Plugin\Paginator'
dataobjectQueryFilter: '%$SilverStripe\GraphQL\Schema\DataObject\Plugin\QueryFilter\QueryFilter'
dataobjectQuerySort: '%$SilverStripe\GraphQL\Schema\DataObject\Plugin\QuerySort'
dataobjectInheritance: '%$SilverStripe\GraphQL\Schema\DataObject\Plugin\Inheritance'
canViewPermission: '%$SilverStripe\GraphQL\Schema\DataObject\Plugin\CanViewPermission'
firstResult: '%$SilverStripe\GraphQL\Schema\DataObject\Plugin\FirstResult'
inheritedPlugins: '%$SilverStripe\GraphQL\Schema\DataObject\Plugin\InheritedPlugins'
sorter: '%$SilverStripe\GraphQL\Schema\Plugin\SortPlugin'
- 'SilverStripe\GraphQL\Schema\Plugin\PaginationPlugin'
chillu marked this conversation as resolved.
Show resolved Hide resolved
- 'SilverStripe\GraphQL\Schema\DataObject\Plugin\Paginator'
- 'SilverStripe\GraphQL\Schema\DataObject\Plugin\QueryFilter\QueryFilter'
- 'SilverStripe\GraphQL\Schema\DataObject\Plugin\QuerySort'
- 'SilverStripe\GraphQL\Schema\DataObject\Plugin\Inheritance'
- 'SilverStripe\GraphQL\Schema\DataObject\Plugin\CanViewPermission'
- 'SilverStripe\GraphQL\Schema\DataObject\Plugin\FirstResult'
- 'SilverStripe\GraphQL\Schema\DataObject\Plugin\InheritedPlugins'
- 'SilverStripe\GraphQL\Schema\Plugin\SortPlugin'
2 changes: 1 addition & 1 deletion src/Schema/Plugin/AbstractNestedInputPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ protected function buildAllFieldsConfig(ModelType $modelType, Schema $schema, in
$filters[$fieldObj->getName()] = true;
}
}
$this->_allConfigCache[$modelType->getName()] = $filters;
$this->_allConfigCache[$key] = $filters;

return $filters;
}
Expand Down
18 changes: 11 additions & 7 deletions src/Schema/Registry/PluginRegistry.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
namespace SilverStripe\GraphQL\Schema\Registry;

use SilverStripe\Core\Injector\Injectable;
use SilverStripe\Core\Injector\Injector;
use SilverStripe\GraphQL\Schema\Exception\SchemaBuilderException;
use SilverStripe\GraphQL\Schema\Interfaces\PluginInterface;
use SilverStripe\GraphQL\Schema\Schema;
Expand All @@ -21,25 +22,26 @@ class PluginRegistry
private $plugins = [];

/**
* @param PluginInterface[] ...$plugins
* @param array ...$plugins
* @throws SchemaBuilderException
*/
public function __construct(...$plugins)
{
foreach ($plugins as $plugin) {
$inst = Injector::inst()->get($plugin);
chillu marked this conversation as resolved.
Show resolved Hide resolved
Schema::invariant(
$plugin instanceof PluginInterface,
$inst instanceof PluginInterface,
'%s only accepts implementations of %s',
__CLASS__,
PluginInterface::class
);
$existing = $this->plugins[$plugin->getIdentifier()] ?? null;
$existing = $this->plugins[$inst->getIdentifier()] ?? null;
Schema::invariant(
!$existing || (get_class($existing) === get_class($plugin)),
!$existing || ($existing === $plugin),
'Two different plugins are registered under identifier %s',
$plugin->getIdentifier()
$inst->getIdentifier()
);
$this->plugins[$plugin->getIdentifier()] = $plugin;
$this->plugins[$inst->getIdentifier()] = $plugin;
}
}

Expand All @@ -49,6 +51,8 @@ public function __construct(...$plugins)
*/
public function getPluginByID(string $id): ?PluginInterface
{
return $this->plugins[$id] ?? null;
$class = $this->plugins[$id] ?? null;

return $class ? Injector::inst()->create($class) : null;
}
}
2 changes: 2 additions & 0 deletions src/Schema/Schema.php
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,8 @@ private function applyComponentPlugins(SchemaComponent $component, string $name)
// Duck programming here just because there is such an exhaustive list of possible
// interfaces, and they can't have a common ancestor until PHP 7.4 allows it.
// https://wiki.php.net/rfc/covariant-returns-and-contravariant-parameters
// ideally, this should be `instanceof PluginInterface` and PluginInterface should have
// apply(SchemaComponent)
if (!method_exists($plugin, 'apply')) {
continue;
}
Expand Down