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

DefaultSchema::create()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 32
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 20
nc 1
nop 4
dl 0
loc 32
rs 8.8571
c 0
b 0
f 0
1
<?php
2
3
namespace Drupal\graphql_core\Plugin\GraphQL\Schemas;
4
5
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
6
use Drupal\graphql\Plugin\GraphQL\Schemas\SchemaPluginBase;
7
use Symfony\Component\DependencyInjection\ContainerInterface;
8
use Youshido\GraphQL\Config\Schema\SchemaConfig;
9
use Youshido\GraphQL\Schema\InternalSchemaMutationObject;
10
use Youshido\GraphQL\Schema\InternalSchemaQueryObject;
11
12
/**
13
 * Default generated schema.
14
 *
15
 * @GraphQLSchema(
16
 *   id = "default",
17
 *   name = "Default",
18
 *   path = "/graphql"
19
 * )
20
 */
21
class DefaultSchema extends SchemaPluginBase implements ContainerFactoryPluginInterface {
22
23
  /**
24
   * {@inheritdoc}
25
   */
26
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
27
    /** @var \Drupal\graphql\Plugin\GraphQL\SchemaBuilderFactory $schemaBuilderFactory */
28
    $schemaBuilderFactory = $container->get('graphql.schema_builder_factory');
29
    // TODO: Inject schema reducer configuration into the schema builder.
30
    $schemaBuilder = $schemaBuilderFactory->getSchemaBuilder();
31
32
    $mutation = new InternalSchemaMutationObject(['name' => 'RootMutation']);
33
    $mutation->addFields($schemaBuilder->getMutations());
34
35
    $query = new InternalSchemaQueryObject(['name' => 'RootQuery']);
36
    $query->addFields($schemaBuilder->getRootFields());
37
38
    $types = $schemaBuilder->find(function() {
39
      return TRUE;
40
    }, [
41
      GRAPHQL_UNION_TYPE_PLUGIN,
42
      GRAPHQL_TYPE_PLUGIN,
43
      GRAPHQL_INPUT_TYPE_PLUGIN,
44
    ]);
45
46
    $schema = [
47
      'query' => $query,
48
      'mutation' => $mutation,
49
      'types' => $types,
50
    ];
51
52
    return new static(
53
      $configuration + ['schema' => $schema],
54
      $plugin_id,
55
      $plugin_definition
56
    );
57
  }
58
59
  /**
60
   * {@inheritdoc}
61
   */
62
  protected function constructSchema($configuration, $pluginId, $pluginDefinition) {
63
    $this->config = new SchemaConfig($configuration['schema']);
64
  }
65
}
66