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

SchemaPluginBase   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 73
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
A create() 0 8 1
A __construct() 0 4 1
B getSchema() 0 29 2
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
    $config->setQuery(new ObjectType([
70
      'name' => 'QueryRoot',
71
      'fields' => function () {
72
        return $this->schemaBuilder->getFields('Root');
73
      },
74
    ]));
75
76
    $config->setTypes(function () {
77
      return $this->schemaBuilder->getTypes();
78
    });
79
80
    $config->setTypeLoader(function ($name) {
81
      return $this->schemaBuilder->getType($name);
82
    });
83
84
    return new Schema($config);
85
  }
86
87
}
88