Completed
Pull Request — 8.x-3.x (#525)
by Philipp
02:33
created

SchemaPluginBase   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 75
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 75
rs 10
wmc 5
lcom 1
cbo 4

3 Methods

Rating   Name   Duplication   Size   Complexity  
A create() 0 8 1
A __construct() 0 4 1
B getSchema() 0 31 3
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\Plugin\SchemaBuilder;
9
use Drupal\graphql\Plugin\SchemaPluginInterface;
10
use GraphQL\Type\Definition\ObjectType;
11
use GraphQL\Type\Schema;
12
use GraphQL\Type\SchemaConfig;
13
use Symfony\Component\DependencyInjection\ContainerInterface;
14
15
abstract class SchemaPluginBase extends PluginBase implements SchemaPluginInterface, ContainerFactoryPluginInterface {
16
  use DependencySerializationTrait;
17
18
  /**
19
   * The schema builder service.
20
   *
21
   * @var \Drupal\graphql\Plugin\SchemaBuilder
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.schema_builder')
34
    );
35
  }
36
37
  /**
38
   * SchemaPluginBase constructor.
39
   *
40
   * @param array $configuration
41
   *   The plugin configuration array.
42
   * @param string $pluginId
43
   *   The plugin id.
44
   * @param array $pluginDefinition
45
   *   The plugin definition array.
46
   * @param \Drupal\graphql\Plugin\SchemaBuilder $schemaBuilder
47
   *   The schema builder service.
48
   */
49
  public function __construct($configuration, $pluginId, $pluginDefinition, SchemaBuilder $schemaBuilder) {
50
    parent::__construct($configuration, $pluginId, $pluginDefinition);
51
    $this->schemaBuilder = $schemaBuilder;
52
  }
53
54
  /**
55
   * {@inheritdoc}
56
   */
57
  public function getSchema() {
58
    $config = new SchemaConfig();
59
60
    if ($this->schemaBuilder->hasMutations()) {
61
      $config->setMutation(new ObjectType([
62
        'name' => 'MutationRoot',
63
        'fields' => function () {
64
          return $this->schemaBuilder->getMutations();
65
        }
66
      ]));
67
    }
68
69
    if ($this->schemaBuilder->hasFields('Root')) {
70
      $config->setQuery(new ObjectType([
71
        'name' => 'QueryRoot',
72
        'fields' => function () {
73
          return $this->schemaBuilder->getFields('Root');
74
        }
75
      ]));
76
    }
77
78
    $config->setTypes(function () {
79
      return $this->schemaBuilder->getTypes();
80
    });
81
82
    $config->setTypeLoader(function ($name) {
83
      return $this->schemaBuilder->getType($name);
84
    });
85
86
    return new Schema($config);
87
  }
88
89
}
90