Completed
Pull Request — 8.x-3.x (#473)
by Sebastian
02:05
created

SchemaPluginBase::extractDefinitions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 1
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
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\SchemaPluginInterface;
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 SchemaPluginInterface, 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 getSchema() {
61
    $mutation = new InternalSchemaMutationObject(['name' => 'RootMutation']);
62
    $mutation->addFields($this->extractDefinitions(($this->getMutations())));
63
64
    $query = new InternalSchemaQueryObject(['name' => 'RootQuery']);
65
    $query->addFields($this->extractDefinitions($this->getRootFields()));
66
67
    $types = $this->extractDefinitions($this->getTypes());
68
69
    return new Schema([
70
      'query' => $query,
71
      'mutation' => $mutation,
72
      'types' => $types,
73
    ]);
74
  }
75
76
  /**
77
   * Extract type or field definitions from plugins.
78
   *
79
   * @param array $plugins
80
   *   The list of plugins to extract the type or field definitions from.
81
   *
82
   * @return array
83
   *   The list of extracted type or field definitions.
84
   */
85
  protected function extractDefinitions(array $plugins) {
86
    return array_map(function (TypeSystemPluginInterface $plugin) {
87
      return $plugin->getDefinition($this->schemaBuilder);
88
    }, $plugins);
89
  }
90
91
  /**
92
   * Retrieve all mutations.
93
   *
94
   * @return \Drupal\graphql\Plugin\GraphQL\TypeSystemPluginInterface[]
95
   *   The list of mutation plugins.
96
   */
97
  protected function getMutations() {
98
    return $this->schemaBuilder->find(function() {
99
      return TRUE;
100
    }, [GRAPHQL_MUTATION_PLUGIN]);
101
  }
102
103
  /**
104
   * Retrieve all fields that are not associated with a specific type.
105
   *
106
   * @return \Drupal\graphql\Plugin\GraphQL\TypeSystemPluginInterface[]
107
   *   The list root field plugins.
108
   */
109
  protected function getRootFields() {
110
    // Retrieve the list of fields that are not attached to any type or are
111
    // explicitly attached to the artificial "Root" type.
112
    return $this->schemaBuilder->find(function($definition) {
113
      return empty($definition['parents']) || in_array('Root', $definition['parents']);
114
    }, [GRAPHQL_FIELD_PLUGIN]);
115
  }
116
117
  /**
118
   * Retrieve all types to be registered explicitly.
119
   *
120
   * @return \Drupal\graphql\Plugin\GraphQL\TypeSystemPluginInterface[]
121
   *   The list of types to be registered explicitly.
122
   */
123
  protected function getTypes() {
124
    return $this->schemaBuilder->find(function() {
125
      return TRUE;
126
    }, [
127
      GRAPHQL_UNION_TYPE_PLUGIN,
128
      GRAPHQL_TYPE_PLUGIN,
129
      GRAPHQL_INPUT_TYPE_PLUGIN,
130
    ]);
131
  }
132
133
}
134