Completed
Pull Request — 8.x-3.x (#518)
by Philipp
02:16
created

graphql.module (4 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
/**
4
 * @file
5
 * The GraphQL module.
6
 */
7
8
use Drupal\Core\Url;
9
use Drupal\graphql\Utility\StringHelper;
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;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$definition was never initialized. Although not strictly required by PHP, it is generally a good practice to add $definition = array(); before regardless.

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:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

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 the bar 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.

Loading history...
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
 * Helper function to decorate legacy definitions.
94
 *
95
 * @param array $definitions
96
 *   A plugin definitions array.
97
 */
98
function _graphql_decorate_deprecated_type(array &$definitions) {
99
  foreach ($definitions as &$definition) {
100
    if (!empty($definition['type'])) {
101
      if (!empty($definition['multi'])) {
102
        $definition['type'] = StringHelper::listType($definition['type']);
103
      }
104
105
      if (isset($definition['nullable']) && empty($definition['nullable'])) {
106
        $definition['type'] = StringHelper::nonNullType($definition['type']);
107
      }
108
    }
109
110
    if (!empty($definition['fields'])) {
111
      _graphql_decorate_deprecated_type($definition['fields']);
112
    }
113
114
    if (!empty($definition['arguments'])) {
115
      _graphql_decorate_deprecated_type($definition['arguments']);
116
    }
117
  }
118
}
119
120
/**
121
 * Implements hook_graphql_fields_alter().
122
 */
123
function graphql_graphql_fields_alter(&$definitions) {
124
  _graphql_decorate_deprecated_type($definitions);
125
}
126
127
/**
128
 * Implements hook_graphql_mutations_alter().
129
 */
130
function graphql_graphql_mutations_alter(&$definitions) {
131
  _graphql_decorate_deprecated_type($definitions);
132
}
133
134
/**
135
 * Implements hook_graphql_input_types_alter().
136
 */
137
function graphql_graphql_input_types_alter(&$definitions) {
138
  _graphql_decorate_deprecated_type($definitions);
139
}
140
141
/**
142
 * Get a flattened list of a plugins interface inheritance tree.
143
 *
144
 * @param array $definitions
145
 *   The list of interface definitions.
146
 * @param mixed $definition
147
 *   A plugin definition.
148
 *
149
 * @return string[]
150
 *   A list of interface names.
151
 */
152
function graphql_list_interfaces(array &$definitions, $definition) {
153
  $parents = array_filter($definitions, function($parent) use ($definition) {
154
    return in_array($parent['name'], $definition['interfaces']);
155
  });
156
157
  $interfaces = array_reduce(array_map(function($parent) use ($definitions) {
158
    return graphql_list_interfaces($definitions, $parent);
159
  }, $parents), 'array_merge', $definition['interfaces']);
160
161
  return $interfaces;
162
}
163
164
/**
165
 * Implements hook_graphql_schema_operations().
166
 */
167
function graphql_graphql_schema_operations($pluginId, array $pluginDefinition) {
0 ignored issues
show
The parameter $pluginDefinition is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
168
  $operations = [];
169
170 View Code Duplication
  if (\Drupal::currentUser()->hasPermission('use graphql explorer')) {
0 ignored issues
show
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
171
    $operations['explorer'] = [
172
      'title' => 'Explorer',
173
      'weight' => 10,
174
      'url' => Url::fromRoute("graphql.explorer.$pluginId"),
175
    ];
176
  }
177
178 View Code Duplication
  if (\Drupal::currentUser()->hasPermission('use graphql voyager')) {
0 ignored issues
show
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
179
    $operations['voyager'] = [
180
      'title' => 'Voyager',
181
      'weight' => 10,
182
      'url' => Url::fromRoute("graphql.voyager.$pluginId"),
183
    ];
184
  }
185
186
  return $operations;
187
}
188