Skip to content

Commit

Permalink
GraphQL v4 support
Browse files Browse the repository at this point in the history
  • Loading branch information
blueo committed Oct 28, 2020
1 parent 6777e41 commit c94e388
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 168 deletions.
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.

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

namespace SilverStripe\Link\GraphQL;

use GraphQL\Type\Definition\ResolveInfo;
use SilverStripe\GraphQL\Schema\Resolver\DefaultResolverProvider;
use SilverStripe\Link\Type\Registry;

class LinkDescriptionResolver extends DefaultResolverProvider
{
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.

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

namespace SilverStripe\Link\GraphQL;

use GraphQL\Type\Definition\ResolveInfo;
use GraphQL\Type\Definition\Type as GraphqlType;
use SilverStripe\GraphQL\QueryCreator;
use SilverStripe\GraphQL\Schema\Resolver\DefaultResolverProvider;
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 extends DefaultResolverProvider
{

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.

0 comments on commit c94e388

Please sign in to comment.