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

RequestController::handleBatchRequest()   C

Complexity

Conditions 7
Paths 4

Size

Total Lines 84
Code Lines 46

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 46
nc 4
nop 2
dl 0
loc 84
rs 6.5389
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Drupal\graphql\Controller;
4
5
use Drupal\Core\Cache\Cache;
6
use Drupal\Core\Cache\CacheableJsonResponse;
7
use Drupal\Core\Cache\CacheableMetadata;
8
use Drupal\Core\Cache\CacheableResponseInterface;
9
use Drupal\Core\Cache\RefinableCacheableDependencyInterface;
10
use Drupal\Core\Config\Config;
11
use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
12
use Drupal\Core\Render\RenderContext;
13
use Drupal\Core\Render\RendererInterface;
14
use Drupal\Core\Url;
15
use Drupal\graphql\GraphQL\Execution\QueryProcessor;
16
use Symfony\Component\DependencyInjection\ContainerInterface;
17
use Symfony\Component\HttpFoundation\Request;
18
use Symfony\Component\HttpFoundation\RequestStack;
19
use Symfony\Component\HttpFoundation\Response;
20
use Symfony\Component\HttpKernel\HttpKernelInterface;
21
22
/**
23
 * Handles GraphQL requests.
24
 */
25
class RequestController implements ContainerInjectionInterface {
26
27
  /**
28
   * The renderer service.
29
   *
30
   * @var \Drupal\Core\Render\RendererInterface
31
   */
32
  protected $renderer;
33
34
  /**
35
   * The query processor.
36
   *
37
   * @var \Drupal\graphql\GraphQL\Execution\QueryProcessor
38
   */
39
  protected $processor;
40
41
  /**
42
   * {@inheritdoc}
43
   */
44
  public static function create(ContainerInterface $container) {
45
    return new static(
46
      $container->get('renderer'),
47
      $container->get('graphql.query_processor')
48
    );
49
  }
50
51
  /**
52
   * RequestController constructor.
53
   *
54
   * @param \Drupal\Core\Render\RendererInterface $renderer
55
   *   The renderer service.
56
   * @param \Drupal\graphql\GraphQL\Execution\QueryProcessor $processor
57
   *   The query processor.
58
   */
59
  public function __construct(
60
    RendererInterface $renderer,
61
    QueryProcessor $processor
62
  ) {
63
    $this->renderer = $renderer;
64
    $this->processor = $processor;
65
  }
66
67
  /**
68
   * Handles GraphQL requests.
69
   *
70
   * @param string $schema
71
   *   The name of the schema.
72
   * @param \GraphQL\Server\OperationParams|\GraphQL\Server\OperationParams[] $operations
73
   *   The graphql operation(s) to execute.
74
   *
75
   * @return \Drupal\Core\Cache\CacheableJsonResponse
76
   *   The JSON formatted response.
77
   */
78
  public function handleRequest($schema, $operations) {
79
    /** @var \Drupal\graphql\GraphQL\Execution\QueryResult $result */
80
    $result = NULL;
81
    $context = new RenderContext();
82
83
    // Evaluating the GraphQL request can potentially invoke rendering. We allow
84
    // those to "leak" and collect them here in a render context.
85
    $this->renderer->executeInRenderContext($context, function() use ($schema, $operations, &$result) {
86
      $result = $this->processor->processQuery($schema, $operations);
87
    });
88
89
    $response = new CacheableJsonResponse($result->getData());
90
    $response->addCacheableDependency($response->getCacheableMetadata());
91
    // Apply render context cache metadata to the response.
92
    if (!$context->isEmpty()) {
93
      $response->addCacheableDependency($context->pop());
94
    }
95
96
    return $response;
97
  }
98
}
99