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

getSchemaConfiguration()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 1
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Drupal\graphql\Plugin\GraphQL\Schemas;
4
5
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
6
use Drupal\graphql\Plugin\GraphQL\PluggableSchemaManagerInterface;
7
use Drupal\graphql\Plugin\GraphQL\PluggableSchemaPluginInterface;
8
use Symfony\Component\DependencyInjection\ContainerInterface;
9
use Youshido\GraphQL\Schema\InternalSchemaMutationObject;
10
use Youshido\GraphQL\Schema\InternalSchemaQueryObject;
11
12
abstract class PluggableSchemaPluginBase extends SchemaPluginBase implements PluggableSchemaPluginInterface, ContainerFactoryPluginInterface {
13
14
  /**
15
   * @param \Drupal\graphql\Plugin\GraphQL\PluggableSchemaManagerInterface $schemaManager
16
   * @return \Youshido\GraphQL\Schema\InternalSchemaMutationObject
17
   */
18
  protected static function getSchemaMutationObject(PluggableSchemaManagerInterface $schemaManager) {
19
    $mutation = new InternalSchemaMutationObject(['name' => 'RootMutation']);
20
    $mutation->addFields($schemaManager->getMutations());
21
22
    return $mutation;
23
  }
24
25
  /**
26
   * @param \Drupal\graphql\Plugin\GraphQL\PluggableSchemaManagerInterface $schemaManager
27
   * @return \Youshido\GraphQL\Schema\InternalSchemaQueryObject
28
   */
29
  protected static function getSchemaQueryObject(PluggableSchemaManagerInterface $schemaManager) {
30
    $query = new InternalSchemaQueryObject(['name' => 'RootQuery']);
31
    $query->addFields($schemaManager->getRootFields());
32
33
    return $query;
34
  }
35
36
  /**
37
   * @param \Drupal\graphql\Plugin\GraphQL\PluggableSchemaManagerInterface $schemaManager
38
   * @return \object[]
39
   */
40
  protected static function getSchemaTypesList(PluggableSchemaManagerInterface $schemaManager) {
41
    $types = $schemaManager->find(function() {
42
      return TRUE;
43
    }, [
44
      GRAPHQL_UNION_TYPE_PLUGIN,
45
      GRAPHQL_TYPE_PLUGIN,
46
      GRAPHQL_INPUT_TYPE_PLUGIN,
47
    ]);
48
49
    return $types;
50
  }
51
52
  /**
53
   * @param \Drupal\graphql\Plugin\GraphQL\PluggableSchemaManagerInterface $schemaManager
54
   * @return array
55
   */
56
  protected static function getSchemaConfiguration(PluggableSchemaManagerInterface $schemaManager) {
57
    return [
58
      'query' => static::getSchemaQueryObject($schemaManager),
59
      'mutation' => static::getSchemaMutationObject($schemaManager),
60
      'types' => static::getSchemaTypesList($schemaManager),
61
    ];
62
  }
63
64
  /**
65
   * {@inheritdoc}
66
   */
67
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
68
    /** @var \Drupal\graphql\Plugin\GraphQL\PluggableSchemaManager $schemaManager */
69
    $schemaManager = $container->get('graphql.pluggable_schema_manager');
70
    $schemaConfig = static::getSchemaConfiguration($schemaManager);
71
72
    return new static($configuration + ['schema' => $schemaConfig], $plugin_id, $plugin_definition);
73
  }
74
}
75