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

QueryProcessor::processQuery()   A

Complexity

Conditions 2
Paths 4

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 9
nc 4
nop 3
dl 0
loc 14
rs 9.4285
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 GraphQL\Error\FormattedError;
11
use GraphQL\GraphQL;
12
use GraphQL\Type\Definition\ResolveInfo;
13
14
class QueryProcessor {
15
16
  /**
17
   * The current user account.
18
   *
19
   * @var \Drupal\Core\Session\AccountProxyInterface
20
   */
21
  protected $currentUser;
22
23
  /**
24
   * The graphql container parameters.
25
   *
26
   * @var array
27
   */
28
  protected $parameters;
29
30
  /**
31
   * The schema plugin manager.
32
   *
33
   * @var \Drupal\graphql\Plugin\GraphQL\SchemaPluginManager
34
   */
35
  protected $pluginManager;
36
37
  /**
38
   * QueryProcessor constructor.
39
   *
40
   * @param \Drupal\graphql\Plugin\GraphQL\SchemaPluginManager $pluginManager
41
   *   The schema plugin manager.
42
   * @param \Drupal\Core\Session\AccountProxyInterface $currentUser
43
   *   The current user.
44
   * @param array $parameters
45
   *   The graphql container parameters.
46
   */
47
  public function __construct(
48
    SchemaPluginManager $pluginManager,
49
    AccountProxyInterface $currentUser,
50
    array $parameters
51
  ) {
52
    $this->pluginManager = $pluginManager;
53
    $this->currentUser = $currentUser;
54
    $this->parameters = $parameters;
55
  }
56
57
  /**
58
   * Execute a GraphQL query.
59
   *
60
   * @param string $id
61
   *   The name of the schema to process the query against.
62
   * @param string $query
63
   *   The GraphQL query.
64
   * @param array $variables
65
   *   The query variables.
66
   *
67
   * @return \Drupal\graphql\GraphQL\Execution\QueryResult.
68
   *   The GraphQL query result.
69
   */
70
  public function processQuery($id, $query, array $variables = []) {
71
    $debug = !empty($this->parameters['development']);
72
73
    try {
74
      $schema = $this->pluginManager->createInstance($id)->getSchema();
75
      $result = GraphQL::executeQuery($schema, $query, NULL, NULL, $variables, NULL);
76
      $output = $result->toArray($debug);
77
    }
78
    catch (\Exception $error) {
79
      $output = ['errors' => FormattedError::createFromException($error, $debug)];
0 ignored issues
show
Documentation introduced by
$error is of type object<Exception>, but the function expects a object<Throwable>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
80
    }
81
82
    return new QueryResult($output, new CacheableMetadata());
83
  }
84
85
}
86