Completed
Push — 8.x-3.x ( e67c0e...2e01b0 )
by Philipp
02:27
created

UnionTypePluginBase::resolveType()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 5
nc 3
nop 1
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Drupal\graphql\Plugin\GraphQL\Unions;
4
5
use Drupal\Component\Plugin\PluginBase;
6
use Drupal\graphql\GraphQL\Type\UnionType;
7
use Drupal\graphql\Plugin\GraphQL\PluggableSchemaBuilderInterface;
8
use Drupal\graphql\Plugin\GraphQL\Traits\CacheablePluginTrait;
9
use Drupal\graphql\Plugin\GraphQL\Traits\NamedPluginTrait;
10
use Drupal\graphql\Plugin\GraphQL\TypeSystemPluginInterface;
11
12
/**
13
 * Base class for GraphQL union type plugins.
14
 */
15
abstract class UnionTypePluginBase extends PluginBase implements TypeSystemPluginInterface {
16
  use CacheablePluginTrait;
17
  use NamedPluginTrait;
18
19
  /**
20
   * The type instance.
21
   *
22
   * @var \Drupal\graphql\GraphQL\Type\UnionType
23
   */
24
  protected $definition;
25
26
  /**
27
   * {@inheritdoc}
28
   */
29
  public function getDefinition(PluggableSchemaBuilderInterface $schemaBuilder) {
30
    if (!isset($this->definition)) {
31
      $name = $this->buildName();
32
33
      $this->definition = new UnionType($this, [
34
        'name' => $name,
35
        'description' => $this->buildDescription(),
36
        'types' => $this->buildTypes($schemaBuilder, $name),
37
      ]);
38
    }
39
40
    return $this->definition;
41
  }
42
43
  /**
44
   * Builds the list of types that are contained within this union type.
45
   *
46
   * @param \Drupal\graphql\Plugin\GraphQL\PluggableSchemaBuilderInterface $schemaBuilder
47
   *   The schema manager.
48
   * @param $name
49
   *   The name of this plugin.
50
   *
51
   * @return \Drupal\graphql\GraphQL\Type\ObjectType[]
52
   *   An array of types to add to this union type.
53
   */
54
  protected function buildTypes(PluggableSchemaBuilderInterface $schemaBuilder, $name) {
55
    /** @var \Drupal\graphql\GraphQL\Type\ObjectType[] $types */
56
    $types = array_map(function (TypeSystemPluginInterface $type) use ($schemaBuilder) {
57
      return $type->getDefinition($schemaBuilder);
58
    }, $schemaBuilder->find(function ($type) use ($name) {
59
      return in_array($name, $type['unions']);
60
    }, [
61
      GRAPHQL_TYPE_PLUGIN,
62
    ]));
63
64
    return $types;
65
  }
66
67
}
68