From 003fa005ede70d7f24ec53bb63c7290142ea2431 Mon Sep 17 00:00:00 2001 From: vladar Date: Sun, 30 Aug 2015 13:43:55 +0600 Subject: [PATCH] Ability to set type-level "resolveField" method --- src/Executor/Executor.php | 12 ++++++------ src/Type/Definition/ObjectType.php | 9 ++++++++- 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/src/Executor/Executor.php b/src/Executor/Executor.php index 983e720..73bf7f2 100644 --- a/src/Executor/Executor.php +++ b/src/Executor/Executor.php @@ -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; } /** diff --git a/src/Type/Definition/ObjectType.php b/src/Type/Definition/ObjectType.php index 8f67aac..dc9c444 100644 --- a/src/Type/Definition/ObjectType.php +++ b/src/Type/Definition/ObjectType.php @@ -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 + * @return FieldDefinition[] */ public function getFields() {