forked from drupal-graphql/graphql
-
Notifications
You must be signed in to change notification settings - Fork 0
/
graphql.module
191 lines (164 loc) · 4.95 KB
/
graphql.module
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
<?php
use Drupal\Core\Cache\Cache;
use Drupal\Core\Url;
use Drupal\graphql\Utility\StringHelper;
define('GRAPHQL_SCALAR_PLUGIN', 'scalar');
define('GRAPHQL_FIELD_PLUGIN', 'field');
define('GRAPHQL_MUTATION_PLUGIN', 'mutation');
define('GRAPHQL_SUBSCRIPTION_PLUGIN', 'subscription');
define('GRAPHQL_INTERFACE_PLUGIN', 'interface');
define('GRAPHQL_UNION_TYPE_PLUGIN', 'union');
define('GRAPHQL_INPUT_TYPE_PLUGIN', 'input');
define('GRAPHQL_TYPE_PLUGIN', 'type');
define('GRAPHQL_ENUM_PLUGIN', 'enum');
/**
* Implements hook_help().
*/
function graphql_help($routeName) {
if ($routeName !== 'help.page.graphql') {
return;
}
$title = t('About');
$description = t('
<p>This module generates and exposes a
<a href="http://graphql.org/" target="_blank">GraphQL</a> schema for
<a href="https://www.drupal.org/8" target="_blank">Drupal 8</a> entities,
and allows you to expose your own custom schema in a consistent way and with
minimal effort.</p>');
$help = <<<EOT
<h3>$title</h3>
$description
EOT;
return $help;
}
/**
* Implements hook_theme().
*/
function graphql_theme() {
return [
'page__graphql_explorer' => [
'render element' => 'elements',
'base hook' => 'block',
],
'page__graphql_voyager' => [
'render element' => 'elements',
'base hook' => 'block',
],
];
}
/**
* Implements hook_graphql_interfaces_alter().
*
* Flatten the interface inheritance tree.
*/
function graphql_graphql_interfaces_alter(&$definitions) {
$interfaces = array_map(function($definition) use ($definitions) {
return graphql_list_interfaces($definitions, $definition);
}, $definitions);
foreach ($interfaces as $index => $list) {
$definitions[$index]['interfaces'] = $list;
}
}
/**
* Implements hook_graphql_types_alter().
*
* Flatten the interface inheritance tree.
*/
function graphql_graphql_types_alter(&$definitions) {
$interfaceDefinitions = \Drupal::service('plugin.manager.graphql.interface')->getDefinitions();
$interfaces = array_map(function($definition) use ($interfaceDefinitions) {
return graphql_list_interfaces($interfaceDefinitions, $definition);
}, $definitions);
foreach ($interfaces as $index => $list) {
$definitions[$index]['interfaces'] = $list;
}
}
/**
* Helper function to decorate legacy definitions.
*
* @param array $definitions
* A plugin definitions array.
*/
function _graphql_decorate_deprecated_type(array &$definitions) {
foreach ($definitions as &$definition) {
if (!empty($definition['type'])) {
if (!empty($definition['multi'])) {
$definition['type'] = StringHelper::listType($definition['type']);
}
if (isset($definition['nullable']) && empty($definition['nullable'])) {
$definition['type'] = StringHelper::nonNullType($definition['type']);
}
}
if (!empty($definition['fields'])) {
_graphql_decorate_deprecated_type($definition['fields']);
}
if (!empty($definition['arguments'])) {
_graphql_decorate_deprecated_type($definition['arguments']);
}
}
}
/**
* Implements hook_graphql_fields_alter().
*/
function graphql_graphql_fields_alter(&$definitions) {
_graphql_decorate_deprecated_type($definitions);
}
/**
* Implements hook_graphql_mutations_alter().
*/
function graphql_graphql_mutations_alter(&$definitions) {
_graphql_decorate_deprecated_type($definitions);
}
/**
* Implements hook_graphql_mutations_alter().
*/
function graphql_graphql_subscriptions_alter(&$definitions) {
_graphql_decorate_deprecated_type($definitions);
}
/**
* Implements hook_graphql_input_types_alter().
*/
function graphql_graphql_input_types_alter(&$definitions) {
_graphql_decorate_deprecated_type($definitions);
}
/**
* Get a flattened list of a plugins interface inheritance tree.
*
* @param array $definitions
* The list of interface definitions.
* @param mixed $definition
* A plugin definition.
*
* @return string[]
* A list of interface names.
*/
function graphql_list_interfaces(array &$definitions, $definition) {
$parents = array_filter($definitions, function($parent) use ($definition) {
return in_array($parent['name'], $definition['interfaces']);
});
$interfaces = array_reduce(array_map(function($parent) use ($definitions) {
return graphql_list_interfaces($definitions, $parent);
}, $parents), 'array_merge', $definition['interfaces']);
return $interfaces;
}
/**
* Implements hook_graphql_schema_operations().
*/
function graphql_graphql_schema_operations($pluginId, array $pluginDefinition) {
$operations = [];
if (\Drupal::currentUser()->hasPermission('use graphql explorer')) {
$operations['explorer'] = [
'title' => 'Explorer',
'weight' => 10,
'url' => Url::fromRoute("graphql.explorer.$pluginId"),
];
}
if (\Drupal::currentUser()->hasPermission('use graphql voyager')) {
$operations['voyager'] = [
'title' => 'Voyager',
'weight' => 10,
'url' => Url::fromRoute("graphql.voyager.$pluginId"),
];
}
return $operations;
}