1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Digia\Lumen\GraphQL; |
4
|
|
|
|
5
|
|
|
use Digia\Lumen\GraphQL\Contracts\TypeResolverInterface; |
6
|
|
|
use Youshido\GraphQL\Execution\Processor; |
7
|
|
|
use Illuminate\Cache\Repository as CacheRepository; |
8
|
|
|
use Illuminate\Contracts\Filesystem\FileNotFoundException; |
9
|
|
|
|
10
|
|
|
class GraphQLService |
11
|
|
|
{ |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* The cache key used when caching processor instances |
15
|
|
|
*/ |
16
|
|
|
const PROCESSOR_CACHE_KEY = 'graphql_processor'; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Path to graphql introspection file |
20
|
|
|
*/ |
21
|
|
|
const INTROSPECTION_QUERY_PATH = 'graphql/Introspection.graphql'; |
22
|
|
|
|
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var Processor |
26
|
|
|
*/ |
27
|
|
|
private $processor; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var TypeResolverInterface |
31
|
|
|
*/ |
32
|
|
|
private $typeResolver; |
|
|
|
|
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var CacheRepository |
36
|
|
|
*/ |
37
|
|
|
private $cacheRepository; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* GraphQLController constructor. |
41
|
|
|
* |
42
|
|
|
* @param Processor $processor |
43
|
|
|
*/ |
44
|
|
|
public function __construct(Processor $processor, CacheRepository $cacheRepository) |
45
|
|
|
{ |
46
|
|
|
$this->processor = $processor; |
47
|
|
|
$this->cacheRepository = $cacheRepository; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Processor instantiation takes a while so we cache the instance forever. The processor cache can be cleared by |
52
|
|
|
* running "php artisan graphql:clear:processor:cache". |
53
|
|
|
* |
54
|
|
|
* @return Processor |
55
|
|
|
*/ |
56
|
|
|
public function getProcessor() |
57
|
|
|
{ |
58
|
|
|
$processor = $this->cacheRepository->get(self::PROCESSOR_CACHE_KEY); |
59
|
|
|
|
60
|
|
|
if ($processor === null) { |
61
|
|
|
$processor = $this->processor; |
62
|
|
|
|
63
|
|
|
$this->cacheRepository->forever(self::PROCESSOR_CACHE_KEY, $processor); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
return $processor; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Removes the currently cached processor instance, if any |
71
|
|
|
*/ |
72
|
|
|
public function forgetProcessor() |
73
|
|
|
{ |
74
|
|
|
$this->cacheRepository->forget(self::PROCESSOR_CACHE_KEY); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @return array |
79
|
|
|
*/ |
80
|
|
|
public function getIntrospectionQueryResponse(): array |
81
|
|
|
{ |
82
|
|
|
return $this->getProcessor()->processPayload($this->getIntrospectionQuery())->getResponseData(); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @return string |
87
|
|
|
* |
88
|
|
|
* @throws FileNotFoundException |
89
|
|
|
*/ |
90
|
|
|
private function getIntrospectionQuery(): string |
91
|
|
|
{ |
92
|
|
|
$path = resource_path(self::INTROSPECTION_QUERY_PATH); |
93
|
|
|
|
94
|
|
|
if (!file_exists($path)) { |
95
|
|
|
throw new FileNotFoundException('Could not find introspection query file'); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
return file_get_contents($path); |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|