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

QueryProcessor   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 100
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
C processQuery() 0 40 11
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
10
class QueryProcessor {
11
12
  /**
13
   * The current user account.
14
   *
15
   * @var \Drupal\Core\Session\AccountProxyInterface
16
   */
17
  protected $currentUser;
18
19
  /**
20
   * The schema loader service.
21
   *
22
   * @var \Drupal\graphql\GraphQL\Schema\SchemaLoader
23
   */
24
  protected $schemaLoader;
25
26
  /**
27
   * The graphql container parameters.
28
   *
29
   * @var array
30
   */
31
  protected $parameters;
32
33
  /**
34
   * QueryProcessor constructor.
35
   *
36
   * @param \Drupal\graphql\GraphQL\Schema\SchemaLoader $schemaLoader
37
   *   The schema loader service.
38
   * @param \Drupal\Core\Session\AccountProxyInterface $currentUser
39
   *   The current user.
40
   * @param array $parameters
41
   *   The graphql container parameters.
42
   */
43
  public function __construct(
44
    SchemaLoader $schemaLoader,
45
    AccountProxyInterface $currentUser,
46
    array $parameters
47
  ) {
48
    $this->currentUser = $currentUser;
49
    $this->parameters = $parameters;
50
    $this->schemaLoader = $schemaLoader;
51
  }
52
53
  /**
54
   * Execute a GraphQL query.
55
   *
56
   * @param string $schemaId
57
   *   The name of the schema to process the query against.
58
   * @param string $query
59
   *   The GraphQL query.
60
   * @param array $variables
61
   *   The query variables.
62
   * @param bool $bypassSecurity
63
   *   Bypass field security
64
   *
65
   * @return \Drupal\graphql\GraphQL\Execution\QueryResult.
66
   *   The GraphQL query result.
67
   */
68
  public function processQuery($schemaId, $query, array $variables = [], $bypassSecurity = FALSE) {
69
    if (!$schema = $this->schemaLoader->getSchema($schemaId)) {
70
      throw new \InvalidArgumentException(sprintf('Could not load schema %s', [$schemaId]));
71
    }
72
73
    // Set up the processor with parameters to be used in the resolvers.
74
    $processor = new Processor($schema);
75
    $context = $processor->getExecutionContext();
76
    $container = $context->getContainer();
77
    $secure = !!($bypassSecurity || $this->currentUser->hasPermission('bypass graphql field security') || $this->parameters['development']);
78
    $container->set('secure', $secure);
79
    $container->set('metadata', new CacheableMetadata());
80
81
    // Run the query against the parser.
82
    $result = $processor->processPayload($query, $variables)->getResponseData();
83
84
    // Add collected cache metadata from the query processor.
85
    $responseCacheMetadata = new CacheableMetadata();
86
    if ($container->has('metadata') && ($metadata = $container->get('metadata'))) {
87
      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...
88
        $responseCacheMetadata->addCacheableDependency($metadata);
89
      }
90
    }
91
92
    // Prevent caching if this is a mutation query or an error occurred.
93
    $request = $context->getRequest();
94
    if ((!empty($request) && $request->hasMutations()) || $context->hasErrors()) {
95
      $responseCacheMetadata->setCacheMaxAge(0);
96
    }
97
98
    // Do not cache this response anywhere (even page cache) if the graphql
99
    // cache is disabled through the service parameters.
100
    if (empty($config['result_cache'])) {
0 ignored issues
show
Bug introduced by
The variable $config seems to never exist, and therefore empty should always return true. Did you maybe rename this variable?

This check looks for calls to isset(...) or empty() on variables that are yet undefined. These calls will always produce the same result and can be removed.

This is most likely caused by the renaming of a variable or the removal of a function/method parameter.

Loading history...
101
      $responseCacheMetadata->setCacheMaxAge(0);
102
    }
103
104
    // Load the schema's cache metadata.
105
    $schemaCacheMetadata = $this->schemaLoader->getResponseCacheMetadata($schemaId);
106
    return new QueryResult($result, $responseCacheMetadata, $schemaCacheMetadata);
107
  }
108
109
}
110