From 0c76ddccaacacd6d11dc2801f6315f887285fb6d Mon Sep 17 00:00:00 2001 From: Aaron Carlino Date: Tue, 20 Jul 2021 13:07:32 +1200 Subject: [PATCH 1/3] Refactor VersionedDataPlugin to only be a schema updater to ensure all types are in place before other plugins need them --- _config/graphql_plugins.yml | 1 - _graphql/config.yml | 5 +- src/GraphQL/Plugins/VersionedDataObject.php | 108 +++++++++----------- 3 files changed, 50 insertions(+), 64 deletions(-) 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..d204c271 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)) { + return; + } - $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,9 @@ public function apply(ModelType $type, Schema $schema, array $config = []): void 'fields' => [ 'version' => true ], ]); Paginator::singleton()->apply($field, $schema); - }); + }); + } + } /** From e628502d6ec6503ef615a1133cffd4a551072733 Mon Sep 17 00:00:00 2001 From: Aaron Carlino Date: Tue, 20 Jul 2021 13:37:08 +1200 Subject: [PATCH 2/3] Linting, fix tests --- src/GraphQL/Plugins/VersionedDataObject.php | 1 - tests/php/GraphQL/Plugins/VersionedDataObjectPluginTest.php | 2 -- 2 files changed, 3 deletions(-) diff --git a/src/GraphQL/Plugins/VersionedDataObject.php b/src/GraphQL/Plugins/VersionedDataObject.php index d204c271..a398a22d 100644 --- a/src/GraphQL/Plugins/VersionedDataObject.php +++ b/src/GraphQL/Plugins/VersionedDataObject.php @@ -106,7 +106,6 @@ public static function updateSchema(Schema $schema): void 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); From bab3c00fd343491dd4333c7f4ea4f3554ad8fba4 Mon Sep 17 00:00:00 2001 From: Aaron Carlino Date: Wed, 4 Aug 2021 22:27:15 +1200 Subject: [PATCH 3/3] Fix early return bug --- src/GraphQL/Plugins/VersionedDataObject.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/GraphQL/Plugins/VersionedDataObject.php b/src/GraphQL/Plugins/VersionedDataObject.php index a398a22d..94293d6b 100644 --- a/src/GraphQL/Plugins/VersionedDataObject.php +++ b/src/GraphQL/Plugins/VersionedDataObject.php @@ -61,7 +61,7 @@ public static function updateSchema(Schema $schema): void ); if (!Extensible::has_extension($class, Versioned::class)) { - return; + continue; } $versionName = $type->getModel()->getTypeName() . 'Version';