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 Symfony\Bundle\FrameworkBundle\Controller\Controller; |
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
|
|
|
/** |
23
|
|
|
* Class GraphQLEndpointController |
24
|
|
|
*/ |
25
|
|
|
class GraphQLEndpointController extends Controller |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* @param Request $request |
29
|
|
|
* |
30
|
|
|
* @return JsonResponse |
31
|
|
|
*/ |
32
|
14 |
|
public function rootAction(Request $request): JsonResponse |
33
|
|
|
{ |
34
|
14 |
|
$debugMode = $this->container->getParameter('kernel.debug'); |
35
|
|
|
|
36
|
14 |
|
if (!$debugMode && $request->getMethod() !== Request::METHOD_POST) { |
37
|
|
|
throw new HttpException(Response::HTTP_BAD_REQUEST, 'The method should be POST to talk with GraphQL API'); |
38
|
|
|
} |
39
|
|
|
|
40
|
14 |
|
$schema = $this->get(SchemaCompiler::class)->compile(); |
41
|
|
|
|
42
|
14 |
|
$input = json_decode($request->getContent(), true); |
43
|
14 |
|
$query = $input['query']; |
44
|
14 |
|
$variableValues = isset($input['variables']) ? $input['variables'] : null; |
45
|
14 |
|
$operationName = isset($input['operationName']) ? $input['operationName'] : null; |
46
|
|
|
|
47
|
|
|
try { |
48
|
14 |
|
$schema->assertValid(); |
49
|
14 |
|
$context = null; |
50
|
14 |
|
$result = GraphQL::executeQuery($schema, $query, $context, null, $variableValues, $operationName); |
51
|
|
|
|
52
|
14 |
|
$debug = false; |
53
|
14 |
|
if ($debugMode) { |
54
|
14 |
|
$debug = Debug::INCLUDE_DEBUG_MESSAGE | Debug::INCLUDE_TRACE; |
55
|
|
|
} |
56
|
|
|
|
57
|
14 |
|
$output = $result->toArray($debug); |
58
|
14 |
|
$statusCode = Response::HTTP_OK; |
59
|
|
|
|
60
|
14 |
|
if (isset($output['errors'])) { |
61
|
14 |
|
$statusCode = Response::HTTP_BAD_REQUEST; |
62
|
|
|
} |
63
|
|
|
} catch (\Exception $e) { |
64
|
|
|
if ($this->has('logger')) { |
65
|
|
|
$this->get('logger')->error($e->getMessage(), $e->getTrace()); |
66
|
|
|
} |
67
|
|
|
$statusCode = Response::HTTP_INTERNAL_SERVER_ERROR; |
68
|
|
|
$output['errors']['message'] = $e->getMessage(); |
69
|
|
|
$output['errors']['category'] = 'internal'; |
70
|
|
|
|
71
|
|
|
if ($debugMode) { |
72
|
|
|
$output['errors']['trace'] = $e->getTraceAsString(); |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|
76
|
14 |
|
return JsonResponse::create($output, $statusCode); |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|