You are here

protected function QueryProcessor::executeOperation in GraphQL 8.3

Parameters

\GraphQL\Executor\Promise\PromiseAdapter $adapter:

\GraphQL\Server\ServerConfig $config:

\GraphQL\Server\OperationParams $params:

bool $batching:

Return value

\GraphQL\Executor\Promise\Promise

1 call to QueryProcessor::executeOperation()
QueryProcessor::executeOperationWithReporting in src/GraphQL/Execution/QueryProcessor.php

File

src/GraphQL/Execution/QueryProcessor.php, line 173

Class

QueryProcessor

Namespace

Drupal\graphql\GraphQL\Execution

Code

protected function executeOperation(PromiseAdapter $adapter, ServerConfig $config, OperationParams $params, $batching = FALSE) {
  try {
    if (!$config
      ->getSchema()) {
      throw new Error('Missing schema for query execution.');
    }
    if ($batching && !$config
      ->getQueryBatching()) {
      throw new RequestError('Batched queries are not supported by this server.');
    }
    if ($errors = $this
      ->validateOperationParams($params)) {
      return $adapter
        ->createFulfilled(new QueryResult(NULL, $errors));
    }
    $persisted = isset($params->queryId);
    $document = $persisted ? $this
      ->loadPersistedQuery($config, $params) : $params->query;
    if (!$document instanceof DocumentNode) {
      $document = Parser::parse($document);
    }

    // Read the operation type from the document. Subscriptions and mutations
    // only work through POST requests. One cannot have mutations and queries
    // in the same document, hence this check is sufficient.
    $operation = $params->operation;
    $type = AST::getOperation($document, $operation);
    if ($params
      ->isReadOnly() && $type !== 'query') {
      throw new RequestError('GET requests are only supported for query operations.');
    }

    // Only queries can be cached (mutations and subscriptions can't).
    if ($type === 'query') {
      return $this
        ->executeCacheableOperation($adapter, $config, $params, $document, !$persisted);
    }
    return $this
      ->executeUncachableOperation($adapter, $config, $params, $document, !$persisted);
  } catch (CacheableRequestError $exception) {
    return $adapter
      ->createFulfilled(new QueryResult(NULL, [
      Error::createLocatedError($exception),
    ], [], $exception));
  } catch (RequestError $exception) {
    return $adapter
      ->createFulfilled(new QueryResult(NULL, [
      Error::createLocatedError($exception),
    ]));
  } catch (Error $exception) {
    return $adapter
      ->createFulfilled(new QueryResult(NULL, [
      $exception,
    ]));
  }
}