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

SchemaPluginBase   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
A create() 0 8 1
A __construct() 0 4 1
B getSchema() 0 27 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\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
    $config->setMutation(new ObjectType([
61
      'name' => 'MutationRoot',
62
      'fields' => function () {
63
        return $this->schemaBuilder->getMutations();
64
      },
65
    ]));
66
67
    $config->setQuery(new ObjectType([
68
      'name' => 'QueryRoot',
69
      'fields' => function () {
70
        return $this->schemaBuilder->getFields('Root');
71
      },
72
    ]));
73
74
    $config->setTypes(function () {
75
      return $this->schemaBuilder->getTypes();
76
    });
77
78
    $config->setTypeLoader(function ($name) {
79
      return $this->schemaBuilder->getType($name);
80
    });
81
82
    return new Schema($config);
83
  }
84
85
}
86