GraphQLService::getStoredQuery()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 1
dl 0
loc 9
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Digia\Lumen\GraphQL;
4
5
use Youshido\GraphQL\Execution\Processor;
6
use Illuminate\Contracts\Cache\Repository as CacheRepository;
7
use Illuminate\Contracts\Filesystem\FileNotFoundException;
8
9
class GraphQLService
10
{
11
12
    /**
13
     * The cache key used when caching processor instances
14
     */
15
    public const PROCESSOR_CACHE_KEY = 'graphql_processor';
16
17
    /**
18
     * Path to graphql introspection file
19
     */
20
    public const INTROSPECTION_QUERY_PATH = 'graphql/Introspection.graphql';
21
22
23
    /**
24
     * @var Processor
25
     */
26
    private $processor;
27
28
    /**
29
     * @var CacheRepository
30
     */
31
    private $cacheRepository;
32
33
    /**
34
     * GraphQLController constructor.
35
     *
36
     * @param Processor       $processor
37
     * @param CacheRepository $cacheRepository
38
     */
39
    public function __construct(Processor $processor, CacheRepository $cacheRepository)
40
    {
41
        $this->processor       = $processor;
42
        $this->cacheRepository = $cacheRepository;
43
    }
44
45
    /**
46
     * Processor instantiation takes a while so we cache the instance forever. The processor cache can be cleared by
47
     * running "php artisan graphql:clear:processor:cache".
48
     *
49
     * @return Processor
50
     */
51
    public function getProcessor()
52
    {
53
        $processor = $this->cacheRepository->get(self::PROCESSOR_CACHE_KEY);
54
55
        if ($processor === null) {
56
            $processor = $this->processor;
57
58
            $this->cacheRepository->forever(self::PROCESSOR_CACHE_KEY, $processor);
59
        }
60
61
        return $processor;
62
    }
63
64
    /**
65
     * Removes the currently cached processor instance, if any
66
     */
67
    public function forgetProcessor()
68
    {
69
        $this->cacheRepository->forget(self::PROCESSOR_CACHE_KEY);
70
    }
71
72
    /**
73
     * @param string $queryResourcePath
74
     * @param array  $variables
75
     *
76
     * @return array
77
     *
78
     * @throws \Exception
79
     */
80
    public function getStoredQueryResponse(string $queryResourcePath, array $variables = []): array
81
    {
82
        return $this->getQueryResponse($this->getStoredQuery($queryResourcePath), $variables);
83
    }
84
85
    /**
86
     * @param string $query
87
     * @param array  $variables (optional)
88
     *
89
     * @return array
90
     *
91
     * @throws \Exception any underlying exception that occurred while processing the request
92
     */
93
    public function getQueryResponse(string $query, array $variables = []): array
94
    {
95
        $processor = $this->getProcessor();
96
        $response  = $processor->processPayload($query, $variables)->getResponseData();
97
98
        // Re-throw exceptions, we are not technically doing GraphQL
99
        if (isset($response['exceptions'])) {
100
            throw $response['exceptions'][0];
101
        }
102
103
        return $response;
104
    }
105
106
    /**
107
     * @param string $resourcePath
108
     *
109
     * @return string
110
     *
111
     * @throws FileNotFoundException
112
     */
113
    private function getStoredQuery(string $resourcePath): string
114
    {
115
        $path = resource_path($resourcePath);
116
117
        if (!file_exists($path)) {
118
            throw new FileNotFoundException('Could not find the specified query file');
119
        }
120
121
        return file_get_contents($path);
122
    }
123
}
124