1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @file |
5
|
|
|
* The GraphQL module. |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
use Drupal\graphql\Utility\StringHelper; |
9
|
|
|
use Drupal\Core\Url; |
10
|
|
|
|
11
|
|
|
define('GRAPHQL_SCALAR_PLUGIN', 'scalar'); |
12
|
|
|
define('GRAPHQL_FIELD_PLUGIN', 'field'); |
13
|
|
|
define('GRAPHQL_MUTATION_PLUGIN', 'mutation'); |
14
|
|
|
define('GRAPHQL_INTERFACE_PLUGIN', 'interface'); |
15
|
|
|
define('GRAPHQL_UNION_TYPE_PLUGIN', 'union'); |
16
|
|
|
define('GRAPHQL_INPUT_TYPE_PLUGIN', 'input'); |
17
|
|
|
define('GRAPHQL_TYPE_PLUGIN', 'type'); |
18
|
|
|
define('GRAPHQL_ENUM_PLUGIN', 'enum'); |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Implements hook_help(). |
22
|
|
|
*/ |
23
|
|
|
function graphql_help($routeName) { |
24
|
|
|
if ($routeName !== 'help.page.graphql') { |
25
|
|
|
return; |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
$title = t('About'); |
29
|
|
|
$description = t(' |
30
|
|
|
<p>This module generates and exposes a |
31
|
|
|
<a href="http://graphql.org/" target="_blank">GraphQL</a> schema for |
32
|
|
|
<a href="https://www.drupal.org/8" target="_blank">Drupal 8</a> entities, |
33
|
|
|
and allows you to expose your own custom schema in a consistent way and with |
34
|
|
|
minimal effort.</p>'); |
35
|
|
|
|
36
|
|
|
$help = <<<EOT |
37
|
|
|
<h3>$title</h3> |
38
|
|
|
$description |
39
|
|
|
EOT; |
40
|
|
|
|
41
|
|
|
return $help; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Implements hook_theme(). |
46
|
|
|
*/ |
47
|
|
|
function graphql_theme() { |
48
|
|
|
return [ |
49
|
|
|
'page__graphql_explorer' => [ |
50
|
|
|
'render element' => 'elements', |
51
|
|
|
'base hook' => 'block', |
52
|
|
|
], |
53
|
|
|
'page__graphql_voyager' => [ |
54
|
|
|
'render element' => 'elements', |
55
|
|
|
'base hook' => 'block', |
56
|
|
|
], |
57
|
|
|
]; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Implements hook_graphql_interfaces_alter(). |
62
|
|
|
* |
63
|
|
|
* Flatten the interface inheritance tree. |
64
|
|
|
*/ |
65
|
|
|
function graphql_graphql_interfaces_alter(&$definitions) { |
66
|
|
|
$interfaces = array_map(function($definition) use ($definitions) { |
67
|
|
|
return graphql_list_interfaces($definitions, $definition); |
68
|
|
|
}, $definitions); |
69
|
|
|
|
70
|
|
|
foreach ($interfaces as $index => $list) { |
71
|
|
|
$definition['interfaces'] = $list; |
|
|
|
|
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Implements hook_graphql_types_alter(). |
77
|
|
|
* |
78
|
|
|
* Flatten the interface inheritance tree. |
79
|
|
|
*/ |
80
|
|
|
function graphql_graphql_types_alter(&$definitions) { |
81
|
|
|
$interfaceDefinitions = \Drupal::service('plugin.manager.graphql.interface')->getDefinitions(); |
82
|
|
|
|
83
|
|
|
$interfaces = array_map(function($definition) use ($interfaceDefinitions) { |
84
|
|
|
return graphql_list_interfaces($interfaceDefinitions, $definition); |
85
|
|
|
}, $definitions); |
86
|
|
|
|
87
|
|
|
foreach ($interfaces as $index => $list) { |
88
|
|
|
$definitions[$index]['interfaces'] = $list; |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Get a flattened list of a plugins interface inheritance tree. |
94
|
|
|
* |
95
|
|
|
* @param array $definitions |
96
|
|
|
* The list of interface definitions. |
97
|
|
|
* @param mixed $definition |
98
|
|
|
* A plugin definition. |
99
|
|
|
* |
100
|
|
|
* @return string[] |
101
|
|
|
* A list of interface names. |
102
|
|
|
*/ |
103
|
|
|
function graphql_list_interfaces(array &$definitions, $definition) { |
104
|
|
|
$parents = array_filter($definitions, function($parent) use ($definition) { |
105
|
|
|
return in_array($parent['name'], $definition['interfaces']); |
106
|
|
|
}); |
107
|
|
|
|
108
|
|
|
$interfaces = array_reduce(array_map(function($parent) use ($definitions) { |
109
|
|
|
return graphql_list_interfaces($definitions, $parent); |
110
|
|
|
}, $parents), 'array_merge', $definition['interfaces']); |
111
|
|
|
|
112
|
|
|
return $interfaces; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Alter the subrequest payload and add contextual data. |
117
|
|
|
*/ |
118
|
|
|
function graphql_graphql_subrequest_alter(&$data, $requirements) { |
119
|
|
|
/** @var \Drupal\Core\Plugin\Context\ContextRepositoryInterface $repository */ |
120
|
|
|
$repository = \Drupal::service('graphql.context_repository'); |
121
|
|
|
$requiredContexts = array_intersect(array_keys($repository->getAvailableContexts()), $requirements); |
122
|
|
|
$runtimeContexts = $repository->getRuntimeContexts($requiredContexts); |
123
|
|
|
foreach ($requiredContexts as $contextId) { |
124
|
|
|
if (array_key_exists($contextId, $runtimeContexts)) { |
125
|
|
|
$data[$contextId] = $runtimeContexts[$contextId]->getContextValue(); |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Implements hook_graphql_schema_operations(). |
132
|
|
|
*/ |
133
|
|
|
function graphql_graphql_schema_operations($pluginId, array $pluginDefinition) { |
|
|
|
|
134
|
|
|
$operations = []; |
135
|
|
|
|
136
|
|
View Code Duplication |
if (\Drupal::currentUser()->hasPermission('use graphql explorer')) { |
|
|
|
|
137
|
|
|
$operations['explorer'] = [ |
138
|
|
|
'title' => 'Explorer', |
139
|
|
|
'weight' => 10, |
140
|
|
|
'url' => Url::fromRoute("graphql.explorer.$pluginId"), |
141
|
|
|
]; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
View Code Duplication |
if (\Drupal::currentUser()->hasPermission('use graphql voyager')) { |
|
|
|
|
145
|
|
|
$operations['voyager'] = [ |
146
|
|
|
'title' => 'Voyager', |
147
|
|
|
'weight' => 10, |
148
|
|
|
'url' => Url::fromRoute("graphql.voyager.$pluginId"), |
149
|
|
|
]; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
return $operations; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* Turn a list of machine names into a camel-cased string. |
157
|
|
|
* |
158
|
|
|
* @deprecated in graphql 3.x and will be removed before 3.0-alpha6. |
159
|
|
|
* Use \Drupal\graphql\Utility\StringHelper::camelCase() instead. |
160
|
|
|
*/ |
161
|
|
|
function graphql_camelcase($components) { |
162
|
|
|
return StringHelper::camelCase($components); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* Turn a list of machine names into a property-cased string. |
167
|
|
|
* |
168
|
|
|
* @deprecated in graphql 3.x and will be removed before 3.0-alpha6. |
169
|
|
|
* Use \Drupal\graphql\Utility\StringHelper::propCase() instead. |
170
|
|
|
*/ |
171
|
|
|
function graphql_propcase($components) { |
172
|
|
|
return StringHelper::propCase($components); |
173
|
|
|
} |
174
|
|
|
|
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArray
is initialized the first time when the foreach loop is entered. You can also see that the value of thebar
key is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.