You are here

public static function InterfacePluginBase::createInstance in GraphQL 8.3

Parameters

\Drupal\graphql\Plugin\SchemaBuilderInterface $builder:

\Drupal\graphql\Plugin\TypePluginManager $manager:

$definition:

$id:

Return value

mixed

Overrides TypePluginInterface::createInstance

File

src/Plugin/GraphQL/Interfaces/InterfacePluginBase.php, line 21

Class

InterfacePluginBase

Namespace

Drupal\graphql\Plugin\GraphQL\Interfaces

Code

public static function createInstance(SchemaBuilderInterface $builder, TypePluginManager $manager, $definition, $id) {
  return new InterfaceType([
    'name' => $definition['name'],
    'description' => $definition['description'],
    'contexts' => function () use ($builder, $definition) {
      $types = $builder
        ->getSubTypes($definition['name']);
      return array_reduce($types, function ($carry, $current) use ($builder) {
        $type = $builder
          ->getType($current);
        if (!empty($type->config['contexts'])) {
          $contexts = $type->config['contexts'];
          $contexts = is_callable($contexts) ? $contexts() : $contexts;
          return Cache::mergeContexts($carry, $contexts);
        }
        return $carry;
      }, $definition['contexts']);
    },
    'fields' => function () use ($builder, $definition) {
      $fields = $builder
        ->getFields($definition['name']);
      if (!empty($definition['interfaces'])) {
        $inherited = array_map(function ($name) use ($builder) {
          return $builder
            ->getFields($name);
        }, $definition['interfaces']);
        $inherited = call_user_func_array('array_merge', $inherited);
        return array_merge($inherited, $fields);
      }
      return $fields;
    },
    'resolveType' => function ($value, $context, $info) use ($builder, $definition) {
      return $builder
        ->resolveType($definition['name'], $value, $context, $info);
    },
  ]);
}