|
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
|
|
|
|