Completed
Push — 8.x-3.x ( b2cda0...629366 )
by Sebastian
05:52 queued 03:52
created

QueryProcessor::processQuery()   C

Complexity

Conditions 7
Paths 13

Size

Total Lines 30
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 16
nc 13
nop 4
dl 0
loc 30
rs 6.7272
c 0
b 0
f 0
1
<?php
2
3
namespace Drupal\graphql\GraphQL\Execution;
4
5
use Drupal\Core\Cache\Cache;
6
use Drupal\Core\Cache\CacheableMetadata;
7
use Drupal\Core\Session\AccountProxyInterface;
8
use Drupal\graphql\GraphQL\Reducers\ReducerManager;
9
use Drupal\graphql\GraphQL\Schema\SchemaLoader;
10
use Symfony\Component\DependencyInjection\ContainerInterface;
11
use Youshido\GraphQL\Schema\AbstractSchema;
12
13
/**
14
 * Drupal service for executing GraphQL queries.
15
 */
16
class QueryProcessor {
17
18
  /**
19
   * The dependency injection container.
20
   *
21
   * @var \Symfony\Component\DependencyInjection\ContainerInterface
22
   */
23
  protected $container;
24
25
  /**
26
   * The current user account.
27
   *
28
   * @var \Drupal\Core\Session\AccountProxyInterface
29
   */
30
  protected $currentUser;
31
32
  /**
33
   * The schema loader service.
34
   *
35
   * @var \Drupal\graphql\GraphQL\Schema\SchemaLoader
36
   */
37
  protected $schemaLoader;
38
39
  /**
40
   * The graphql container parameters.
41
   *
42
   * @var array
43
   */
44
  protected $parameters;
45
46
  /**
47
   * QueryProcessor constructor.
48
   *
49
   * @param \Symfony\Component\DependencyInjection\ContainerInterface $container
50
   *   The dependency injection container.
51
   * @param \Drupal\Core\Session\AccountProxyInterface $currentUser
52
   *   The current user.
53
   * @param \Drupal\graphql\GraphQL\Schema\SchemaLoader $schemaLoader
54
   *   The schema loader service.
55
   * @param array $parameters
56
   *   The graphql container parameters.
57
   */
58
  public function __construct(
59
    ContainerInterface $container,
60
    SchemaLoader $schemaLoader,
61
    AccountProxyInterface $currentUser,
62
    array $parameters
63
  ) {
64
    $this->container = $container;
65
    $this->currentUser = $currentUser;
66
    $this->parameters = $parameters;
67
    $this->schemaLoader = $schemaLoader;
68
  }
69
70
  /**
71
   * Execute a GraphQL query.
72
   *
73
   * @param string $schemaId
74
   *   The name of the schema to process the query against.
75
   * @param string $query
76
   *   The GraphQL query.
77
   * @param array $variables
78
   *   The query variables.
79
   * @param bool $bypassSecurity
80
   *   Bypass field security
81
   *
82
   * @return \Drupal\graphql\GraphQL\Execution\QueryResult.
83
   *   The GraphQL query result.
84
   */
85
  public function processQuery($schemaId, $query, array $variables = [], $bypassSecurity = FALSE) {
86
    if (!$schema = $this->schemaLoader->getSchema($schemaId)) {
87
      throw new \InvalidArgumentException(sprintf('Could not load schema %s', [$schemaId]));
88
    }
89
90
    /** @var \Youshido\GraphQL\Schema\AbstractSchema $schema */
91
    $secure = !!($bypassSecurity || $this->currentUser->hasPermission('bypass graphql field security') || $this->parameters['development']);
92
    $processor = new Processor($this->container, $schema, $secure);
93
    $processor->processPayload($query, $variables);
94
95
    // Add collected cache metadata from the query processor.
96
    $metadata = new CacheableMetadata();
97
    $context = $processor->getExecutionContext();
98
    $container = $context->getContainer();
99
    if ($container->has('metadata')) {
100
      $metadata->addCacheableDependency($container->get('metadata'));
101
    }
102
103
    // Add cache metadata from the schema in use.
104
    /** @var \Drupal\graphql\Plugin\GraphQL\SchemaPluginInterface $schema */
105
    $metadata->addCacheableDependency($schema->getResponseCacheMetadata());
106
107
    // Prevent caching if this is a mutation query.
108
    $request = $context->getRequest();
109
    if (!empty($request) && $request->hasMutations()) {
110
      $metadata->setCacheMaxAge(0);
111
    }
112
113
    return new QueryResult($processor->getResponseData(), $metadata);
114
  }
115
116
}