You are here

public static function UnionTypePluginBase::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/Unions/UnionTypePluginBase.php, line 19

Class

UnionTypePluginBase

Namespace

Drupal\graphql\Plugin\GraphQL\Unions

Code

public static function createInstance(SchemaBuilderInterface $builder, TypePluginManager $manager, $definition, $id) {
  return new UnionType([
    'name' => $definition['name'],
    'description' => $definition['description'],
    'types' => function () use ($builder, $definition) {
      return array_map(function ($type) use ($builder) {
        if (!($type = $builder
          ->getType($type)) instanceof ObjectType) {
          throw new \LogicException('Union types can only reference object types.');
        }
        return $type;
      }, $builder
        ->getSubTypes($definition['name']));
    },
    'resolveType' => function ($value, $context, $info) use ($builder, $definition) {
      return $builder
        ->resolveType($definition['name'], $value, $context, $info);
    },
  ]);
}