/
FileProvider.php
79 lines (67 loc) · 2.04 KB
/
FileProvider.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
<?php
namespace SilverStripe\GraphQL\PersistedQuery;
use SilverStripe\Core\Config\Configurable;
use SilverStripe\Core\Injector\Injectable;
/**
* Class FileProvider
* @package SilverStripe\GraphQL\PersistedQuery
*/
class FileProvider implements PersistedQueryMappingProvider
{
use Configurable, Injectable;
/**
* Example:
* <code>
* SilverStripe\Core\Injector\Injector:
* SilverStripe\GraphQL\PersistedQuery\PersistedQueryMappingProvider:
* class: SilverStripe\GraphQL\PersistedQuery\FileProvider:
* properties:
* schemaMapping:
* default: '/var/www/project/persisted-graphql-query-mapping.json'
* </code>
*
* Note: The mapping supports multi-schema feature, you can have other schemaKey rather than 'default'
*
* @var array
* @config
*/
protected array $schemaToPath = [
'default' => ''
];
/**
* return a map from <id> to <query>
*/
public function getQueryMapping(string $schemaKey = 'default'): array
{
/** @noinspection PhpUndefinedFieldInspection */
/** @noinspection StaticInvocationViaThisInspection */
$pathWithKey = $this->getSchemaMapping();
if (!isset($pathWithKey[$schemaKey])) {
return [];
}
$path = trim($pathWithKey[$schemaKey] ?? '');
$contents = trim(file_get_contents($path ?? '') ?? '');
$result = json_decode($contents ?? '', true);
if (!is_array($result)) {
return [];
}
return $result;
}
/**
* return a query given an ID
*/
public function getByID(string $queryID, string $schemaKey = 'default'): ?string
{
$mapping = $this->getQueryMapping($schemaKey);
return isset($mapping[$queryID]) ? $mapping[$queryID] : null;
}
public function setSchemaMapping(array $mapping): self
{
$this->schemaToPath = $mapping;
return $this;
}
public function getSchemaMapping(): array
{
return $this->schemaToPath;
}
}