Completed
Pull Request — 8.x-3.x (#525)
by Sebastian
02:36
created

QueryProcessor::processQuery()   B

Complexity

Conditions 3
Paths 2

Size

Total Lines 29
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 17
nc 2
nop 3
dl 0
loc 29
rs 8.8571
c 0
b 0
f 0
1
<?php
2
3
namespace Drupal\graphql\GraphQL\Execution;
4
5
use Drupal\Core\Cache\CacheableDependencyInterface;
6
use Drupal\Core\Cache\CacheableMetadata;
7
use Drupal\Core\Session\AccountProxyInterface;
8
use Drupal\graphql\GraphQL\Schema\SchemaLoader;
9
use Drupal\graphql\Plugin\GraphQL\SchemaPluginManager;
10
use Drupal\graphql\QueryProvider\QueryProviderInterface;
11
use GraphQL\Error\FormattedError;
12
use GraphQL\Error\UserError;
13
use GraphQL\GraphQL;
14
use GraphQL\Server\Helper;
15
use GraphQL\Server\OperationParams;
16
use GraphQL\Server\RequestError;
17
use GraphQL\Server\ServerConfig;
18
use GraphQL\Type\Definition\ResolveInfo;
19
20
class QueryProcessor {
21
22
  /**
23
   * The current user account.
24
   *
25
   * @var \Drupal\Core\Session\AccountProxyInterface
26
   */
27
  protected $currentUser;
28
29
  /**
30
   * The graphql container parameters.
31
   *
32
   * @var array
33
   */
34
  protected $parameters;
35
36
  /**
37
   * The schema plugin manager.
38
   *
39
   * @var \Drupal\graphql\Plugin\GraphQL\SchemaPluginManager
40
   */
41
  protected $pluginManager;
42
43
  /**
44
   * The server helper.
45
   *
46
   * @var \GraphQL\Server\Helper
47
   */
48
  protected $helper;
49
50
  /**
51
   * @var \Drupal\graphql\QueryProvider\QueryProviderInterface
52
   */
53
  private $queryProvider;
54
55
  /**
56
   * QueryProcessor constructor.
57
   *
58
   * @param \Drupal\graphql\Plugin\GraphQL\SchemaPluginManager $pluginManager
59
   *   The schema plugin manager.
60
   * @param \Drupal\Core\Session\AccountProxyInterface $currentUser
61
   *   The current user.
62
   * @param \Drupal\graphql\QueryProvider\QueryProviderInterface $queryProvider
63
   *   The query provider service.
64
   * @param array $parameters
65
   *   The graphql container parameters.
66
   */
67
  public function __construct(
68
    SchemaPluginManager $pluginManager,
69
    AccountProxyInterface $currentUser,
70
    QueryProviderInterface $queryProvider,
71
    array $parameters
72
  ) {
73
    $this->pluginManager = $pluginManager;
74
    $this->currentUser = $currentUser;
75
    $this->queryProvider = $queryProvider;
76
    $this->parameters = $parameters;
77
    $this->helper = new Helper();
78
  }
79
80
  /**
81
   * Execute a GraphQL query.
82
   *
83
   * @param string $schema
84
   *   The name of the schema to execute.
85
   * @param \GraphQL\Server\OperationParams|\GraphQL\Server\OperationParams[] $operations
86
   *   The graphql operation(s) to execute.
87
   * @param array $config
88
   *
89
   * @return \Drupal\graphql\GraphQL\Execution\QueryResult .
90
   *   The GraphQL query result.
91
   */
92
  public function processQuery($schema, $operations, array $config = []) {
93
    $debug = !empty($this->parameters['development']);
94
95
    // Load the plugin from the schema manager.
96
    $plugin = $this->pluginManager->createInstance($schema);
97
    $schema = $plugin->getSchema();
98
99
    // Create the server config.
100
    $server = ServerConfig::create($config);
101
    $server->setDebug($debug);
102
    $server->setSchema($schema);
103
    $server->setQueryBatching(TRUE);
104
    $server->setPersistentQueryLoader(function ($id, OperationParams $operation) {
105
      if ($query = $this->queryProvider->getQuery($id, $operation)) {
106
        return $query;
107
      }
108
109
      throw new RequestError(sprintf("Failed to load query map for id '%s'.", $id));
110
    });
111
112
    if (is_array($operations)) {
113
      $output = $this->helper->executeBatch($server, $operations);
114
    }
115
    else {
116
      $output = $this->helper->executeOperation($server, $operations);
117
    }
118
119
    return new QueryResult($output->toArray($debug), new CacheableMetadata());
120
  }
121
122
}
123