diff --git a/_config/graphql_plugins.yml b/_config/graphql_plugins.yml index 5f63ffdb..1db61e15 100644 --- a/_config/graphql_plugins.yml +++ b/_config/graphql_plugins.yml @@ -7,6 +7,5 @@ Only: SilverStripe\Core\Injector\Injector: SilverStripe\GraphQL\Schema\Registry\PluginRegistry: constructor: - - 'SilverStripe\Versioned\GraphQL\Plugins\VersionedDataObject' - 'SilverStripe\Versioned\GraphQL\Plugins\UnpublishOnDelete' - 'SilverStripe\Versioned\GraphQL\Plugins\VersionedRead' diff --git a/_graphql/config.yml b/_graphql/config.yml index 46ec11eb..20415748 100644 --- a/_graphql/config.yml +++ b/_graphql/config.yml @@ -1,8 +1,7 @@ +execute: + - SilverStripe\Versioned\GraphQL\Plugins\VersionedDataObject modelConfig: DataObject: - plugins: - versioning: - before: inheritance operations: copyToStage: class: SilverStripe\Versioned\GraphQL\Operations\CopyToStageCreator diff --git a/src/GraphQL/Plugins/VersionedDataObject.php b/src/GraphQL/Plugins/VersionedDataObject.php index a113b540..94293d6b 100644 --- a/src/GraphQL/Plugins/VersionedDataObject.php +++ b/src/GraphQL/Plugins/VersionedDataObject.php @@ -3,6 +3,7 @@ namespace SilverStripe\Versioned\GraphQL\Plugins; use SilverStripe\Core\Extensible; +use SilverStripe\GraphQL\Schema\DataObject\DataObjectModel; use SilverStripe\GraphQL\Schema\DataObject\Plugin\Paginator; use SilverStripe\GraphQL\Schema\Exception\SchemaBuilderException; use SilverStripe\GraphQL\Schema\Field\Field; @@ -28,18 +29,8 @@ return; } -class VersionedDataObject implements ModelTypePlugin, SchemaUpdater +class VersionedDataObject implements SchemaUpdater { - const IDENTIFIER = 'versioning'; - - /** - * @return string - */ - public function getIdentifier(): string - { - return self::IDENTIFIER; - } - /** * @param Schema $schema * @throws SchemaBuilderException @@ -56,61 +47,56 @@ public static function updateSchema(Schema $schema): void if (!$schema->getType('PageInfo')) { PaginationPlugin::updateSchema($schema); } - } - /** - * @param ModelType $type - * @param Schema $schema - * @param array $config - * @throws SchemaBuilderException - */ - public function apply(ModelType $type, Schema $schema, array $config = []): void - { - $class = $type->getModel()->getSourceClass(); - Schema::invariant( - is_subclass_of($class, DataObject::class), - 'The %s plugin can only be applied to types generated by %s models', - __CLASS__, - DataObject::class - ); + foreach ($schema->getModels() as $type) { + if (!$type->getModel() instanceof DataObjectModel) { + continue; + } + $class = $type->getModel()->getSourceClass(); + Schema::invariant( + is_subclass_of($class, DataObject::class), + 'The %s plugin can only be applied to types generated by %s models', + __CLASS__, + DataObject::class + ); - if (!Extensible::has_extension($class, Versioned::class)) { - return; - } + if (!Extensible::has_extension($class, Versioned::class)) { + continue; + } - $versionName = $type->getModel()->getTypeName() . 'Version'; - $memberType = $schema->getModelByClassName(Member::class); - Schema::invariant( - $memberType, - 'The %s class was not added as a model. Should have been done in %s::%s?', - Member::class, - __CLASS__, - 'updateSchema' - ); - $memberTypeName = $memberType->getModel()->getTypeName(); - $resolver = ['resolver' => [VersionedResolver::class, 'resolveVersionFields']]; + $versionName = $type->getModel()->getTypeName() . 'Version'; + $memberType = $schema->getModelByClassName(Member::class); + Schema::invariant( + $memberType, + 'The %s class was not added as a model. Should have been done in %s::%s?', + Member::class, + __CLASS__, + 'updateSchema' + ); + $memberTypeName = $memberType->getModel()->getTypeName(); + $resolver = ['resolver' => [VersionedResolver::class, 'resolveVersionFields']]; - $type->addField('version', 'Int'); + $type->addField('version', 'Int'); - $versionType = Type::create($versionName) - ->addField('author', ['type' => $memberTypeName] + $resolver) - ->addField('publisher', ['type' => $memberTypeName] + $resolver) - ->addField('published', ['type' => 'Boolean'] + $resolver) - ->addField('liveVersion', ['type' => 'Boolean'] + $resolver) - ->addField('deleted', ['type' => 'Boolean'] + $resolver) - ->addField('draft', ['type' => 'Boolean'] + $resolver) - ->addField('latestDraftVersion', ['type' => 'Boolean'] + $resolver); + $versionType = Type::create($versionName) + ->addField('author', ['type' => $memberTypeName] + $resolver) + ->addField('publisher', ['type' => $memberTypeName] + $resolver) + ->addField('published', ['type' => 'Boolean'] + $resolver) + ->addField('liveVersion', ['type' => 'Boolean'] + $resolver) + ->addField('deleted', ['type' => 'Boolean'] + $resolver) + ->addField('draft', ['type' => 'Boolean'] + $resolver) + ->addField('latestDraftVersion', ['type' => 'Boolean'] + $resolver); - foreach ($type->getFields() as $field) { - $clone = clone $field; - $versionType->addField($clone->getName(), $clone); - } - foreach ($type->getInterfaces() as $interface) { - $versionType->addInterface($interface); - } + foreach ($type->getFields() as $field) { + $clone = clone $field; + $versionType->addField($clone->getName(), $clone); + } + foreach ($type->getInterfaces() as $interface) { + $versionType->addInterface($interface); + } - $schema->addType($versionType); - $type->addField('versions', '[' . $versionName . ']', function (Field $field) use ($type, $schema, $config) { + $schema->addType($versionType); + $type->addField('versions', '[' . $versionName . ']', function (Field $field) use ($type, $schema) { $field->setResolver([VersionedResolver::class, 'resolveVersionList']) ->addResolverContext('sourceClass', $type->getModel()->getSourceClass()); SortPlugin::singleton()->apply($field, $schema, [ @@ -118,7 +104,8 @@ public function apply(ModelType $type, Schema $schema, array $config = []): void 'fields' => [ 'version' => true ], ]); Paginator::singleton()->apply($field, $schema); - }); + }); + } } /** diff --git a/tests/php/GraphQL/Plugins/VersionedDataObjectPluginTest.php b/tests/php/GraphQL/Plugins/VersionedDataObjectPluginTest.php index 6aa3a619..63cba770 100644 --- a/tests/php/GraphQL/Plugins/VersionedDataObjectPluginTest.php +++ b/tests/php/GraphQL/Plugins/VersionedDataObjectPluginTest.php @@ -61,7 +61,6 @@ public function testPluginAddsVersionedFields() $plugin->updateSchema($schema); $this->assertInstanceOf(ModelType::class, $schema->getModelByClassName(Member::class)); - $plugin->apply($type, $schema); $storableSchema = $schema->createStoreableSchema(); $types = $storableSchema->getTypes(); $this->assertArrayHasKey('FakeVersion', $types); @@ -98,7 +97,6 @@ public function testPluginDoesntAddVersionedFieldsToUnversionedObjects() $plugin = new VersionedDataObject(); $plugin->updateSchema($schema); - $plugin->apply($type, $schema); $type = $schema->getType('FakeVersion'); $this->assertNull($type);