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\GraphQL\Schema\Schema; |
9
|
|
|
use Drupal\graphql\Plugin\GraphQL\PluggableSchemaBuilder; |
10
|
|
|
use Drupal\graphql\Plugin\GraphQL\TypeSystemPluginInterface; |
11
|
|
|
use Drupal\graphql\Plugin\GraphQL\TypeSystemPluginManagerAggregator; |
12
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface; |
13
|
|
|
use Youshido\GraphQL\Schema\InternalSchemaMutationObject; |
14
|
|
|
use Youshido\GraphQL\Schema\InternalSchemaQueryObject; |
15
|
|
|
|
16
|
|
|
abstract class SchemaPluginBase extends PluginBase implements ContainerFactoryPluginInterface { |
17
|
|
|
|
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
|
|
|
$mutation = new InternalSchemaMutationObject(['name' => 'RootMutation']); |
61
|
|
|
$mutation->addFields($this->extractDefinitions(($this->getMutations()))); |
62
|
|
|
|
63
|
|
|
$query = new InternalSchemaQueryObject(['name' => 'RootQuery']); |
64
|
|
|
$query->addFields($this->extractDefinitions($this->getRootFields())); |
65
|
|
|
|
66
|
|
|
$types = $this->extractDefinitions($this->getTypes()); |
67
|
|
|
|
68
|
|
|
return new Schema([ |
69
|
|
|
'query' => $query, |
70
|
|
|
'mutation' => $mutation, |
71
|
|
|
'types' => $types, |
72
|
|
|
]); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Extract type or field definitions from plugins. |
77
|
|
|
* |
78
|
|
|
* @param array $plugins |
79
|
|
|
* The list of plugins to extract the type or field definitions from. |
80
|
|
|
* |
81
|
|
|
* @return array |
82
|
|
|
* The list of extracted type or field definitions. |
83
|
|
|
*/ |
84
|
|
|
protected function extractDefinitions(array $plugins) { |
85
|
|
|
return array_map(function (TypeSystemPluginInterface $plugin) { |
86
|
|
|
return $plugin->getDefinition($this->schemaBuilder); |
87
|
|
|
}, $plugins); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Retrieve all mutations. |
92
|
|
|
* |
93
|
|
|
* @return \Drupal\graphql\Plugin\GraphQL\TypeSystemPluginInterface[] |
94
|
|
|
* The list of mutation plugins. |
95
|
|
|
*/ |
96
|
|
|
protected function getMutations() { |
97
|
|
|
return $this->schemaBuilder->find(function() { |
98
|
|
|
return TRUE; |
99
|
|
|
}, [GRAPHQL_MUTATION_PLUGIN]); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Retrieve all fields that are not associated with a specific type. |
104
|
|
|
* |
105
|
|
|
* @return \Drupal\graphql\Plugin\GraphQL\TypeSystemPluginInterface[] |
106
|
|
|
* The list root field plugins. |
107
|
|
|
*/ |
108
|
|
|
protected function getRootFields() { |
109
|
|
|
// Retrieve the list of fields that are not attached to any type or are |
110
|
|
|
// explicitly attached to the artificial "Root" type. |
111
|
|
|
return $this->schemaBuilder->find(function($definition) { |
112
|
|
|
return empty($definition['parents']) || in_array('Root', $definition['parents']); |
113
|
|
|
}, [GRAPHQL_FIELD_PLUGIN]); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Retrieve all types to be registered explicitly. |
118
|
|
|
* |
119
|
|
|
* @return \Drupal\graphql\Plugin\GraphQL\TypeSystemPluginInterface[] |
120
|
|
|
* The list of types to be registered explicitly. |
121
|
|
|
*/ |
122
|
|
|
protected function getTypes() { |
123
|
|
|
return $this->schemaBuilder->find(function() { |
124
|
|
|
return TRUE; |
125
|
|
|
}, [ |
126
|
|
|
GRAPHQL_UNION_TYPE_PLUGIN, |
127
|
|
|
GRAPHQL_TYPE_PLUGIN, |
128
|
|
|
GRAPHQL_INPUT_TYPE_PLUGIN, |
129
|
|
|
]); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
} |
133
|
|
|
|