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