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

API Standardise OperationResolver::resolve() #28

Merged
merged 1 commit into from
Dec 20, 2016
Merged
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
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,11 @@ namespace MyProject\GraphQL;

use GraphQL\Type\Definition\Type;
use SilverStripe\GraphQL\QueryCreator;
use SilverStripe\GraphQL\OperationResolver;
use MyProject\MyDataObject;
use SilverStripe\Security\Member;

class ReadMembersQueryCreator extends QueryCreator
class ReadMembersQueryCreator extends QueryCreator implements OperationResolver
{

public function attributes()
Expand All @@ -126,7 +127,7 @@ class ReadMembersQueryCreator extends QueryCreator
}


public function resolve($args)
public function resolve($object, array $args, $context, $info)
{
$list = Member::get();

Expand Down Expand Up @@ -372,10 +373,10 @@ namespace MyProject\GraphQL;

use GraphQL\Type\Definition\Type;
use SilverStripe\GraphQL\MutationCreator;
use SilverStripe\GraphQL\OperationResolver;
use SilverStripe\Security\Member;


class CreateMemberMutationCreator extends MutationCreator
class CreateMemberMutationCreator extends MutationCreator implements OperationResolver
{

public function attributes()
Expand Down
5 changes: 2 additions & 3 deletions src/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
use SilverStripe\Control\Controller as BaseController;
use SilverStripe\Control\HTTPRequest;
use SilverStripe\Control\HTTPResponse;
use SilverStripe\Core\Injector\Injector;
use SilverStripe\Core\Config\Config;
use SilverStripe\Control\Director;
use Exception;
Expand Down Expand Up @@ -37,7 +36,7 @@ public function index(HTTPRequest $request)
$variables = isset($data['variables']) ? $data['variables'] : null;

// Some clients (e.g. GraphiQL) double encode as string
if(is_string($variables)) {
if (is_string($variables)) {
$variables = json_decode($variables, true);
}

Expand All @@ -48,7 +47,7 @@ public function index(HTTPRequest $request)
} catch (Exception $exception) {
$error = ['message' => $exception->getMessage()];

if(Director::isDev()) {
if (Director::isDev()) {
$error['code'] = $exception->getCode();
$error['file'] = $exception->getFile();
$error['line'] = $exception->getLine();
Expand Down
13 changes: 6 additions & 7 deletions src/DataObjectInterfaceTypeCreator.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@

namespace SilverStripe\GraphQL;

use SilverStripe\GraphQL\Util\CaseInsensitiveFieldAccessor;
use GraphQL\Type\Definition\Type;

/**
* Base interface for any {@link DataObject} passed back as a node.
*
*/
class DataObjectInterfaceTypeCreator extends InterfaceTypeCreator {
class DataObjectInterfaceTypeCreator extends InterfaceTypeCreator
{

public function attributes()
{
Expand All @@ -19,7 +18,8 @@ public function attributes()
];
}

public function fields() {
public function fields()
{
return [
'id' => [
'type' => Type::nonNull(Type::int()),
Expand All @@ -37,15 +37,14 @@ public function resolveType($object)
{
$type = null;

if($fqnType = $this->manager->getType(get_class($object))) {
if ($fqnType = $this->manager->getType(get_class($object))) {
$type = $fqnType;
}

if($baseType = $this->manager->getType(get_class($object))) {
if ($baseType = $this->manager->getType(get_class($object))) {
$type = $baseType;
}

return $type;
}

}
6 changes: 3 additions & 3 deletions src/FieldCreator.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@

use SilverStripe\Core\Object;
use GraphQL\Type\Definition\Type;
use SilverStripe\GraphQL\Manager;
use SilverStripe\ORM\Limitable;

class FieldCreator extends Object
{
Expand Down Expand Up @@ -97,10 +95,12 @@ public function __get($key)

return isset($attributes[$key]) ? $attributes[$key] : null;
}

/**
* Dynamically check if an attribute is set.
*
* @param string $key
* @return bool
*/
public function __isset($key)
{
Expand All @@ -114,7 +114,7 @@ public function __isset($key)
*/
protected function getResolver()
{
if (!method_exists($this, 'resolve')) {
if (! method_exists($this, 'resolve')) {
return null;
}

Expand Down
77 changes: 39 additions & 38 deletions src/GraphiQLController.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,45 +5,46 @@
use SilverStripe\Control\Controller as BaseController;
use SilverStripe\Control\Director;
use SilverStripe\View\Requirements;
class GraphiQLController extends BaseController

class GraphiQLController extends BaseController
{
/**
* @var string
*/
protected $template = 'GraphiQL';

/**
* Initialise the controller, sanity check, load javascript
*/
public function init()
{
parent::init();

if(!Director::isDev()) {
return $this->httpError(403, 'The GraphiQL tool is only available in dev mode');
}

$routes = Director::config()->get('rules');
$route = null;

foreach($routes as $pattern => $controllerInfo) {
if($controllerInfo == Controller::class || is_subclass_of($controllerInfo, Controller::class)) {
$route = $pattern;
break;
}
}

if(!$route) {
throw new \RuntimeException("There are no routes set up for a GraphQL server. You will need to add one to the SilverStripe\Control\Director.rules config setting.");
}

Requirements::customScript(
<<<JS
/**
* @var string
*/
protected $template = 'GraphiQL';

/**
* Initialise the controller, sanity check, load javascript
*/
public function init()
{
parent::init();

if (!Director::isDev()) {
$this->httpError(403, 'The GraphiQL tool is only available in dev mode');
return;
}

$routes = Director::config()->get('rules');
$route = null;

foreach ($routes as $pattern => $controllerInfo) {
if ($controllerInfo == Controller::class || is_subclass_of($controllerInfo, Controller::class)) {
$route = $pattern;
break;
}
}

if (!$route) {
throw new \RuntimeException("There are no routes set up for a GraphQL server. You will need to add one to the SilverStripe\Control\Director.rules config setting.");
}

Requirements::customScript(
<<<JS
var GRAPHQL_ROUTE = '{$route}';
JS
);
);

Requirements::javascript(GRAPHQL_DIR.'/client/dist/graphiql.js');
}
}
Requirements::javascript(GRAPHQL_DIR.'/client/dist/graphiql.js');
}
}
13 changes: 5 additions & 8 deletions src/InterfaceTypeCreator.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,17 @@

use GraphQL\Type\Definition\InterfaceType as BaseInterfaceType;

class InterfaceTypeCreator extends TypeCreator {
class InterfaceTypeCreator extends TypeCreator
{

protected function getTypeResolver()
{
if(!method_exists($this, 'resolveType'))
{
if (!method_exists($this, 'resolveType')) {
return null;
}

$resolver = array($this, 'resolveType');
return function() use ($resolver)
{
return function () use ($resolver) {
$args = func_get_args();
return call_user_func_array($resolver, $args);
};
Expand All @@ -31,8 +30,7 @@ public function getAttributes()
$attributes = parent::getAttributes();

$resolver = $this->getTypeResolver();
if(isset($resolver))
{
if (isset($resolver)) {
$attributes['resolveType'] = $resolver;
}

Expand All @@ -43,5 +41,4 @@ public function toType()
{
return new BaseInterfaceType($this->toArray());
}

}
4 changes: 2 additions & 2 deletions src/Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ public function queryAndReturnResult($query, $params = [], $schema = null)
*/
public function addType(Type $type, $name = '')
{
if(!$name) {
if (!$name) {
$name = (string)$type;
}

Expand All @@ -174,7 +174,7 @@ public function addType(Type $type, $name = '')
*/
public function getType($name)
{
if(isset($this->types[$name])) {
if (isset($this->types[$name])) {
return $this->types[$name];
} else {
throw new \InvalidArgumentException("Type '$name' is not a registered GraphQL type");
Expand Down
2 changes: 1 addition & 1 deletion src/MutationCreator.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*
* @todo Validation support
*/
class MutationCreator extends FieldCreator
abstract class MutationCreator extends FieldCreator
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was buggedby that as well, but assumed you couldn't declare a subclass of a concrete class as abstract - turns out you can!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

zomg..

{

}
24 changes: 24 additions & 0 deletions src/OperationResolver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace SilverStripe\GraphQL;

use GraphQL\Type\Definition\ResolveInfo;
use GraphQL\Executor\Executor;

/**
* Standard resolve callback for Mutations or Queries
*/
interface OperationResolver
{
/**
* Invoked by the Executor class to resolve this mutation / query
* @see Executor
*
* @param mixed $object
* @param array $args
* @param mixed $context
* @param ResolveInfo $info
* @return mixed
*/
public function resolve($object, array $args, $context, ResolveInfo $info);
}
Loading