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

SchemaPluginBase   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 124
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
dl 0
loc 124
rs 10
c 0
b 0
f 0
wmc 9
lcom 1
cbo 6

8 Methods

Rating   Name   Duplication   Size   Complexity  
A create() 0 8 1
A __construct() 0 4 1
A getSchemaBuilder() 0 3 1
A getSchema() 0 15 1
A extractDefinitions() 0 5 1
A getMutations() 0 5 1
A getRootFields() 0 7 2
A getTypes() 0 9 1
1
<?php
2
3
namespace Drupal\graphql\Plugin\GraphQL\Schemas;
4
5
use Drupal\Component\Plugin\PluginBase;
6
use Drupal\Core\DependencyInjection\DependencySerializationTrait;
7
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
8
use Drupal\graphql\GraphQL\Schema\Schema;
9
use Drupal\graphql\Plugin\GraphQL\PluggableSchemaBuilder;
10
use Drupal\graphql\Plugin\GraphQL\PluggableSchemaPluginInterface;
11
use Drupal\graphql\Plugin\GraphQL\TypeSystemPluginInterface;
12
use Drupal\graphql\Plugin\GraphQL\TypeSystemPluginManagerAggregator;
13
use Symfony\Component\DependencyInjection\ContainerInterface;
14
use Youshido\GraphQL\Schema\InternalSchemaMutationObject;
15
use Youshido\GraphQL\Schema\InternalSchemaQueryObject;
16
17
abstract class SchemaPluginBase extends PluginBase implements PluggableSchemaPluginInterface, ContainerFactoryPluginInterface {
18
19
  use DependencySerializationTrait;
20
21
  /**
22
   * The schema builder object.
23
   *
24
   * @var \Drupal\graphql\Plugin\GraphQL\PluggableSchemaBuilderInterface
25
   */
26
  protected $schemaBuilder;
27
28
  /**
29
   * {@inheritdoc}
30
   */
31
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
32
    return new static(
33
      $configuration,
34
      $plugin_id,
35
      $plugin_definition,
36
      $container->get('graphql.plugin_manager_aggregator')
37
    );
38
  }
39
40
  /**
41
   * SchemaPluginBase constructor.
42
   *
43
   * @param array $configuration
44
   *   The plugin configuration array.
45
   * @param string $pluginId
46
   *   The plugin id.
47
   * @param array $pluginDefinition
48
   *   The plugin definition array.
49
   * @param \Drupal\graphql\Plugin\GraphQL\TypeSystemPluginManagerAggregator $pluginManagers
50
   *   Type system plugin manager aggregator service.
51
   */
52
  public function __construct($configuration, $pluginId, $pluginDefinition, TypeSystemPluginManagerAggregator $pluginManagers) {
53
    parent::__construct($configuration, $pluginId, $pluginDefinition);
54
    $this->schemaBuilder = new PluggableSchemaBuilder($pluginManagers);
55
  }
56
57
  /**
58
   * {@inheritdoc}
59
   */
60
  public function getSchemaBuilder() {
61
    return $this->schemaBuilder;
62
  }
63
64
  /**
65
   * {@inheritdoc}
66
   */
67
  public function getSchema() {
68
    $mutation = new InternalSchemaMutationObject(['name' => 'RootMutation']);
69
    $mutation->addFields($this->extractDefinitions(($this->getMutations())));
70
71
    $query = new InternalSchemaQueryObject(['name' => 'RootQuery']);
72
    $query->addFields($this->extractDefinitions($this->getRootFields()));
73
74
    $types = $this->extractDefinitions($this->getTypes());
75
76
    return new Schema($this, [
77
      'query' => $query,
78
      'mutation' => $mutation,
79
      'types' => $types,
80
    ]);
81
  }
82
83
  /**
84
   * Extract type or field definitions from plugins.
85
   *
86
   * @param array $plugins
87
   *   The list of plugins to extract the type or field definitions from.
88
   *
89
   * @return array
90
   *   The list of extracted type or field definitions.
91
   */
92
  protected function extractDefinitions(array $plugins) {
93
    return array_map(function (TypeSystemPluginInterface $plugin) {
94
      return $plugin->getDefinition($this->schemaBuilder);
95
    }, $plugins);
96
  }
97
98
  /**
99
   * Retrieve all mutations.
100
   *
101
   * @return \Drupal\graphql\Plugin\GraphQL\TypeSystemPluginInterface[]
102
   *   The list of mutation plugins.
103
   */
104
  protected function getMutations() {
105
    return $this->schemaBuilder->find(function() {
106
      return TRUE;
107
    }, [GRAPHQL_MUTATION_PLUGIN]);
108
  }
109
110
  /**
111
   * Retrieve all fields that are not associated with a specific type.
112
   *
113
   * @return \Drupal\graphql\Plugin\GraphQL\TypeSystemPluginInterface[]
114
   *   The list root field plugins.
115
   */
116
  protected function getRootFields() {
117
    // Retrieve the list of fields that are not attached to any type or are
118
    // explicitly attached to the artificial "Root" type.
119
    return $this->schemaBuilder->find(function($definition) {
120
      return empty($definition['parents']) || in_array('Root', $definition['parents']);
121
    }, [GRAPHQL_FIELD_PLUGIN]);
122
  }
123
124
  /**
125
   * Retrieve all types to be registered explicitly.
126
   *
127
   * @return \Drupal\graphql\Plugin\GraphQL\TypeSystemPluginInterface[]
128
   *   The list of types to be registered explicitly.
129
   */
130
  protected function getTypes() {
131
    return $this->schemaBuilder->find(function() {
132
      return TRUE;
133
    }, [
134
      GRAPHQL_UNION_TYPE_PLUGIN,
135
      GRAPHQL_TYPE_PLUGIN,
136
      GRAPHQL_INPUT_TYPE_PLUGIN,
137
    ]);
138
  }
139
140
}
141