Пример #1
0
 public function __construct(array $config)
 {
     Config::validate($config, ['name' => Config::STRING, 'fields' => Config::arrayOf(FieldDefinition::getDefinition(), Config::KEY_AS_NAME), 'resolveType' => Config::CALLBACK, 'description' => Config::STRING]);
     $this->name = $config['name'];
     $this->description = isset($config['description']) ? $config['description'] : null;
     $this->_fields = !empty($config['fields']) ? FieldDefinition::createMap($config['fields']) : [];
     $this->_resolveTypeFn = isset($config['resolveType']) ? $config['resolveType'] : null;
 }
Пример #2
0
 /**
  * @return FieldDefinition[]
  */
 public function getFields()
 {
     if (null === $this->fields) {
         $this->fields = [];
         $fields = isset($this->config['fields']) ? $this->config['fields'] : [];
         $fields = is_callable($fields) ? call_user_func($fields) : $fields;
         $this->fields = FieldDefinition::createMap($fields, $this->name);
     }
     return $this->fields;
 }
Пример #3
0
 public function __construct(array $config)
 {
     Config::validate($config, ['name' => Config::STRING | Config::REQUIRED, 'fields' => Config::arrayOf(FieldDefinition::getDefinition(), Config::KEY_AS_NAME), 'description' => Config::STRING, 'interfaces' => Config::arrayOf(Config::INTERFACE_TYPE), 'isTypeOf' => Config::CALLBACK]);
     $this->name = $config['name'];
     $this->description = isset($config['description']) ? $config['description'] : null;
     if (isset($config['fields'])) {
         $this->_fields = FieldDefinition::createMap($config['fields']);
     }
     $this->_interfaces = isset($config['interfaces']) ? $config['interfaces'] : [];
     $this->_isTypeOf = isset($config['isTypeOf']) ? $config['isTypeOf'] : null;
     if (!empty($this->_interfaces)) {
         InterfaceType::addImplementationToInterfaces($this, $this->_interfaces);
     }
 }
Пример #4
0
 public static function typeNameMetaFieldDef()
 {
     if (!isset(self::$map['__typename'])) {
         self::$map['__typename'] = FieldDefinition::create(['name' => '__typename', 'type' => Type::nonNull(Type::string()), 'description' => 'The name of the current Object type at runtime.', 'args' => [], 'resolve' => function ($source, $args, $context, ResolveInfo $info) {
             return $info->parentType->name;
         }]);
     }
     return self::$map['__typename'];
 }
Пример #5
0
 /**
  * Late instance initialization
  */
 private function initialize()
 {
     if ($this->_initialized) {
         return;
     }
     $config = $this->_config;
     if (isset($config['fields']) && is_callable($config['fields'])) {
         $config['fields'] = call_user_func($config['fields']);
     }
     if (isset($config['interfaces']) && is_callable($config['interfaces'])) {
         $config['interfaces'] = call_user_func($config['interfaces']);
     }
     // Note: this validation is disabled by default, because it is resource-consuming
     // TODO: add bin/validate script to check if schema is valid during development
     Config::validate($this->_config, ['name' => Config::STRING | Config::REQUIRED, 'fields' => Config::arrayOf(FieldDefinition::getDefinition(), Config::KEY_AS_NAME), 'description' => Config::STRING, 'interfaces' => Config::arrayOf(Config::INTERFACE_TYPE), 'isTypeOf' => Config::CALLBACK, 'resolveField' => Config::CALLBACK]);
     $this->_fields = FieldDefinition::createMap($config['fields']);
     $this->_interfaces = isset($config['interfaces']) ? $config['interfaces'] : [];
     $this->_isTypeOf = isset($config['isTypeOf']) ? $config['isTypeOf'] : null;
     $this->_initialized = true;
 }
Пример #6
0
 /**
  * Resolves the field on the given source object. In particular, this
  * figures out the object that the field returns using the resolve function,
  * then calls completeField to coerce scalars or execute the sub
  * selection set for objects.
  */
 private static function resolveFieldOrError(ExecutionContext $exeContext, ObjectType $parentType, $source, $fieldASTs, FieldDefinition $fieldDef)
 {
     $fieldAST = $fieldASTs[0];
     $fieldType = $fieldDef->getType();
     $resolveFn = $fieldDef->resolve ?: [__CLASS__, 'defaultResolveFn'];
     // Build a JS object of arguments from the field.arguments AST, using the
     // variables scope to fulfill any variable references.
     // TODO: find a way to memoize, in case this field is within a Array type.
     $args = Values::getArgumentValues($fieldDef->args, $fieldAST->arguments, $exeContext->variables);
     try {
         $result = call_user_func($resolveFn, $source, $args, $exeContext->root, $fieldAST, $fieldType, $parentType, $exeContext->schema);
     } catch (\Exception $error) {
         throw new Error($error->getMessage(), [$fieldAST], $error->getTrace());
     }
     return self::completeField($exeContext, $fieldType, $fieldASTs, $result);
 }