Completed
Pull Request — master (#1358)
by Alan
03:19
created

GraphqlEntrypointAction   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 80
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 8

Importance

Changes 0
Metric Value
wmc 16
lcom 0
cbo 8
dl 0
loc 80
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
B __invoke() 0 20 6
D parseRequest() 0 36 9
1
<?php
2
3
/*
4
 * This file is part of the API Platform project.
5
 *
6
 * (c) Kévin Dunglas <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace ApiPlatform\Core\Action;
15
16
use ApiPlatform\Core\Bridge\Graphql\ExecutorInterface;
17
use ApiPlatform\Core\Bridge\Graphql\Type\SchemaBuilderInterface;
18
use Symfony\Component\HttpFoundation\JsonResponse;
19
use Symfony\Component\HttpFoundation\Request;
20
use Symfony\Component\HttpFoundation\Response;
21
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
22
23
/**
24
 * GraphQL API entrypoint.
25
 *
26
 * @author Alan Poulain <[email protected]>
27
 */
28
final class GraphqlEntrypointAction
29
{
30
    private $schemaBuilder;
31
    private $executor;
32
    private $twig;
33
    private $debug;
34
    private $title;
35
    private $graphiqlEnabled;
36
37
    public function __construct(SchemaBuilderInterface $schemaBuilder, ExecutorInterface $executor, \Twig_Environment $twig, bool $debug = false, bool $graphiqlEnabled = false, string $title = '')
38
    {
39
        $this->schemaBuilder = $schemaBuilder;
40
        $this->executor = $executor;
41
        $this->twig = $twig;
42
        $this->debug = $debug;
43
        $this->graphiqlEnabled = $graphiqlEnabled;
44
        $this->title = $title;
45
    }
46
47
    /**
48
     * @throws BadRequestHttpException
49
     */
50
    public function __invoke(Request $request): Response
51
    {
52
        if ($this->graphiqlEnabled && $request->isMethod('GET') && 'html' === $request->getRequestFormat()) {
53
            return new Response($this->twig->render('@ApiPlatform/Graphiql/index.html.twig', ['title' => $this->title]));
54
        }
55
56
        list($query, $operation, $variables) = $this->parseRequest($request);
57
58
        if (null === $query) {
59
            return new JsonResponse(['error' => 'GraphQL query is not valid'], Response::HTTP_BAD_REQUEST);
60
        }
61
62
        if (null === $variables) {
63
            return new JsonResponse(['error' => 'GraphQL variables are not valid JSON'], Response::HTTP_BAD_REQUEST);
64
        }
65
66
        $executionResult = $this->executor->executeQuery($this->schemaBuilder->getSchema(), $query, null, null, $variables, $operation);
67
68
        return new JsonResponse($executionResult->toArray($this->debug));
69
    }
70
71
    private function parseRequest(Request $request): array
72
    {
73
        $query = $request->query->get('query');
74
        $operation = $request->query->get('operation');
75
        if ($variables = $request->query->get('variables', [])) {
76
            $variables = \json_decode($variables, true);
77
        }
78
79
        if (!$request->isMethod('POST')) {
80
            return [$query, $operation, $variables];
81
        }
82
83
        if ('json' === $request->getContentType()) {
84
            $input = \json_decode($request->getContent(), true);
85
86
            if (isset($input['query'])) {
87
                $query = $input['query'];
88
            }
89
90
            if (isset($input['variables'])) {
91
                $variables = \is_array($input['variables']) ? $input['variables'] : \json_decode($input['variables'], true);
92
            }
93
94
            if (isset($input['operation'])) {
95
                $operation = $input['operation'];
96
            }
97
98
            return [$query, $operation, $variables];
99
        }
100
101
        if ('application/graphql' === $request->headers->get('CONTENT_TYPE')) {
102
            $query = $request->getContent();
103
        }
104
105
        return [$query, $operation, $variables];
106
    }
107
}
108