You are here

public function SchemaPluginBase::getServer in GraphQL 8.3

Return value

mixed

Overrides SchemaPluginInterface::getServer

File

src/Plugin/GraphQL/Schemas/SchemaPluginBase.php, line 246

Class

SchemaPluginBase

Namespace

Drupal\graphql\Plugin\GraphQL\Schemas

Code

public function getServer() {

  // If the current user has appropriate permissions, allow to bypass
  // the secure fields restriction.
  $globals['bypass field security'] = $this->currentUser
    ->hasPermission('bypass graphql field security');

  // Create the server config.
  $config = ServerConfig::create();

  // Each document (e.g. in a batch query) gets its own resolve context. This
  // allows us to collect the cache metadata and contextual values (e.g.
  // inheritance for language) for each query separately.
  $config
    ->setContext(function ($params, $document, $operation) use ($globals) {

    // Each document (e.g. in a batch query) gets its own resolve context. This
    // allows us to collect the cache metadata and contextual values (e.g.
    // inheritance for language) for each query separately.
    $context = new ResolveContext($globals, [
      'language' => $this->languageManager
        ->getCurrentLanguage()
        ->getId(),
    ]);
    $context
      ->addCacheTags([
      'graphql',
    ]);

    // Always add the language_url cache context.
    $context
      ->addCacheContexts([
      'languages:language_url',
      'languages:language_interface',
      'languages:language_content',
      'user.permissions',
    ]);
    return $context;
  });
  $config
    ->setValidationRules(function (OperationParams $params, DocumentNode $document, $operation) {
    if (isset($params->queryId) && empty($params
      ->getOriginalInput('query'))) {

      // Assume that pre-parsed documents are already validated. This allows
      // us to store pre-validated query documents e.g. for persisted queries
      // effectively improving performance by skipping run-time validation.
      return [];
    }
    return array_values(DocumentValidator::defaultRules());
  });
  $config
    ->setPersistentQueryLoader([
    $this->queryProvider,
    'getQuery',
  ]);
  $config
    ->setQueryBatching(TRUE);
  $config
    ->setDebug(!!$this->parameters['development']);
  $config
    ->setSchema($this
    ->getSchema());

  // Always log the errors.
  $config
    ->setErrorsHandler(function (array $errors, callable $formatter) {

    /** @var \GraphQL\Error\Error $error */
    foreach ($errors as $error) {
      $this->logger
        ->error($error
        ->getMessage());
    }
    return array_map($formatter, $errors);
  });
  return $config;
}