Ability to set type-level "resolveField" method

This commit is contained in:
vladar 2015-08-30 13:43:55 +06:00
parent 5241c8a5d3
commit 003fa005ed
2 changed files with 14 additions and 7 deletions

View File

@ -47,7 +47,7 @@ class Executor
{
private static $UNDEFINED;
private static $defaultResolveFn;
private static $defaultResolveFn = [__CLASS__, 'defaultResolveFn'];
/**
* Custom default resolve function
@ -365,10 +365,10 @@ class Executor
if (isset($fieldDef->resolve)) {
$resolveFn = $fieldDef->resolve;
} else if (isset(self::$defaultResolveFn)) {
$resolveFn = self::$defaultResolveFn;
} else if (isset($parentType->resolveField)) {
$resolveFn = $parentType->resolveField;
} else {
$resolveFn = [__CLASS__, 'defaultResolveFn'];
$resolveFn = self::$defaultResolveFn;
}
// Build hash of arguments from the field.arguments AST, using the
@ -577,12 +577,12 @@ class Executor
$property = $source[$fieldName];
}
} else if (is_object($source)) {
if (property_exists($source, $fieldName)) {
if (isset($source->{$fieldName})) {
$property = $source->{$fieldName};
}
}
return is_callable($property) ? call_user_func($property, $source) : $property;
return $property instanceof \Closure ? $property($source) : $property;
}
/**

View File

@ -66,12 +66,18 @@ class ObjectType extends Type implements OutputType, CompositeType
private $_initialized = false;
/**
* @var callable
*/
public $resolveField;
public function __construct(array $config)
{
Utils::invariant(!empty($config['name']), 'Every type is expected to have name');
$this->name = $config['name'];
$this->description = isset($config['description']) ? $config['description'] : null;
$this->resolveField = isset($config['resolveField']) ? $config['resolveField'] : null;
$this->_config = $config;
if (isset($config['interfaces'])) {
@ -109,6 +115,7 @@ class ObjectType extends Type implements OutputType, CompositeType
Config::INTERFACE_TYPE
),
'isTypeOf' => Config::CALLBACK, // ($value, ResolveInfo $info) => boolean
'resolveField' => Config::CALLBACK
]);
$this->_fields = FieldDefinition::createMap($config['fields']);
@ -118,7 +125,7 @@ class ObjectType extends Type implements OutputType, CompositeType
}
/**
* @return array<FieldDefinition>
* @return FieldDefinition[]
*/
public function getFields()
{