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