1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @file |
5
|
|
|
* The GraphQL module. |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
use Drupal\Core\Url; |
9
|
|
|
|
10
|
|
|
define('GRAPHQL_SCALAR_PLUGIN', 'scalar'); |
11
|
|
|
define('GRAPHQL_FIELD_PLUGIN', 'field'); |
12
|
|
|
define('GRAPHQL_MUTATION_PLUGIN', 'mutation'); |
13
|
|
|
define('GRAPHQL_INTERFACE_PLUGIN', 'interface'); |
14
|
|
|
define('GRAPHQL_UNION_TYPE_PLUGIN', 'union'); |
15
|
|
|
define('GRAPHQL_INPUT_TYPE_PLUGIN', 'input'); |
16
|
|
|
define('GRAPHQL_TYPE_PLUGIN', 'type'); |
17
|
|
|
define('GRAPHQL_ENUM_PLUGIN', 'enum'); |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Implements hook_help(). |
21
|
|
|
*/ |
22
|
|
|
function graphql_help($routeName) { |
23
|
|
|
if ($routeName !== 'help.page.graphql') { |
24
|
|
|
return; |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
$title = t('About'); |
28
|
|
|
$description = t(' |
29
|
|
|
<p>This module generates and exposes a |
30
|
|
|
<a href="http://graphql.org/" target="_blank">GraphQL</a> schema for |
31
|
|
|
<a href="https://www.drupal.org/8" target="_blank">Drupal 8</a> entities, |
32
|
|
|
and allows you to expose your own custom schema in a consistent way and with |
33
|
|
|
minimal effort.</p>'); |
34
|
|
|
|
35
|
|
|
$help = <<<EOT |
36
|
|
|
<h3>$title</h3> |
37
|
|
|
$description |
38
|
|
|
EOT; |
39
|
|
|
|
40
|
|
|
return $help; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Implements hook_theme(). |
45
|
|
|
*/ |
46
|
|
|
function graphql_theme() { |
47
|
|
|
return [ |
48
|
|
|
'page__graphql_explorer' => [ |
49
|
|
|
'render element' => 'elements', |
50
|
|
|
'base hook' => 'block', |
51
|
|
|
], |
52
|
|
|
'page__graphql_voyager' => [ |
53
|
|
|
'render element' => 'elements', |
54
|
|
|
'base hook' => 'block', |
55
|
|
|
], |
56
|
|
|
]; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Implements hook_graphql_interfaces_alter(). |
61
|
|
|
* |
62
|
|
|
* Flatten the interface inheritance tree. |
63
|
|
|
*/ |
64
|
|
|
function graphql_graphql_interfaces_alter(&$definitions) { |
65
|
|
|
$interfaces = array_map(function($definition) use ($definitions) { |
66
|
|
|
return graphql_list_interfaces($definitions, $definition); |
67
|
|
|
}, $definitions); |
68
|
|
|
|
69
|
|
|
foreach ($interfaces as $index => $list) { |
70
|
|
|
$definition['interfaces'] = $list; |
|
|
|
|
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Implements hook_graphql_types_alter(). |
76
|
|
|
* |
77
|
|
|
* Flatten the interface inheritance tree. |
78
|
|
|
*/ |
79
|
|
|
function graphql_graphql_types_alter(&$definitions) { |
80
|
|
|
$interfaceDefinitions = \Drupal::service('plugin.manager.graphql.interface')->getDefinitions(); |
81
|
|
|
|
82
|
|
|
$interfaces = array_map(function($definition) use ($interfaceDefinitions) { |
83
|
|
|
return graphql_list_interfaces($interfaceDefinitions, $definition); |
84
|
|
|
}, $definitions); |
85
|
|
|
|
86
|
|
|
foreach ($interfaces as $index => $list) { |
87
|
|
|
$definitions[$index]['interfaces'] = $list; |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Get a flattened list of a plugins interface inheritance tree. |
93
|
|
|
* |
94
|
|
|
* @param array $definitions |
95
|
|
|
* The list of interface definitions. |
96
|
|
|
* @param mixed $definition |
97
|
|
|
* A plugin definition. |
98
|
|
|
* |
99
|
|
|
* @return string[] |
100
|
|
|
* A list of interface names. |
101
|
|
|
*/ |
102
|
|
|
function graphql_list_interfaces(array &$definitions, $definition) { |
103
|
|
|
$parents = array_filter($definitions, function($parent) use ($definition) { |
104
|
|
|
return in_array($parent['name'], $definition['interfaces']); |
105
|
|
|
}); |
106
|
|
|
|
107
|
|
|
$interfaces = array_reduce(array_map(function($parent) use ($definitions) { |
108
|
|
|
return graphql_list_interfaces($definitions, $parent); |
109
|
|
|
}, $parents), 'array_merge', $definition['interfaces']); |
110
|
|
|
|
111
|
|
|
return $interfaces; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Implements hook_graphql_schema_operations(). |
116
|
|
|
*/ |
117
|
|
|
function graphql_graphql_schema_operations($pluginId, array $pluginDefinition) { |
|
|
|
|
118
|
|
|
$operations = []; |
119
|
|
|
|
120
|
|
View Code Duplication |
if (\Drupal::currentUser()->hasPermission('use graphql explorer')) { |
|
|
|
|
121
|
|
|
$operations['explorer'] = [ |
122
|
|
|
'title' => 'Explorer', |
123
|
|
|
'weight' => 10, |
124
|
|
|
'url' => Url::fromRoute("graphql.explorer.$pluginId"), |
125
|
|
|
]; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
View Code Duplication |
if (\Drupal::currentUser()->hasPermission('use graphql voyager')) { |
|
|
|
|
129
|
|
|
$operations['voyager'] = [ |
130
|
|
|
'title' => 'Voyager', |
131
|
|
|
'weight' => 10, |
132
|
|
|
'url' => Url::fromRoute("graphql.voyager.$pluginId"), |
133
|
|
|
]; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
return $operations; |
137
|
|
|
} |
138
|
|
|
|
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.