1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
use Drupal\Core\Cache\Cache; |
4
|
|
|
use Drupal\Core\Url; |
5
|
|
|
use Drupal\graphql\Utility\StringHelper; |
6
|
|
|
|
7
|
|
|
define('GRAPHQL_SCALAR_PLUGIN', 'scalar'); |
8
|
|
|
define('GRAPHQL_FIELD_PLUGIN', 'field'); |
9
|
|
|
define('GRAPHQL_MUTATION_PLUGIN', 'mutation'); |
10
|
|
|
define('GRAPHQL_SUBSCRIPTION_PLUGIN', 'subscription'); |
11
|
|
|
define('GRAPHQL_INTERFACE_PLUGIN', 'interface'); |
12
|
|
|
define('GRAPHQL_UNION_TYPE_PLUGIN', 'union'); |
13
|
|
|
define('GRAPHQL_INPUT_TYPE_PLUGIN', 'input'); |
14
|
|
|
define('GRAPHQL_TYPE_PLUGIN', 'type'); |
15
|
|
|
define('GRAPHQL_ENUM_PLUGIN', 'enum'); |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Implements hook_help(). |
19
|
|
|
*/ |
20
|
|
|
function graphql_help($routeName) { |
21
|
|
|
if ($routeName !== 'help.page.graphql') { |
22
|
|
|
return; |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
$title = t('About'); |
26
|
|
|
$description = t(' |
27
|
|
|
<p>This module generates and exposes a |
28
|
|
|
<a href="http://graphql.org/" target="_blank">GraphQL</a> schema for |
29
|
|
|
<a href="https://www.drupal.org/8" target="_blank">Drupal 8</a> entities, |
30
|
|
|
and allows you to expose your own custom schema in a consistent way and with |
31
|
|
|
minimal effort.</p>'); |
32
|
|
|
|
33
|
|
|
$help = <<<EOT |
34
|
|
|
<h3>$title</h3> |
35
|
|
|
$description |
36
|
|
|
EOT; |
37
|
|
|
|
38
|
|
|
return $help; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Implements hook_theme(). |
43
|
|
|
*/ |
44
|
|
|
function graphql_theme() { |
45
|
|
|
return [ |
46
|
|
|
'page__graphql_explorer' => [ |
47
|
|
|
'render element' => 'elements', |
48
|
|
|
'base hook' => 'block', |
49
|
|
|
], |
50
|
|
|
'page__graphql_voyager' => [ |
51
|
|
|
'render element' => 'elements', |
52
|
|
|
'base hook' => 'block', |
53
|
|
|
], |
54
|
|
|
]; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Implements hook_graphql_interfaces_alter(). |
59
|
|
|
* |
60
|
|
|
* Flatten the interface inheritance tree. |
61
|
|
|
*/ |
62
|
|
|
function graphql_graphql_interfaces_alter(&$definitions) { |
63
|
|
|
$interfaces = array_map(function($definition) use ($definitions) { |
64
|
|
|
return graphql_list_interfaces($definitions, $definition); |
65
|
|
|
}, $definitions); |
66
|
|
|
|
67
|
|
|
foreach ($interfaces as $index => $list) { |
68
|
|
|
$definitions[$index]['interfaces'] = $list; |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Implements hook_graphql_types_alter(). |
74
|
|
|
* |
75
|
|
|
* Flatten the interface inheritance tree. |
76
|
|
|
*/ |
77
|
|
|
function graphql_graphql_types_alter(&$definitions) { |
78
|
|
|
$interfaceDefinitions = \Drupal::service('plugin.manager.graphql.interface')->getDefinitions(); |
79
|
|
|
|
80
|
|
|
$interfaces = array_map(function($definition) use ($interfaceDefinitions) { |
81
|
|
|
return graphql_list_interfaces($interfaceDefinitions, $definition); |
82
|
|
|
}, $definitions); |
83
|
|
|
|
84
|
|
|
foreach ($interfaces as $index => $list) { |
85
|
|
|
$definitions[$index]['interfaces'] = $list; |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Helper function to decorate legacy definitions. |
91
|
|
|
* |
92
|
|
|
* @param array $definitions |
93
|
|
|
* A plugin definitions array. |
94
|
|
|
*/ |
95
|
|
|
function _graphql_decorate_deprecated_type(array &$definitions) { |
96
|
|
|
foreach ($definitions as &$definition) { |
97
|
|
|
if (!empty($definition['type'])) { |
98
|
|
|
if (!empty($definition['multi'])) { |
99
|
|
|
$definition['type'] = StringHelper::listType($definition['type']); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
if (isset($definition['nullable']) && empty($definition['nullable'])) { |
103
|
|
|
$definition['type'] = StringHelper::nonNullType($definition['type']); |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
if (!empty($definition['fields'])) { |
108
|
|
|
_graphql_decorate_deprecated_type($definition['fields']); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
if (!empty($definition['arguments'])) { |
112
|
|
|
_graphql_decorate_deprecated_type($definition['arguments']); |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Implements hook_graphql_fields_alter(). |
119
|
|
|
*/ |
120
|
|
|
function graphql_graphql_fields_alter(&$definitions) { |
121
|
|
|
_graphql_decorate_deprecated_type($definitions); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Implements hook_graphql_mutations_alter(). |
126
|
|
|
*/ |
127
|
|
|
function graphql_graphql_mutations_alter(&$definitions) { |
128
|
|
|
_graphql_decorate_deprecated_type($definitions); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Implements hook_graphql_mutations_alter(). |
133
|
|
|
*/ |
134
|
|
|
function graphql_graphql_subscriptions_alter(&$definitions) { |
135
|
|
|
_graphql_decorate_deprecated_type($definitions); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Implements hook_graphql_input_types_alter(). |
140
|
|
|
*/ |
141
|
|
|
function graphql_graphql_input_types_alter(&$definitions) { |
142
|
|
|
_graphql_decorate_deprecated_type($definitions); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* Get a flattened list of a plugins interface inheritance tree. |
147
|
|
|
* |
148
|
|
|
* @param array $definitions |
149
|
|
|
* The list of interface definitions. |
150
|
|
|
* @param mixed $definition |
151
|
|
|
* A plugin definition. |
152
|
|
|
* |
153
|
|
|
* @return string[] |
154
|
|
|
* A list of interface names. |
155
|
|
|
*/ |
156
|
|
|
function graphql_list_interfaces(array &$definitions, $definition) { |
157
|
|
|
$parents = array_filter($definitions, function($parent) use ($definition) { |
158
|
|
|
return in_array($parent['name'], $definition['interfaces']); |
159
|
|
|
}); |
160
|
|
|
|
161
|
|
|
$interfaces = array_reduce(array_map(function($parent) use ($definitions) { |
162
|
|
|
return graphql_list_interfaces($definitions, $parent); |
163
|
|
|
}, $parents), 'array_merge', $definition['interfaces']); |
164
|
|
|
|
165
|
|
|
return $interfaces; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* Implements hook_graphql_schema_operations(). |
170
|
|
|
*/ |
171
|
|
|
function graphql_graphql_schema_operations($pluginId, array $pluginDefinition) { |
|
|
|
|
172
|
|
|
$operations = []; |
173
|
|
|
|
174
|
|
View Code Duplication |
if (\Drupal::currentUser()->hasPermission('use graphql explorer')) { |
|
|
|
|
175
|
|
|
$operations['explorer'] = [ |
176
|
|
|
'title' => 'Explorer', |
177
|
|
|
'weight' => 10, |
178
|
|
|
'url' => Url::fromRoute("graphql.explorer.$pluginId"), |
179
|
|
|
]; |
180
|
|
|
} |
181
|
|
|
|
182
|
|
View Code Duplication |
if (\Drupal::currentUser()->hasPermission('use graphql voyager')) { |
|
|
|
|
183
|
|
|
$operations['voyager'] = [ |
184
|
|
|
'title' => 'Voyager', |
185
|
|
|
'weight' => 10, |
186
|
|
|
'url' => Url::fromRoute("graphql.voyager.$pluginId"), |
187
|
|
|
]; |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
return $operations; |
191
|
|
|
} |
192
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.