|
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\GraphQL\PluggableSchemaBuilder; |
|
9
|
|
|
use Drupal\graphql\Plugin\GraphQL\SchemaPluginInterface; |
|
10
|
|
|
use Drupal\graphql\Plugin\GraphQL\TypeSystemPluginInterface; |
|
11
|
|
|
use Drupal\graphql\Plugin\GraphQL\TypeSystemPluginManagerAggregator; |
|
12
|
|
|
use GraphQL\Type\Definition\ObjectType; |
|
13
|
|
|
use GraphQL\Type\Schema; |
|
14
|
|
|
use GraphQL\Type\SchemaConfig; |
|
15
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface; |
|
16
|
|
|
|
|
17
|
|
|
abstract class SchemaPluginBase extends PluginBase implements SchemaPluginInterface, ContainerFactoryPluginInterface { |
|
18
|
|
|
use DependencySerializationTrait; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* The schema builder object. |
|
22
|
|
|
* |
|
23
|
|
|
* @var \Drupal\graphql\Plugin\GraphQL\PluggableSchemaBuilderInterface |
|
24
|
|
|
*/ |
|
25
|
|
|
protected $schemaBuilder; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* {@inheritdoc} |
|
29
|
|
|
*/ |
|
30
|
|
|
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { |
|
31
|
|
|
return new static( |
|
32
|
|
|
$configuration, |
|
33
|
|
|
$plugin_id, |
|
34
|
|
|
$plugin_definition, |
|
35
|
|
|
$container->get('graphql.plugin_manager_aggregator') |
|
36
|
|
|
); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* SchemaPluginBase constructor. |
|
41
|
|
|
* |
|
42
|
|
|
* @param array $configuration |
|
43
|
|
|
* The plugin configuration array. |
|
44
|
|
|
* @param string $pluginId |
|
45
|
|
|
* The plugin id. |
|
46
|
|
|
* @param array $pluginDefinition |
|
47
|
|
|
* The plugin definition array. |
|
48
|
|
|
* @param \Drupal\graphql\Plugin\GraphQL\TypeSystemPluginManagerAggregator $pluginManagers |
|
49
|
|
|
* Type system plugin manager aggregator service. |
|
50
|
|
|
*/ |
|
51
|
|
|
public function __construct($configuration, $pluginId, $pluginDefinition, TypeSystemPluginManagerAggregator $pluginManagers) { |
|
52
|
|
|
parent::__construct($configuration, $pluginId, $pluginDefinition); |
|
53
|
|
|
$this->schemaBuilder = new PluggableSchemaBuilder($pluginManagers); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* {@inheritdoc} |
|
58
|
|
|
*/ |
|
59
|
|
|
public function getSchema() { |
|
60
|
|
|
$config = new SchemaConfig(); |
|
61
|
|
|
|
|
62
|
|
|
$config->setMutation(new ObjectType([ |
|
63
|
|
|
'name' => 'MutationRoot', |
|
64
|
|
|
'fields' => function () { |
|
65
|
|
|
return $this->schemaBuilder->resolveFields($this->getMutations(), GRAPHQL_MUTATION_PLUGIN); |
|
66
|
|
|
}, |
|
67
|
|
|
])); |
|
68
|
|
|
|
|
69
|
|
|
$config->setQuery(new ObjectType([ |
|
70
|
|
|
'name' => 'QueryRoot', |
|
71
|
|
|
'fields' => function () { |
|
72
|
|
|
return $this->schemaBuilder->resolveFields($this->getRootFields(), GRAPHQL_FIELD_PLUGIN); |
|
73
|
|
|
}, |
|
74
|
|
|
])); |
|
75
|
|
|
|
|
76
|
|
|
$config->setTypes(function () { |
|
77
|
|
|
return $this->getTypes(); |
|
78
|
|
|
}); |
|
79
|
|
|
|
|
80
|
|
|
$config->setTypeLoader(function ($name) { |
|
81
|
|
|
return $this->schemaBuilder->getTypeByName($name); |
|
82
|
|
|
}); |
|
83
|
|
|
|
|
84
|
|
|
return new Schema($config); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* Extract type or field definitions from plugins. |
|
89
|
|
|
* |
|
90
|
|
|
* @param array $plugins |
|
91
|
|
|
* The list of plugins to extract the type or field definitions from. |
|
92
|
|
|
* |
|
93
|
|
|
* @return array |
|
94
|
|
|
* The list of extracted type or field definitions. |
|
95
|
|
|
*/ |
|
96
|
|
|
protected function extractDefinitions(array $plugins) { |
|
97
|
|
|
return array_filter(array_map(function (TypeSystemPluginInterface $plugin) { |
|
98
|
|
|
return $plugin->getDefinition($this->schemaBuilder); |
|
99
|
|
|
}, $plugins)); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* Retrieve all mutations. |
|
104
|
|
|
* |
|
105
|
|
|
* @return \Drupal\graphql\Plugin\GraphQL\TypeSystemPluginInterface[] |
|
106
|
|
|
* The list of mutation plugins. |
|
107
|
|
|
*/ |
|
108
|
|
|
protected function getMutations() { |
|
109
|
|
|
return $this->schemaBuilder->getMutationMap(); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* Retrieve all fields that are not associated with a specific type. |
|
114
|
|
|
* |
|
115
|
|
|
* @return \Drupal\graphql\Plugin\GraphQL\TypeSystemPluginInterface[] |
|
116
|
|
|
* The list root field plugins. |
|
117
|
|
|
*/ |
|
118
|
|
|
protected function getRootFields() { |
|
119
|
|
|
return $this->getFields('Root'); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* Retrieve all fields that are not associated with a specific type. |
|
124
|
|
|
* |
|
125
|
|
|
* @return \Drupal\graphql\Plugin\GraphQL\TypeSystemPluginInterface[] |
|
126
|
|
|
* The list root field plugins. |
|
127
|
|
|
*/ |
|
128
|
|
|
protected function getFields($type) { |
|
129
|
|
|
$map = $this->schemaBuilder->getFieldMap(); |
|
130
|
|
|
return isset($map[$type]) ? $map[$type] : []; |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* Retrieve all types to be registered explicitly. |
|
135
|
|
|
* |
|
136
|
|
|
* @return \Drupal\graphql\Plugin\GraphQL\TypeSystemPluginInterface[] |
|
137
|
|
|
* The list of types to be registered explicitly. |
|
138
|
|
|
*/ |
|
139
|
|
|
protected function getTypes() { |
|
140
|
|
|
return array_filter(array_map(function ($type) { |
|
141
|
|
|
return $this->schemaBuilder->getTypeByName($type); |
|
142
|
|
|
}, array_keys($this->schemaBuilder->getTypeMap()))); |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
} |
|
146
|
|
|
|