Skip to content

Commit

Permalink
InputObject support
Browse files Browse the repository at this point in the history
  • Loading branch information
chillu committed Sep 24, 2016
1 parent 1f1f931 commit 690a699
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 36 deletions.
18 changes: 18 additions & 0 deletions src/TypeCreator.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use SilverStripe\Core\Object;
use GraphQL\Type\Definition\ObjectType;
use GraphQL\Type\Definition\InputObjectType;
use Chillu\GraphQL\Manager;

/**
Expand All @@ -18,6 +19,11 @@ class TypeCreator extends Object
*/
protected $manager;

/**
* @var bool Determines if the object should be cast as an {@link InputObjectType}
*/
protected $inputObject = false;

/**
* @param Manager|null Used to retrieve types (including the one returned from this creator),
* and nest field types regardless of instantiation of their creators.
Expand Down Expand Up @@ -70,11 +76,23 @@ public function getFields()
return $allFields;
}

/**
* @return bool
*/
public function isInputObject()
{
return $this->inputObject;
}

/**
* @return ObjectType
*/
public function toType()
{
if($this->isInputObject()) {
return new InputObjectType($this->toArray());
}

return new ObjectType($this->toArray());
}

Expand Down
82 changes: 46 additions & 36 deletions tests/TypeCreatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,68 +2,60 @@

namespace Chillu\GraphQL;

use GraphQL\Type\Definition\InputObjectType;
use SilverStripe\Dev\SapphireTest;
use GraphQL\Type\Definition\Type;

class TypeCreatorTest extends SapphireTest
{
public function testGetFields()
{
$mock = $this->getMockBuilder(TypeCreator::class)
->setMethods(['fields'])
->getMock();
$mock = $this->getTypeCreatorMock();
$mock->method('fields')->willReturn([
'ID' => [
'type' => Type::nonNull(Type::id()),
],
]);

$fields = $mock->getFields();

$this->assertArrayHasKey('ID', $fields);
}

public function testToArray()
{
$mock = $this->getMockBuilder(TypeCreator::class)
->setMethods(['fields'])
->getMock();
$mock->method('fields')->willReturn([
'ID' => [
'type' => Type::nonNull(Type::id()),
],
]);

$mock = $this->getTypeCreatorMock();
$actual = $mock->toArray();
$this->assertArrayHasKey('fields', $actual);

$fields = $actual['fields']();
$this->assertArrayHasKey('ID', $fields);
}

public function testGetAttributes()
public function testToType()
{
$mock = $this->getMockBuilder(TypeCreator::class)
->setMethods(['fields'])
->getMock();
$mock->method('fields')->willReturn([
'ID' => [
'type' => Type::nonNull(Type::id()),
],
]);
$mock = $this->getTypeCreatorMock();
$actual = $mock->toType();
$this->assertInstanceOf(Type::class, $actual);
}

public function testToTypeWithInputObject()
{
$mock = $this->getTypeCreatorMock(['isInputObject']);
$mock->method('isInputObject')->willReturn(true);
$actual = $mock->toType();
$this->assertInstanceOf(InputObjectType::class, $actual);
}

public function testGetAttributes()
{
$mock = $this->getTypeCreatorMock();
$actual = $mock->getAttributes();
$this->assertArrayHasKey('fields', $actual);

$fields = $actual['fields']();
$this->assertArrayHasKey('ID', $fields);
}

public function testGetFieldsUsesResolveConfig()
{
$mock = $this->getMockBuilder(TypeCreator::class)
->setMethods(['fields','resolveFieldAField'])
->getMock();
$mock = $this->getTypeCreatorMock(['resolveFieldAField', 'fields']);
$mock->method('fields')->willReturn([
'fieldA' => [
'type' => Type::string(),
Expand All @@ -73,7 +65,7 @@ public function testGetFieldsUsesResolveConfig()
'type' => Type::string(),
],
]);
$mock->method('resolveFieldA')
$mock->method('resolveFieldAField')
->willReturn('method');

$fields = $mock->getFields();
Expand All @@ -86,9 +78,7 @@ public function testGetFieldsUsesResolveConfig()

public function testGetFieldsUsesResolverMethod()
{
$mock = $this->getMockBuilder(TypeCreator::class)
->setMethods(['fields','resolveFieldAField'])
->getMock();
$mock = $this->getTypeCreatorMock(['resolveFieldAField', 'fields']);
$mock->method('fields')->willReturn([
'fieldA' => [
'type' => Type::string(),
Expand All @@ -97,7 +87,7 @@ public function testGetFieldsUsesResolverMethod()
'type' => Type::string(),
],
]);
$mock->method('resolveFieldA')
$mock->method('resolveFieldAField')
->willReturn('resolved');

$fields = $mock->getFields();
Expand All @@ -109,9 +99,7 @@ public function testGetFieldsUsesResolverMethod()

public function testGetFieldsUsesAllFieldsResolverMethod()
{
$mock = $this->getMockBuilder(TypeCreator::class)
->setMethods(['fields','resolveField'])
->getMock();
$mock = $this->getTypeCreatorMock(['resolveField', 'fields']);
$mock->method('fields')->willReturn([
'fieldA' => [
'type' => Type::string(),
Expand All @@ -131,4 +119,26 @@ public function testGetFieldsUsesAllFieldsResolverMethod()
$this->assertEquals('resolved', $fields['fieldA']['resolve']());
$this->assertEquals('resolved', $fields['fieldB']['resolve']());
}

protected function getTypeCreatorMock($extraMethods = [])
{
$mock = $this->getMockBuilder(TypeCreator::class)
->setMethods(array_unique(array_merge(['fields', 'attributes'], $extraMethods)))
->getMock();

if(!in_array('fields', $extraMethods)) {
$mock->method('fields')->willReturn([
'ID' => [
'type' => Type::nonNull(Type::id()),
],
]);
}

if(!in_array('attributes', $extraMethods)) {
$mock->method('attributes')
->willReturn(['name' => 'myType']);
}

return $mock;
}
}

0 comments on commit 690a699

Please sign in to comment.