View source
<?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');
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>{<span class="php-variable">$title</span>}</h3>
{<span class="php-variable">$description</span>}
EOT;
return $help;
}
function graphql_theme() {
return [
'page__graphql_explorer' => [
'render element' => 'elements',
'base hook' => 'block',
],
'page__graphql_voyager' => [
'render element' => 'elements',
'base hook' => 'block',
],
];
}
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;
}
}
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;
}
}
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']);
}
}
}
function graphql_graphql_fields_alter(&$definitions) {
_graphql_decorate_deprecated_type($definitions);
}
function graphql_graphql_mutations_alter(&$definitions) {
_graphql_decorate_deprecated_type($definitions);
}
function graphql_graphql_subscriptions_alter(&$definitions) {
_graphql_decorate_deprecated_type($definitions);
}
function graphql_graphql_input_types_alter(&$definitions) {
_graphql_decorate_deprecated_type($definitions);
}
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;
}
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;
}