You are here

public static function TypePluginBase::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/Types/TypePluginBase.php, line 23

Class

TypePluginBase

Namespace

Drupal\graphql\Plugin\GraphQL\Types

Code

public static function createInstance(SchemaBuilderInterface $builder, TypePluginManager $manager, $definition, $id) {
  return new ObjectType([
    'name' => $definition['name'],
    'description' => $definition['description'],
    'contexts' => $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;
    },
    'interfaces' => function () use ($builder, $definition) {
      return array_filter(array_map(function ($name) use ($builder) {
        return $builder
          ->getType($name);
      }, $definition['interfaces']), function ($type) {
        return $type instanceof InterfaceType;
      });
    },
    'isTypeOf' => function ($object, $context, ResolveInfo $info) use ($manager, $id) {
      $instance = $manager
        ->getInstance([
        'id' => $id,
      ]);
      return $instance
        ->applies($object, $context, $info);
    },
  ]);
}