Skip to content

Commit

Permalink
Add legacy readone graphql resolver (#1260)
Browse files Browse the repository at this point in the history
  • Loading branch information
satrun77 authored Oct 21, 2021
1 parent 648f273 commit 0fd580e
Showing 1 changed file with 48 additions and 0 deletions.
48 changes: 48 additions & 0 deletions _legacy/GraphQL/ReadOneLegacyResolver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

namespace SilverStripe\Admin\GraphQL;

use GraphQL\Type\Definition\ResolveInfo;
use SilverStripe\Core\Injector\Injector;
use SilverStripe\GraphQL\OperationResolver;
use SilverStripe\GraphQL\Scaffolding\Scaffolders\CRUD\ReadOne;
use SilverStripe\GraphQL\Scaffolding\StaticSchema;
use SilverStripe\ORM\DataObject;
use Symfony\Component\VarDumper\Cloner\Data;

if (!class_exists(ReadOne::class)) {
return;
}

/**
* Shim to make readOne work like GraphQL 4
*
* @deprecated 4.8..5.0 Use silverstripe/graphql:^4 functionality.
*/
class ReadOneLegacyResolver implements OperationResolver
{
/**
* @var DataObject
*/
protected $dataObject;

public function __construct(DataObject $dataObject)
{
$this->dataObject = $dataObject;
}

public function resolve($object, array $args, $context, ResolveInfo $info)
{
if (!$this->dataObject->canView($context['currentUser'])) {
throw new \Exception(sprintf('Cannot view %s', $this->dataObject->singular_name()));
}

$idKey = StaticSchema::inst()->formatField('ID');
$id = $args['filter'][$idKey]['eq'];
$readOne = Injector::inst()->createWithArgs(ReadOne::class, [$this->dataObject->baseClass()]);
unset($args['filter']);
$args[$idKey] = $id;

return $readOne->resolve($object, $args, $context, $info);
}
}

0 comments on commit 0fd580e

Please sign in to comment.