You are here

protected function PluggableSchemaDeriver::buildFieldAssociationMap in GraphQL 8.3

Builds an optimized representation of fields keyed by their parent types.

Parameters

\Drupal\graphql\Plugin\FieldPluginManager $manager: The field plugin manager.

$types: The optimized list of types.

Return value

array The list of fields keyed by their parent types.

1 call to PluggableSchemaDeriver::buildFieldAssociationMap()
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 229

Class

PluggableSchemaDeriver

Namespace

Drupal\graphql\Plugin\Deriver

Code

protected function buildFieldAssociationMap(FieldPluginManager $manager, $types) {
  $definitions = $manager
    ->getDefinitions();
  $fields = array_reduce(array_keys($definitions), function ($carry, $id) use ($definitions, $types) {
    $current = $definitions[$id];
    $parents = $current['parents'] ?: [
      'Root',
    ];
    return array_reduce($parents, function ($carry, $parent) use ($current, $id, $types) {

      // Allow plugins to define a different name for each parent.
      if (strpos($parent, ':') !== FALSE) {
        list($parent, $name) = explode(':', $parent);
      }
      $name = isset($name) ? $name : $current['name'];
      if (empty($carry[$parent][$name]) || $carry[$parent][$name]['weight'] < $current['weight']) {
        $carry[$parent][$name] = [
          'id' => $id,
          'weight' => !empty($current['weight']) ? $current['weight'] : 0,
        ];
      }
      return $carry;
    }, $carry);
  }, []);
  $rename = [];
  foreach ($fields as $parent => $fieldList) {
    foreach ($fieldList as $field => $info) {
      if (!array_key_exists($parent, $types)) {
        continue;
      }
      foreach ($types[$parent]['definition']['interfaces'] as $interface) {
        if (isset($fields[$interface][$field]) && $definitions[$fields[$interface][$field]['id']]['type'] != $definitions[$info['id']]['type']) {
          $rename[$parent][$field] = TRUE;
        }
      }
    }
  }
  foreach ($rename as $parent => $names) {
    foreach (array_keys($names) as $name) {
      $fields[$parent][$name . 'Of' . $parent] = $fields[$parent][$name];
      unset($fields[$parent][$name]);
    }
  }

  // Only return fields for types that are actually fieldable.
  $fieldable = [
    GRAPHQL_TYPE_PLUGIN,
    GRAPHQL_INTERFACE_PLUGIN,
  ];
  $fields = array_intersect_key($fields, array_filter($types, function ($type) use ($fieldable) {
    return in_array($type['type'], $fieldable);
  }) + [
    'Root' => NULL,
  ]);

  // We only need the plugin ids in this map.
  return array_map(function ($fields) {
    return array_map(function ($field) {
      return $field['id'];
    }, $fields);
  }, $fields);
}