You are here

protected function PluggableSchemaDeriver::buildTypeAssociationMap in GraphQL 8.3

Builds an optimized representation of type and composite type relations.

Parameters

array $types: The optimized list of types.

Return value

array The optimized list of types and their associated unions/interfaces.

1 call to PluggableSchemaDeriver::buildTypeAssociationMap()
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 298

Class

PluggableSchemaDeriver

Namespace

Drupal\graphql\Plugin\Deriver

Code

protected function buildTypeAssociationMap(array $types) {
  $assocations = array_filter(array_map(function ($type) use ($types) {

    // If this is an object type, just return a mapping for it's interfaces.
    if ($type['type'] === 'type') {
      return array_map(function () use ($type) {
        return [
          $type['definition']['name'],
        ];
      }, array_flip($type['definition']['interfaces']));
    }

    // For interfaces, find all object types that declare to implement it.
    if ($type['type'] === 'interface') {
      return [
        $type['definition']['name'] => array_values(array_map(function ($type) {
          return $type['definition']['name'];
        }, array_filter($types, function ($subType) use ($type) {
          return $subType['type'] === 'type' && in_array($type['definition']['name'], $subType['definition']['interfaces']);
        }))),
      ];
    }

    // Union types combine the two approaches above.
    if ($type['type'] === 'union') {
      $explicit = $type['definition']['types'];
      $implicit = array_values(array_map(function ($type) {
        return $type['definition']['name'];
      }, array_filter($types, function ($subType) use ($type) {
        return $subType['type'] === 'type' && in_array($type['definition']['name'], $subType['definition']['unions']);
      })));
      return [
        $type['definition']['name'] => array_merge($explicit, $implicit),
      ];
    }
    return [];
  }, $types));
  $assocations = array_map('array_unique', array_reduce($assocations, 'array_merge_recursive', []));
  $assocations = array_map(function ($parent) use ($types) {
    $children = array_map(function ($child) use ($types) {
      return $types[$child] + [
        'name' => $child,
      ];
    }, $parent);
    uasort($children, [
      SortArray::class,
      'sortByWeightElement',
    ]);
    $children = array_reverse($children);
    return array_map(function ($child) {
      return $child['name'];
    }, $children);
  }, $assocations);
  return $assocations;
}