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

GraphQL v4 support #2

Open
wants to merge 3 commits into
base: master
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
11 changes: 3 additions & 8 deletions _config/graphql.yml
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
---
Name: linkgraphql
---
SilverStripe\GraphQL\Manager:
SilverStripe\GraphQL\Schema\Schema:
schemas:
admin:
types:
LinkType: SilverStripe\Link\GraphQL\LinkTypeType
LinkDescription: SilverStripe\Link\GraphQL\LinkDescriptionType

queries:
readLinkTypes: SilverStripe\Link\GraphQL\LinkTypeQuery
readLinkDescription: SilverStripe\Link\GraphQL\LinkDescriptionQuery
src:
link: silverstripe/link:_graphql
6 changes: 6 additions & 0 deletions _graphql/queries.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
'readLinkDescription(dataStr: String!)':
type: LinkDescription
resolver: ['SilverStripe\Link\GraphQL\LinkDescriptionResolver', 'resolve']
'readLinkTypes(keys: [ID])':
type: '[LinkType]'
resolver: ['SilverStripe\Link\GraphQL\LinkTypeResolver', 'resolve']
11 changes: 11 additions & 0 deletions _graphql/types.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
LinkDescription:
description: Given some Link data, computes the matching description
fields:
description: String

LinkType:
description: Describe a Type of Link that can be managed by a LinkField
fields:
key: ID
handlerName: String!
title: String!
55 changes: 0 additions & 55 deletions src/GraphQL/LinkDescriptionQuery.php

This file was deleted.

29 changes: 29 additions & 0 deletions src/GraphQL/LinkDescriptionResolver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace SilverStripe\Link\GraphQL;

use GraphQL\Type\Definition\ResolveInfo;
use SilverStripe\Link\Type\Registry;

class LinkDescriptionResolver
{
public static function resolve($object, array $args, $context, ResolveInfo $info)
{
$data = json_decode($args['dataStr'], true);
if (json_last_error() !== JSON_ERROR_NONE) {
throw new \InvalidArgumentException('data must be a valid JSON string');
}

if (empty($data['typeKey'])) {
return ['description' => ''];
}

$type = Registry::singleton()->byKey($data['typeKey']);
if (empty($type)) {
return ['description' => ''];
}


return ['description' => $type->generateLinkDescription($data)];
}
}
35 changes: 0 additions & 35 deletions src/GraphQL/LinkDescriptionType.php

This file was deleted.

32 changes: 3 additions & 29 deletions src/GraphQL/LinkTypeQuery.php → src/GraphQL/LinkTypeResolver.php
Original file line number Diff line number Diff line change
@@ -1,40 +1,14 @@
<?php

namespace SilverStripe\Link\GraphQL;

use GraphQL\Type\Definition\ResolveInfo;
use GraphQL\Type\Definition\Type as GraphqlType;
use SilverStripe\GraphQL\QueryCreator;
use SilverStripe\Link\Type\Registry;
use SilverStripe\Link\Type\Type;

/**
* GraphQL Query to retrieve the list of possible LinkTypes.
*/
class LinkTypeQuery extends QueryCreator
class LinkTypeResolver
{

public function attributes()
{
return [
'name' => 'readLinkTypes'
];
}

public function type()
{
return GraphqlType::listOf($this->manager->getType('LinkType'));
}

public function args()
{
return [
'keys' => [
'type' => GraphqlType::listOf(GraphqlType::id()),
],
];
}

public function resolve($object, array $args, $context, ResolveInfo $info)
public static function resolve($object, array $args, $context, ResolveInfo $info)
{
if (isset($args['keys']) && !is_array($args['keys'])) {
throw new \InvalidArgumentException('If `keys` is provdied, it must be an array');
Expand Down
41 changes: 0 additions & 41 deletions src/GraphQL/LinkTypeType.php

This file was deleted.

2 changes: 1 addition & 1 deletion src/Models/Link.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ function setData($data): JsonData
return $jsonData;
}

public function jsonSerialize()
public function jsonSerialize(): mixed
{
$typeKey = Registry::singleton()->keyByClassName(static::class);
if (empty($typeKey)) {
Expand Down