Completed
Pull Request — 8.x-3.x (#525)
by Sebastian
10:05 queued 07:48
created

QueryProcessor   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 107
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
dl 0
loc 107
rs 10
c 0
b 0
f 0
wmc 4
lcom 1
cbo 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 12 1
B processQuery() 0 30 3
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
   * The query provider service.
52
   *
53
   * @var \Drupal\graphql\QueryProvider\QueryProviderInterface
54
   */
55
  protected $queryProvider;
56
57
  /**
58
   * QueryProcessor constructor.
59
   *
60
   * @param \Drupal\graphql\Plugin\GraphQL\SchemaPluginManager $pluginManager
61
   *   The schema plugin manager.
62
   * @param \Drupal\Core\Session\AccountProxyInterface $currentUser
63
   *   The current user.
64
   * @param \Drupal\graphql\QueryProvider\QueryProviderInterface $queryProvider
65
   *   The query provider service.
66
   * @param array $parameters
67
   *   The graphql container parameters.
68
   */
69
  public function __construct(
70
    SchemaPluginManager $pluginManager,
71
    AccountProxyInterface $currentUser,
72
    QueryProviderInterface $queryProvider,
73
    array $parameters
74
  ) {
75
    $this->pluginManager = $pluginManager;
76
    $this->currentUser = $currentUser;
77
    $this->queryProvider = $queryProvider;
78
    $this->parameters = $parameters;
79
    $this->helper = new Helper();
80
  }
81
82
  /**
83
   * Execute a GraphQL query.
84
   *
85
   * @param string $schema
86
   *   The name of the schema to execute.
87
   * @param \GraphQL\Server\OperationParams|\GraphQL\Server\OperationParams[] $operations
88
   *   The graphql operation(s) to execute.
89
   * @param mixed $context
90
   *   The context for the query.
91
   *
92
   * @return \Drupal\graphql\GraphQL\Execution\QueryResult .
93
   *   The GraphQL query result.
94
   */
95
  public function processQuery($schema, $operations, $context = NULL) {
96
    $debug = !empty($this->parameters['development']);
97
98
    // Load the plugin from the schema manager.
99
    $plugin = $this->pluginManager->createInstance($schema);
100
    $schema = $plugin->getSchema();
101
102
    // Create the server config.
103
    $server = ServerConfig::create();
104
    $server->setDebug($debug);
105
    $server->setSchema($schema);
106
    $server->setContext($context);
107
    $server->setQueryBatching(TRUE);
108
    $server->setPersistentQueryLoader(function ($id, OperationParams $operation) {
109
      if ($query = $this->queryProvider->getQuery($id, $operation)) {
110
        return $query;
111
      }
112
113
      throw new RequestError(sprintf("Failed to load query map for id '%s'.", $id));
114
    });
115
116
    if (is_array($operations)) {
117
      $output = $this->helper->executeBatch($server, $operations);
118
    }
119
    else {
120
      $output = $this->helper->executeOperation($server, $operations);
121
    }
122
123
    return new QueryResult($output->toArray($debug), new CacheableMetadata());
124
  }
125
126
}
127