forked from silverstripe/silverstripe-graphql
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
180 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
<?php | ||
|
||
namespace Chillu\GraphQL\Resolver; | ||
|
||
use GraphQL\Type\Definition\ResolveInfo; | ||
use SilverStripe\ORM\DataObject; | ||
use SilverStripe\Core\ClassInfo; | ||
|
||
/** | ||
* Infer original field name casing from case insensitive database field comparison. | ||
* Useful counterpart to {@link \Convert::upperCamelToLowerCamel()}. | ||
* | ||
* Caution: Assumes fields have been whitelisted through GraphQL type definitions already. | ||
* Does not perform any canView() checks or further validation. | ||
*/ | ||
class DataObjectLowerCamelResolver implements IResolver { | ||
|
||
public function resolve($object, array $args, $context, ResolveInfo $info) | ||
{ | ||
// Correct case for methods (e.g. canView) | ||
if($object->hasMethod($info->fieldName)) { | ||
return $object->{$info->fieldName}(); | ||
} | ||
|
||
// Correct case (and getters) | ||
if($object->hasField($info->fieldName)) { | ||
return $object->{$info->fieldName}; | ||
} | ||
|
||
// Infer casing | ||
if($object instanceof DataObject) { | ||
$parents = ClassInfo::ancestry($object, true); | ||
foreach($parents as $parent) { | ||
$fields = DataObject::database_fields($parent); | ||
foreach($fields as $fieldName => $fieldClass) { | ||
if(strcasecmp($fieldName, $info->fieldName) === 0) { | ||
return $object->getField($fieldName); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php | ||
|
||
namespace Chillu\GraphQL\Resolver; | ||
|
||
use GraphQL\Type\Definition\ResolveInfo; | ||
|
||
interface IResolver { | ||
|
||
/** | ||
* @param mixed $object The parent resolved object | ||
* @param array $args Input arguments | ||
* @param mixed $context The context object hat was passed to GraphQL::execute | ||
* @param ResolveInfo $info ResolveInfo object | ||
* @return mixed | ||
*/ | ||
public function resolve($object, array $args, $context, ResolveInfo $info); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?php | ||
|
||
namespace Chillu\GraphQL\Tests; | ||
|
||
use SilverStripe\Dev\TestOnly; | ||
use SilverStripe\ORM\DataObject; | ||
|
||
class DataObjectFake extends DataObject implements TestOnly | ||
{ | ||
public function getCustomGetter() | ||
{ | ||
return 'customGetterValue'; | ||
} | ||
|
||
public function customMethod() | ||
{ | ||
return 'customMethodValue'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
<?php | ||
|
||
namespace Chillu\GraphQL\Tests\Resolver; | ||
|
||
use Chillu\GraphQL\Resolver\DataObjectLowerCamelResolver; | ||
use Chillu\GraphQL\Tests\DataObjectFake; | ||
use GraphQL\Type\Definition\ResolveInfo; | ||
use SilverStripe\Dev\SapphireTest; | ||
|
||
class DataObjectLowerCamelResolverTest extends SapphireTest | ||
{ | ||
|
||
public function testResolvesOriginalCasing() | ||
{ | ||
$fake = new DataObjectFake([ | ||
'ID' => 99 | ||
]); | ||
$resolver = new DataObjectLowerCamelResolver(); | ||
$info = new ResolveInfo([ | ||
'fieldName' => 'ID' | ||
]); | ||
$this->assertEquals(99, $resolver->resolve($fake, [], [], $info)); | ||
} | ||
|
||
public function testResolvesDifferentCasing() | ||
{ | ||
$fake = new DataObjectFake([ | ||
'ID' => 99 | ||
]); | ||
$resolver = new DataObjectLowerCamelResolver(); | ||
$info = new ResolveInfo([ | ||
'fieldName' => 'id' // lowercase | ||
]); | ||
$this->assertEquals(99, $resolver->resolve($fake, [], [], $info)); | ||
} | ||
|
||
public function testResolvesCustomGetter() | ||
{ | ||
$fake = new DataObjectFake([]); | ||
$resolver = new DataObjectLowerCamelResolver(); | ||
$info = new ResolveInfo([ | ||
'fieldName' => 'customGetter' | ||
]); | ||
$this->assertEquals('customGetterValue', $resolver->resolve($fake, [], [], $info)); | ||
} | ||
|
||
public function testResolvesMethod() | ||
{ | ||
$fake = new DataObjectFake([]); | ||
$resolver = new DataObjectLowerCamelResolver(); | ||
$info = new ResolveInfo([ | ||
'fieldName' => 'customMethod' | ||
]); | ||
$this->assertEquals('customMethodValue', $resolver->resolve($fake, [], [], $info)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters