You are here

graphql.module in GraphQL 8.3

Same filename and directory in other branches
  1. 8.4 graphql.module
  2. 8 graphql.module
  3. 8.2 graphql.module

File

graphql.module
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');

/**
 * Implements hook_help().
 */
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;
}

/**
 * Implements hook_theme().
 */
function graphql_theme() {
  return [
    'page__graphql_explorer' => [
      'render element' => 'elements',
      'base hook' => 'block',
    ],
    'page__graphql_voyager' => [
      'render element' => 'elements',
      'base hook' => 'block',
    ],
  ];
}

/**
 * Implements hook_graphql_interfaces_alter().
 *
 * Flatten the interface inheritance tree.
 */
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;
  }
}

/**
 * Implements hook_graphql_types_alter().
 *
 * Flatten the interface inheritance tree.
 */
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;
  }
}

/**
 * Helper function to decorate legacy definitions.
 *
 * @param array $definitions
 *   A plugin definitions array.
 */
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']);
    }
  }
}

/**
 * Implements hook_graphql_fields_alter().
 */
function graphql_graphql_fields_alter(&$definitions) {
  _graphql_decorate_deprecated_type($definitions);
}

/**
 * Implements hook_graphql_mutations_alter().
 */
function graphql_graphql_mutations_alter(&$definitions) {
  _graphql_decorate_deprecated_type($definitions);
}

/**
 * Implements hook_graphql_mutations_alter().
 */
function graphql_graphql_subscriptions_alter(&$definitions) {
  _graphql_decorate_deprecated_type($definitions);
}

/**
 * Implements hook_graphql_input_types_alter().
 */
function graphql_graphql_input_types_alter(&$definitions) {
  _graphql_decorate_deprecated_type($definitions);
}

/**
 * Get a flattened list of a plugins interface inheritance tree.
 *
 * @param array $definitions
 *   The list of interface definitions.
 * @param mixed $definition
 *   A plugin definition.
 *
 * @return string[]
 *   A list of interface names.
 */
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;
}

/**
 * Implements hook_graphql_schema_operations().
 */
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;
}

Functions

Namesort descending Description
graphql_graphql_fields_alter Implements hook_graphql_fields_alter().
graphql_graphql_input_types_alter Implements hook_graphql_input_types_alter().
graphql_graphql_interfaces_alter Implements hook_graphql_interfaces_alter().
graphql_graphql_mutations_alter Implements hook_graphql_mutations_alter().
graphql_graphql_schema_operations Implements hook_graphql_schema_operations().
graphql_graphql_subscriptions_alter Implements hook_graphql_mutations_alter().
graphql_graphql_types_alter Implements hook_graphql_types_alter().
graphql_help Implements hook_help().
graphql_list_interfaces Get a flattened list of a plugins interface inheritance tree.
graphql_theme Implements hook_theme().
_graphql_decorate_deprecated_type Helper function to decorate legacy definitions.

Constants