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

SchemaPluginBase::getSchema()   B

Complexity

Conditions 3
Paths 4

Size

Total Lines 31
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 15
nc 4
nop 0
dl 0
loc 31
rs 8.8571
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\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 () {
0 ignored issues
show
Unused Code Comprehensibility introduced by
61% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
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