/
EntrypointAction.php
172 lines (142 loc) · 6.28 KB
/
EntrypointAction.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
<?php
/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <dunglas@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types=1);
namespace ApiPlatform\Core\GraphQl\Action;
use ApiPlatform\Core\GraphQl\ExecutorInterface;
use ApiPlatform\Core\GraphQl\Type\SchemaBuilderInterface;
use GraphQL\Error\Debug;
use GraphQL\Error\Error;
use GraphQL\Executor\ExecutionResult;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
/**
* GraphQL API entrypoint.
*
* @author Alan Poulain <contact@alanpoulain.eu>
*/
final class EntrypointAction
{
private $schemaBuilder;
private $executor;
private $graphiQlAction;
private $graphQlPlaygroundAction;
private $debug;
private $graphiqlEnabled;
private $graphQlPlaygroundEnabled;
private $defaultIde;
private $propertyAccessor;
public function __construct(SchemaBuilderInterface $schemaBuilder, ExecutorInterface $executor, GraphiQlAction $graphiQlAction, GraphQlPlaygroundAction $graphQlPlaygroundAction, PropertyAccessorInterface $propertyAccessor, bool $debug = false, bool $graphiqlEnabled = false, bool $graphQlPlaygroundEnabled = false, $defaultIde = false)
{
$this->schemaBuilder = $schemaBuilder;
$this->executor = $executor;
$this->graphiQlAction = $graphiQlAction;
$this->graphQlPlaygroundAction = $graphQlPlaygroundAction;
$this->debug = $debug ? Debug::INCLUDE_DEBUG_MESSAGE | Debug::INCLUDE_TRACE : false;
$this->graphiqlEnabled = $graphiqlEnabled;
$this->graphQlPlaygroundEnabled = $graphQlPlaygroundEnabled;
$this->defaultIde = $defaultIde;
$this->propertyAccessor = $propertyAccessor;
}
public function __invoke(Request $request): Response
{
if ($request->isMethod('GET') && 'html' === $request->getRequestFormat()) {
if ('graphiql' === $this->defaultIde && $this->graphiqlEnabled) {
return ($this->graphiQlAction)($request);
}
if ('graphql-playground' === $this->defaultIde && $this->graphQlPlaygroundEnabled) {
return ($this->graphQlPlaygroundAction)($request);
}
}
[$query, $operation, $variables] = $this->parseRequest($request);
if (null === $query) {
return new JsonResponse(new ExecutionResult(null, [new Error('GraphQL query is not valid')]), Response::HTTP_BAD_REQUEST);
}
if (null === $variables) {
return new JsonResponse(new ExecutionResult(null, [new Error('GraphQL variables are not valid JSON or multipart form map does not match the variables')]), Response::HTTP_BAD_REQUEST);
}
try {
$executionResult = $this->executor->executeQuery($this->schemaBuilder->getSchema(), $query, null, null, $variables, $operation);
} catch (\Exception $e) {
$executionResult = new ExecutionResult(null, [new Error($e->getMessage(), null, null, null, null, $e)]);
}
return new JsonResponse($executionResult->toArray($this->debug));
}
private function parseRequest(Request $request): array
{
$query = $request->query->get('query');
$operation = $request->query->get('operation');
if ($variables = $request->query->get('variables', [])) {
$variables = json_decode($variables, true);
}
if (!$request->isMethod('POST')) {
return [$query, $operation, $variables];
}
if ('json' === $request->getContentType()) {
return $this->parseInput($query, $variables, $operation, $request->getContent());
}
if (false !== mb_stripos($request->headers->get('CONTENT_TYPE'), 'multipart/form-data')) {
if ($request->request->has('operations')) {
[$query, $operation, $variables] = $this->parseInput($query, $variables, $operation, $request->request->get('operations'));
if ($request->request->has('map')) {
$variables = $this->applyMapToVariables($request, $variables);
}
return [$query, $operation, $variables];
}
}
if ('application/graphql' === $request->headers->get('CONTENT_TYPE')) {
$query = $request->getContent();
}
return [$query, $operation, $variables];
}
private function parseInput(?string $query, ?array $variables, ?string $operation, string $jsonContent): array
{
$input = json_decode($jsonContent, true);
if (isset($input['query'])) {
$query = $input['query'];
}
if (isset($input['variables'])) {
$variables = \is_array($input['variables']) ? $input['variables'] : json_decode($input['variables'], true);
}
if (isset($input['operation'])) {
$operation = $input['operation'];
}
return [$query, $operation, $variables];
}
private function applyMapToVariables(Request $request, array $variables): ?array
{
$mapValues = json_decode($request->request->get('map'), true);
if (!$mapValues) {
return $variables;
}
foreach ($mapValues as $key => $value) {
if ($request->files->has($key)) {
foreach ($mapValues[$key] as $mapValue) {
$path = explode('.', $mapValue);
if ('variables' === $path[0]) {
unset($path[0]);
$mapPathExistsInVariables = array_reduce($path, function (array $arr, $idx) {
return (
\array_key_exists($idx, $arr)
) ? $arr[$idx] : false;
}, $variables);
if (false !== $mapPathExistsInVariables) {
$this->propertyAccessor->setValue($variables, '['.implode('][', $path).']', $request->files->get($key));
} else {
return null;
}
}
}
}
}
return $variables;
}
}