1
|
|
|
<?php |
2
|
|
|
/******************************************************************************* |
3
|
|
|
* This file is part of the GraphQL Bundle package. |
4
|
|
|
* |
5
|
|
|
* (c) YnloUltratech <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
******************************************************************************/ |
10
|
|
|
|
11
|
|
|
namespace Ynlo\GraphQLBundle\Controller; |
12
|
|
|
|
13
|
|
|
use GraphQL\Error\Debug; |
14
|
|
|
use GraphQL\GraphQL; |
15
|
|
|
use GraphQL\Validator\DocumentValidator; |
16
|
|
|
use GraphQL\Validator\Rules; |
17
|
|
|
use Psr\Log\LoggerInterface; |
18
|
|
|
use Symfony\Component\HttpFoundation\JsonResponse; |
19
|
|
|
use Symfony\Component\HttpFoundation\Request; |
20
|
|
|
use Symfony\Component\HttpFoundation\Response; |
21
|
|
|
use Symfony\Component\HttpKernel\Exception\HttpException; |
22
|
|
|
use Ynlo\GraphQLBundle\Schema\SchemaCompiler; |
23
|
|
|
|
24
|
|
|
class GraphQLEndpointController |
25
|
|
|
{ |
26
|
|
|
private $compiler; |
27
|
|
|
private $debug; |
28
|
21 |
|
private $logger; |
29
|
|
|
|
30
|
21 |
|
public function __construct(SchemaCompiler $compiler, bool $debug, LoggerInterface $logger = null) |
31
|
21 |
|
{ |
32
|
21 |
|
$this->compiler = $compiler; |
33
|
21 |
|
$this->debug = $debug; |
34
|
|
|
$this->logger = $logger; |
35
|
21 |
|
} |
36
|
|
|
|
37
|
21 |
|
public function __invoke(Request $request): JsonResponse |
38
|
|
|
{ |
39
|
|
|
if (!$this->debug && $request->getMethod() !== Request::METHOD_POST) { |
40
|
|
|
throw new HttpException(Response::HTTP_BAD_REQUEST, 'The method should be POST to talk with GraphQL API'); |
41
|
21 |
|
} |
42
|
|
|
|
43
|
21 |
|
$input = json_decode($request->getContent(), true); |
44
|
21 |
|
$query = $input['query']; |
45
|
21 |
|
$context = null; |
46
|
21 |
|
$variableValues = $input['variables'] ?? null; |
47
|
|
|
$operationName = $input['operationName'] ?? null; |
48
|
|
|
$validationRules = null; // <-- this will override global validation rules for this request |
49
|
21 |
|
|
50
|
21 |
|
try { |
51
|
21 |
|
$schema = $this->compiler->compile(); |
52
|
|
|
$schema->assertValid(); |
53
|
21 |
|
|
54
|
21 |
|
$result = GraphQL::executeQuery($schema, $query, null, $context, $variableValues, $operationName, null, $validationRules); |
55
|
21 |
|
|
56
|
|
|
$debug = false; |
57
|
|
|
if ($this->debug) { |
58
|
21 |
|
$debug = Debug::INCLUDE_DEBUG_MESSAGE | Debug::INCLUDE_TRACE; |
59
|
21 |
|
} |
60
|
|
|
$output = $result->toArray($debug); |
61
|
21 |
|
$statusCode = Response::HTTP_OK; |
62
|
21 |
|
|
63
|
|
|
if (isset($output['errors'])) { |
64
|
|
|
$statusCode = Response::HTTP_BAD_REQUEST; |
65
|
|
|
} |
66
|
|
|
} catch (\Exception $e) { |
67
|
|
|
if (null !== $this->logger) { |
68
|
|
|
$this->logger->error($e->getMessage(), $e->getTrace()); |
69
|
|
|
} |
70
|
|
|
$statusCode = Response::HTTP_INTERNAL_SERVER_ERROR; |
71
|
|
|
$output['errors']['message'] = $e->getMessage(); |
72
|
|
|
$output['errors']['category'] = 'internal'; |
73
|
|
|
|
74
|
|
|
if ($this->debug) { |
75
|
|
|
$output['errors']['trace'] = $e->getTraceAsString(); |
76
|
|
|
} |
77
|
21 |
|
} |
78
|
|
|
|
79
|
|
|
return JsonResponse::create($output, $statusCode); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
public function addGlobalValidationRules(array $validationRules): void |
83
|
|
|
{ |
84
|
|
|
$rules = []; |
85
|
|
|
if (!empty($validationRules['query_complexity'])) { |
86
|
|
|
$rules[] = new Rules\QueryComplexity($validationRules['query_complexity']); |
87
|
|
|
} |
88
|
|
|
if (!empty($validationRules['query_depth'])) { |
89
|
|
|
$rules[] = new Rules\QueryDepth($validationRules['query_depth']); |
90
|
|
|
} |
91
|
|
|
if (!empty($validationRules['disable_introspection'])) { |
92
|
|
|
$rules[] = new Rules\DisableIntrospection(); |
93
|
|
|
} |
94
|
|
|
array_map([DocumentValidator::class, 'addRule'], $rules); |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|