Skip to content

Commit

Permalink
Type resolver support
Browse files Browse the repository at this point in the history
  • Loading branch information
chillu committed Sep 17, 2016
1 parent 14d97f1 commit ccb12c0
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 5 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,3 +116,9 @@ This can also be expressed more cleanly as:
}
}
```

## Advanced

### Resolvers

TODO
33 changes: 31 additions & 2 deletions src/TypeCreator.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,19 @@ public function fields()
public function getFields()
{
$fields = $this->fields();
$allFields = [];

// TODO Field resolution
foreach($fields as $name => $field)
{
$resolver = $this->getFieldResolver($name, $field);
if($resolver)
{
$field['resolve'] = $resolver;
}
$allFields[$name] = $field;
}

return $fields;
return $allFields;
}

/**
Expand Down Expand Up @@ -70,4 +79,24 @@ public function getAttributes()
]
);
}

protected function getFieldResolver($name, $field)
{
$resolveMethod = 'resolve'.ucfirst($name).'Field';
if(isset($field['resolve']))
{
return $field['resolve'];
}
else if(method_exists($this, $resolveMethod))
{
$resolver = array($this, $resolveMethod);
return function() use ($resolver)
{
$args = func_get_args();
return call_user_func_array($resolver, $args);
};
}

return null;
}
}
54 changes: 51 additions & 3 deletions tests/TypeCreatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class TypeCreatorTest extends SapphireTest
{
public function testGetFields()
{
$mock = $this->getMockBuilder('SilverStripe\GraphQL\TypeCreator')
$mock = $this->getMockBuilder(TypeCreator::class)
->setMethods(['fields'])
->getMock();
$mock->method('fields')->willReturn([
Expand All @@ -25,7 +25,7 @@ public function testGetFields()

public function testToArray()
{
$mock = $this->getMockBuilder('SilverStripe\GraphQL\TypeCreator')
$mock = $this->getMockBuilder(TypeCreator::class)
->setMethods(['fields'])
->getMock();
$mock->method('fields')->willReturn([
Expand All @@ -43,7 +43,7 @@ public function testToArray()

public function testGetAttributes()
{
$mock = $this->getMockBuilder('SilverStripe\GraphQL\TypeCreator')
$mock = $this->getMockBuilder(TypeCreator::class)
->setMethods(['fields'])
->getMock();
$mock->method('fields')->willReturn([
Expand All @@ -58,4 +58,52 @@ public function testGetAttributes()
$fields = $actual['fields']();
$this->assertArrayHasKey('ID', $fields);
}

public function testGetFieldsUsesResolveConfig()
{
$mock = $this->getMockBuilder(TypeCreator::class)
->setMethods(['fields','resolveFieldAField'])
->getMock();
$mock->method('fields')->willReturn([
'fieldA' => [
'type' => Type::string(),
'resolve' => function() {return 'config';},
],
'fieldB' => [
'type' => Type::string(),
],
]);
$mock->method('resolveFieldA')
->willReturn('method');

$fields = $mock->getFields();
$this->assertArrayHasKey('fieldA', $fields);
$this->assertArrayHasKey('fieldB', $fields);
$this->assertArrayHasKey('resolve', $fields['fieldA']);
$this->assertArrayNotHasKey('resolve', $fields['fieldB']);
$this->assertEquals('config', $fields['fieldA']['resolve']());
}

public function testGetFieldsUsesResolverMethod()
{
$mock = $this->getMockBuilder(TypeCreator::class)
->setMethods(['fields','resolveFieldAField'])
->getMock();
$mock->method('fields')->willReturn([
'fieldA' => [
'type' => Type::string(),
],
'fieldB' => [
'type' => Type::string(),
],
]);
$mock->method('resolveFieldA')
->willReturn('resolved');

$fields = $mock->getFields();
$this->assertArrayHasKey('fieldA', $fields);
$this->assertArrayHasKey('fieldB', $fields);
$this->assertArrayHasKey('resolve', $fields['fieldA']);
$this->assertArrayNotHasKey('resolve', $fields['fieldB']);
}
}

0 comments on commit ccb12c0

Please sign in to comment.