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 Psr\Log\LoggerInterface; |
16
|
|
|
use Symfony\Component\HttpFoundation\JsonResponse; |
17
|
|
|
use Symfony\Component\HttpFoundation\Request; |
18
|
|
|
use Symfony\Component\HttpFoundation\Response; |
19
|
|
|
use Symfony\Component\HttpKernel\Exception\HttpException; |
20
|
|
|
use Ynlo\GraphQLBundle\Schema\SchemaCompiler; |
21
|
|
|
|
22
|
|
|
class GraphQLEndpointController |
23
|
|
|
{ |
24
|
|
|
private $compiler; |
25
|
|
|
private $debug; |
26
|
|
|
private $logger; |
27
|
|
|
|
28
|
21 |
|
public function __construct(SchemaCompiler $compiler, bool $debug, LoggerInterface $logger = null) |
29
|
|
|
{ |
30
|
21 |
|
$this->compiler = $compiler; |
31
|
21 |
|
$this->debug = $debug; |
32
|
21 |
|
$this->logger = $logger; |
33
|
21 |
|
} |
34
|
|
|
|
35
|
21 |
|
public function __invoke(Request $request): JsonResponse |
36
|
|
|
{ |
37
|
21 |
|
if (!$this->debug && $request->getMethod() !== Request::METHOD_POST) { |
38
|
|
|
throw new HttpException(Response::HTTP_BAD_REQUEST, 'The method should be POST to talk with GraphQL API'); |
39
|
|
|
} |
40
|
|
|
|
41
|
21 |
|
$schema = $this->compiler->compile(); |
42
|
|
|
|
43
|
21 |
|
$input = json_decode($request->getContent(), true); |
44
|
21 |
|
$query = $input['query']; |
45
|
21 |
|
$variableValues = $input['variables'] ?? null; |
46
|
21 |
|
$operationName = $input['operationName'] ?? null; |
47
|
|
|
|
48
|
|
|
try { |
49
|
21 |
|
$schema->assertValid(); |
50
|
21 |
|
$context = null; |
51
|
21 |
|
$result = GraphQL::executeQuery($schema, $query, $context, null, $variableValues, $operationName); |
52
|
|
|
|
53
|
21 |
|
$debug = false; |
54
|
21 |
|
if ($this->debug) { |
55
|
21 |
|
$debug = Debug::INCLUDE_DEBUG_MESSAGE | Debug::INCLUDE_TRACE; |
56
|
|
|
} |
57
|
|
|
|
58
|
21 |
|
$output = $result->toArray($debug); |
59
|
21 |
|
$statusCode = Response::HTTP_OK; |
60
|
|
|
|
61
|
21 |
|
if (isset($output['errors'])) { |
62
|
21 |
|
$statusCode = Response::HTTP_BAD_REQUEST; |
63
|
|
|
} |
64
|
|
|
} catch (\Exception $e) { |
65
|
|
|
if (null !== $this->logger) { |
66
|
|
|
$this->logger->error($e->getMessage(), $e->getTrace()); |
67
|
|
|
} |
68
|
|
|
$statusCode = Response::HTTP_INTERNAL_SERVER_ERROR; |
69
|
|
|
$output['errors']['message'] = $e->getMessage(); |
70
|
|
|
$output['errors']['category'] = 'internal'; |
71
|
|
|
|
72
|
|
|
if ($this->debug) { |
73
|
|
|
$output['errors']['trace'] = $e->getTraceAsString(); |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|
77
|
21 |
|
return JsonResponse::create($output, $statusCode); |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|