-
Notifications
You must be signed in to change notification settings - Fork 96
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add legacy readone graphql resolver (#1260)
- Loading branch information
Showing
1 changed file
with
48 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |