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: Refactor VersionedDataObject Plugin #337

Open
wants to merge 3 commits into
base: 1
Choose a base branch
from
Open
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
1 change: 0 additions & 1 deletion _config/graphql_plugins.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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'
5 changes: 2 additions & 3 deletions _graphql/config.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
execute:
- SilverStripe\Versioned\GraphQL\Plugins\VersionedDataObject
modelConfig:
DataObject:
plugins:
versioning:
before: inheritance
operations:
copyToStage:
class: SilverStripe\Versioned\GraphQL\Operations\CopyToStageCreator
Expand Down
107 changes: 47 additions & 60 deletions src/GraphQL/Plugins/VersionedDataObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand All @@ -56,69 +47,65 @@ 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, [
'resolver' => [static::class, 'sortVersions'],
'fields' => [ 'version' => true ],
]);
Paginator::singleton()->apply($field, $schema);
});
});
}
}

/**
Expand Down
2 changes: 0 additions & 2 deletions tests/php/GraphQL/Plugins/VersionedDataObjectPluginTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -98,7 +97,6 @@ public function testPluginDoesntAddVersionedFieldsToUnversionedObjects()
$plugin = new VersionedDataObject();
$plugin->updateSchema($schema);

$plugin->apply($type, $schema);
$type = $schema->getType('FakeVersion');
$this->assertNull($type);

Expand Down