forked from silverstripe/silverstripe-versioned
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support for silverstripe/silverstripe-graphql#209
- Loading branch information
Showing
3 changed files
with
120 additions
and
33 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
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
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,95 @@ | ||
<?php | ||
|
||
namespace SilverStripe\Versioned\GraphQL\Types; | ||
|
||
use Exception; | ||
use GraphQL\Type\Definition\Type; | ||
use SilverStripe\GraphQL\InterfaceTypeCreator; | ||
use SilverStripe\GraphQL\Scaffolding\StaticSchema; | ||
use SilverStripe\ORM\DataObject; | ||
use SilverStripe\Security\Member; | ||
|
||
if (!class_exists(InterfaceTypeCreator::class)) { | ||
return; | ||
} | ||
|
||
class VersionInterfaceType extends InterfaceTypeCreator | ||
{ | ||
/** | ||
* @return array | ||
*/ | ||
public function attributes() | ||
{ | ||
return [ | ||
'name' => 'VersionInterface', | ||
'description' => 'Metadata fields for versioning on a DataObject' | ||
]; | ||
} | ||
|
||
/** | ||
* @return array | ||
*/ | ||
public function fields() | ||
{ | ||
$memberType = StaticSchema::inst()->typeNameForDataObject(Member::class); | ||
return [ | ||
'Author' => [ | ||
'type' => $this->manager->getType($memberType), | ||
'resolve' => function ($obj) { | ||
return $obj->Author(); | ||
} | ||
], | ||
'Publisher' => [ | ||
'type' => $this->manager->getType($memberType), | ||
'resolve' => function ($obj) { | ||
return $obj->Publisher(); | ||
} | ||
], | ||
'Published' => [ | ||
'type' => Type::boolean(), | ||
'resolve' => function ($obj) { | ||
return $obj->WasPublished; | ||
} | ||
], | ||
'LiveVersion' => [ | ||
'type' => Type::boolean(), | ||
'resolve' => function ($obj) { | ||
return $obj->isLiveVersion(); | ||
} | ||
], | ||
'LatestDraftVersion' => [ | ||
'type' => Type::boolean(), | ||
'resolve' => function ($obj) { | ||
return $obj->isLatestDraftVersion(); | ||
} | ||
], | ||
]; | ||
} | ||
|
||
/** | ||
* @return \GraphQL\Type\Definition\Type | ||
* @throws Exception | ||
*/ | ||
public function resolveType() | ||
{ | ||
if (!$obj instanceof DataObject) { | ||
throw new Exception(sprintf( | ||
'Type with class %s is not a DataObject', | ||
get_class($obj) | ||
)); | ||
} | ||
$class = get_class($obj); | ||
while ($class !== DataObject::class) { | ||
$typeName = StaticSchema::inst()->typeNameForDataObject($class); | ||
if ($this->manager->hasType($typeName)) { | ||
return $this->manager->getType($typeName); | ||
} | ||
$class = get_parent_class($class); | ||
} | ||
throw new Exception(sprintf( | ||
'There is no type defined for %s, and none of its ancestors are defined.', | ||
get_class($obj) | ||
)); | ||
} | ||
|
||
} |