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