-
Notifications
You must be signed in to change notification settings - Fork 201
Expand file tree
/
Copy pathComposableSchema.php
More file actions
148 lines (124 loc) · 3.78 KB
/
ComposableSchema.php
File metadata and controls
148 lines (124 loc) · 3.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
<?php
namespace Drupal\graphql\Plugin\GraphQL\Schema;
use Drupal\Component\Plugin\ConfigurableInterface;
use Drupal\Component\Utility\NestedArray;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Plugin\PluginFormInterface;
use Drupal\graphql\GraphQL\DirectiveProviderExtensionInterface;
use Drupal\graphql\GraphQL\ParentAwareSchemaExtensionInterface;
use Drupal\graphql\GraphQL\ResolverRegistry;
use Drupal\Core\StringTranslation\StringTranslationTrait;
/**
* @Schema(
* id = "composable",
* name = "Composable schema"
* )
*/
class ComposableSchema extends SdlSchemaPluginBase implements ConfigurableInterface, PluginFormInterface {
use StringTranslationTrait;
/**
* {@inheritdoc}
*/
public function getResolverRegistry() {
return new ResolverRegistry();
}
/**
* {@inheritdoc}
*/
protected function getExtensions() {
$extensions = array_map(function ($id) {
return $this->extensionManager->createInstance($id);
}, array_filter($this->getConfiguration()['extensions']));
$schemaDocument = $this->getSchemaDocument($extensions);
// Iterate through all extensions and pass them the current schema, so they
// can act on it.
foreach ($extensions as $extension) {
if ($extension instanceof ParentAwareSchemaExtensionInterface) {
$extension->setParentSchemaDocument($schemaDocument);
}
}
return $extensions;
}
/**
* {@inheritdoc}
*/
protected function getSchemaDefinition() {
$extensions = parent::getExtensions();
// Get all extensions and prepend any defined directives to the schema.
$schema = [];
foreach ($extensions as $extension) {
if ($extension instanceof DirectiveProviderExtensionInterface) {
$schema[] = $extension->getDirectiveDefinitions();
}
}
// Attempt to load a schema file and return it instead of the hardcoded
// empty schema.
$id = $this->getPluginId();
$definition = $this->getPluginDefinition();
$module = $this->moduleHandler->getModule($definition['provider']);
$path = 'graphql/' . $id . '.graphqls';
$file = $module->getPath() . '/' . $path;
if (!file_exists($file)) {
$schema[] = <<<GQL
type Schema {
query: Query
}
type Query
GQL;
}
else {
$schema[] = file_get_contents($file);
}
return implode("\n", $schema);
}
/**
* {@inheritdoc}
*/
public function getConfiguration() {
return $this->configuration;
}
/**
* {@inheritdoc}
*/
public function setConfiguration(array $configuration): void {
$this->configuration = NestedArray::mergeDeep($this->defaultConfiguration(), $configuration);
}
/**
* {@inheritdoc}
*/
public function defaultConfiguration() {
return [];
}
/**
* {@inheritdoc}
*/
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$extensions = $this->extensionManager->getDefinitions();
$form['extensions'] = [
'#type' => 'checkboxes',
'#required' => FALSE,
'#title' => $this->t('Enabled extensions'),
'#options' => [],
'#default_value' => $this->configuration['extensions'] ?? [],
];
foreach ($extensions as $key => $extension) {
$form['extensions']['#options'][$key] = $extension['name'] ?? $extension['id'];
if (!empty($extension['description'])) {
$form['extensions'][$key]['#description'] = $extension['description'];
}
}
return $form;
}
/**
* {@inheritdoc}
*/
public function validateConfigurationForm(array &$form, FormStateInterface $formState): void {
// @todo Validate dependencies between extensions.
}
/**
* {@inheritdoc}
*/
public function submitConfigurationForm(array &$form, FormStateInterface $formState): void {
// Nothing to do here.
}
}