You are here

protected function PluggableSchemaDeriver::buildTypeMap in GraphQL 8.3

Builds an optimized map of types registered with any of the type managers.

Parameters

array $managers: The registered type plugin managers.

Return value

array The optimized representation/registry of type definitions.

1 call to PluggableSchemaDeriver::buildTypeMap()
PluggableSchemaDeriver::getDerivativeDefinitions in src/Plugin/Deriver/PluggableSchemaDeriver.php
Gets the definition of all derivatives of a base plugin.

File

src/Plugin/Deriver/PluggableSchemaDeriver.php, line 146

Class

PluggableSchemaDeriver

Namespace

Drupal\graphql\Plugin\Deriver

Code

protected function buildTypeMap(array $managers) {

  // First collect all definitions by their name, overwriting those with
  // lower weights by their higher weighted counterparts. We also collect
  // the class from the plugin definition to be able to statically create
  // the type instance without loading the plugin managers at all at
  // run-time.
  $types = array_reduce(array_keys($managers), function ($carry, $type) use ($managers) {
    $manager = $managers[$type];
    $definitions = $manager
      ->getDefinitions();
    return array_reduce(array_keys($definitions), function ($carry, $id) use ($type, $definitions) {
      $current = $definitions[$id];
      $name = $current['name'];
      if (empty($carry[$name]) || $carry[$name]['weight'] < $current['weight']) {
        $carry[$name] = [
          'type' => $type,
          'id' => $id,
          'class' => $current['class'],
          'weight' => !empty($current['weight']) ? $current['weight'] : 0,
          'reference' => !empty($current['type']) ? $current['type'] : NULL,
        ];
      }
      return $carry;
    }, $carry);
  }, []);

  // Retrieve the plugins run-time definitions. These will help us to prevent
  // plugin instantiation at run-time unless a plugin is actually called from
  // the graphql query execution. Plugins should take care of not having to
  // instantiate their plugin instances during schema composition.
  return array_map(function ($type) use ($managers) {
    $manager = $managers[$type['type']];

    /** @var \Drupal\graphql\Plugin\TypePluginInterface $instance */
    $instance = $manager
      ->getInstance([
      'id' => $type['id'],
    ]);
    return $type + [
      'definition' => $instance
        ->getDefinition(),
    ] + $type;
  }, $types);
}