Completed
Pull Request — 8.x-3.x (#497)
by Sebastian
03:22
created

QueryProcessor   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 94
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
dl 0
loc 94
rs 10
c 0
b 0
f 0
wmc 11
lcom 1
cbo 6

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
D processQuery() 0 34 10
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 Youshido\GraphQL\Execution\Processor;
10
11
class QueryProcessor {
12
13
  /**
14
   * The current user account.
15
   *
16
   * @var \Drupal\Core\Session\AccountProxyInterface
17
   */
18
  protected $currentUser;
19
20
  /**
21
   * The schema loader service.
22
   *
23
   * @var \Drupal\graphql\GraphQL\Schema\SchemaLoader
24
   */
25
  protected $schemaLoader;
26
27
  /**
28
   * The graphql container parameters.
29
   *
30
   * @var array
31
   */
32
  protected $parameters;
33
34
  /**
35
   * QueryProcessor constructor.
36
   *
37
   * @param \Drupal\Core\Session\AccountProxyInterface $currentUser
38
   *   The current user.
39
   * @param \Drupal\graphql\GraphQL\Schema\SchemaLoader $schemaLoader
40
   *   The schema loader service.
41
   * @param array $parameters
42
   *   The graphql container parameters.
43
   */
44
  public function __construct(
45
    SchemaLoader $schemaLoader,
46
    AccountProxyInterface $currentUser,
47
    array $parameters
48
  ) {
49
    $this->currentUser = $currentUser;
50
    $this->parameters = $parameters;
51
    $this->schemaLoader = $schemaLoader;
52
  }
53
54
  /**
55
   * Execute a GraphQL query.
56
   *
57
   * @param string $schemaId
58
   *   The name of the schema to process the query against.
59
   * @param string $query
60
   *   The GraphQL query.
61
   * @param array $variables
62
   *   The query variables.
63
   * @param bool $bypassSecurity
64
   *   Bypass field security
65
   *
66
   * @return \Drupal\graphql\GraphQL\Execution\QueryResult.
67
   *   The GraphQL query result.
68
   */
69
  public function processQuery($schemaId, $query, array $variables = [], $bypassSecurity = FALSE) {
70
    if (!$schema = $this->schemaLoader->getSchema($schemaId)) {
71
      throw new \InvalidArgumentException(sprintf('Could not load schema %s', [$schemaId]));
72
    }
73
74
    // Set up the processor with parameters to be used in the resolvers.
75
    $processor = new Processor($schema);
76
    $context = $processor->getExecutionContext();
77
    $container = $context->getContainer();
78
    $secure = !!($bypassSecurity || $this->currentUser->hasPermission('bypass graphql field security') || $this->parameters['development']);
79
    $container->set('secure', $secure);
80
    $container->set('metadata', new CacheableMetadata());
81
82
    // Run the query against the parser.
83
    $result = $processor->processPayload($query, $variables)->getResponseData();
84
85
    // Add collected cache metadata from the query processor.
86
    $responseCacheMetadata = new CacheableMetadata();
87
    if ($container->has('metadata') && ($metadata = $container->get('metadata'))) {
88
      if ($metadata instanceof CacheableDependencyInterface) {
0 ignored issues
show
Bug introduced by
The class Drupal\Core\Cache\CacheableDependencyInterface does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
89
        $responseCacheMetadata->addCacheableDependency($metadata);
90
      }
91
    }
92
93
    // Prevent caching if this is a mutation query or an error occurred.
94
    $request = $context->getRequest();
95
    if ((!empty($request) && $request->hasMutations()) || $context->hasErrors()) {
96
      $responseCacheMetadata->setCacheMaxAge(0);
97
    }
98
99
    // Load the schema's cache metadata.
100
    $schemaCacheMetadata = $this->schemaLoader->getResponseCacheMetadata($schemaId);
101
    return new QueryResult($result, $responseCacheMetadata, $schemaCacheMetadata);
102
  }
103
104
}
105