1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Bakery\Http\Controllers; |
4
|
|
|
|
5
|
|
|
use Bakery\Bakery; |
6
|
|
|
use GraphQL\Error\Debug; |
7
|
|
|
use Illuminate\Support\Str; |
8
|
|
|
use Illuminate\Http\Request; |
9
|
|
|
use Illuminate\Http\JsonResponse; |
10
|
|
|
use Illuminate\Routing\Controller; |
11
|
|
|
use Illuminate\Contracts\Debug\ExceptionHandler; |
12
|
|
|
|
13
|
|
|
class BakeryController extends Controller |
14
|
|
|
{ |
15
|
|
|
protected function isExceptionHandlingDisabled() |
16
|
|
|
{ |
17
|
|
|
$handler = app(ExceptionHandler::class); |
18
|
|
|
|
19
|
|
|
return Str::contains(get_class($handler), 'InteractsWithExceptionHandling'); |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
protected function debug() |
23
|
|
|
{ |
24
|
|
|
$debug = null; |
25
|
|
|
|
26
|
|
|
if (config('app.debug') or app()->runningUnitTests()) { |
|
|
|
|
27
|
|
|
$debug = Debug::INCLUDE_DEBUG_MESSAGE; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
if ($this->isExceptionHandlingDisabled()) { |
31
|
|
|
$debug = Debug::RETHROW_INTERNAL_EXCEPTIONS; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
return $debug; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Handle an HTTP response containing the GraphQL query. |
39
|
|
|
* |
40
|
|
|
* @param Request $request |
41
|
|
|
* @param \Bakery\Bakery $bakery |
42
|
|
|
* @return JsonResponse |
43
|
|
|
* @throws \Exception |
44
|
|
|
*/ |
45
|
|
|
public function graphql(Request $request, Bakery $bakery): JsonResponse |
46
|
|
|
{ |
47
|
|
|
$input = $request->all(); |
48
|
|
|
|
49
|
|
|
$data = $bakery->executeQuery($input)->toArray($this->debug()); |
50
|
|
|
|
51
|
|
|
return response()->json($data, 200, []); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Serve the GraphiQL tool. |
56
|
|
|
* |
57
|
|
|
* @param \Illuminate\Http\Request $request |
58
|
|
|
* @param \Bakery\Bakery $bakery |
59
|
|
|
* @return \Illuminate\Contracts\View\View |
60
|
|
|
*/ |
61
|
|
|
public function graphiql(Request $request, Bakery $bakery) |
|
|
|
|
62
|
|
|
{ |
63
|
|
|
if (! app()->isLocal()) { |
|
|
|
|
64
|
|
|
abort(404); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
return $bakery->graphiql('bakery.graphql'); |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|