1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace GraphQLAPI\GraphQLAPI\ModuleResolvers; |
6
|
|
|
|
7
|
|
|
use GraphQLAPI\GraphQLAPI\Plugin; |
8
|
|
|
use GraphQLAPI\GraphQLAPI\ModuleSettings\Properties; |
9
|
|
|
use GraphQLAPI\GraphQLAPI\ModuleResolvers\ModuleResolverTrait; |
10
|
|
|
use GraphQLAPI\GraphQLAPI\ModuleResolvers\SchemaConfigurationFunctionalityModuleResolver; |
11
|
|
|
use GraphQLAPI\GraphQLAPI\ModuleTypeResolvers\ModuleTypeResolver; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* The cache modules have different behavior depending on the environment: |
15
|
|
|
* - "development": visible, disabled by default |
16
|
|
|
* - "production": hidden, enabled by default |
17
|
|
|
* |
18
|
|
|
* @author Leonardo Losoviz <[email protected]> |
19
|
|
|
*/ |
20
|
|
|
class PerformanceFunctionalityModuleResolver extends AbstractFunctionalityModuleResolver |
21
|
|
|
{ |
22
|
|
|
use ModuleResolverTrait; |
23
|
|
|
|
24
|
|
|
public const CACHE_CONTROL = Plugin::NAMESPACE . '\cache-control'; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Setting options |
28
|
|
|
*/ |
29
|
|
|
public const OPTION_MAX_AGE = 'max-age'; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @return string[] |
33
|
|
|
*/ |
34
|
|
|
public static function getModulesToResolve(): array |
35
|
|
|
{ |
36
|
|
|
return [ |
37
|
|
|
self::CACHE_CONTROL, |
38
|
|
|
]; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Enable to customize a specific UI for the module |
43
|
|
|
*/ |
44
|
|
|
public function getModuleType(string $module): string |
45
|
|
|
{ |
46
|
|
|
return ModuleTypeResolver::PERFORMANCE; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @return array<array> List of entries that must be satisfied, each entry is an array where at least 1 module must be satisfied |
51
|
|
|
*/ |
52
|
|
|
public function getDependedModuleLists(string $module): array |
53
|
|
|
{ |
54
|
|
|
switch ($module) { |
55
|
|
|
case self::CACHE_CONTROL: |
56
|
|
|
return [ |
57
|
|
|
[ |
58
|
|
|
SchemaConfigurationFunctionalityModuleResolver::SCHEMA_CONFIGURATION, |
59
|
|
|
], |
60
|
|
|
[ |
61
|
|
|
EndpointFunctionalityModuleResolver::PERSISTED_QUERIES, |
62
|
|
|
], |
63
|
|
|
]; |
64
|
|
|
} |
65
|
|
|
return parent::getDependedModuleLists($module); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
public function getName(string $module): string |
69
|
|
|
{ |
70
|
|
|
$names = [ |
71
|
|
|
self::CACHE_CONTROL => \__('Cache Control', 'graphql-api'), |
72
|
|
|
]; |
73
|
|
|
return $names[$module] ?? $module; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
public function getDescription(string $module): string |
77
|
|
|
{ |
78
|
|
|
switch ($module) { |
79
|
|
|
case self::CACHE_CONTROL: |
80
|
|
|
return \__('Provide HTTP Caching for Persisted Queries, sending the Cache-Control header with a max-age value calculated from all fields in the query', 'graphql-api'); |
81
|
|
|
} |
82
|
|
|
return parent::getDescription($module); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Default value for an option set by the module |
87
|
|
|
* |
88
|
|
|
* @param string $module |
89
|
|
|
* @param string $option |
90
|
|
|
* @return mixed Anything the setting might be: an array|string|bool|int|null |
91
|
|
|
*/ |
92
|
|
|
public function getSettingsDefaultValue(string $module, string $option) |
93
|
|
|
{ |
94
|
|
|
$defaultValues = [ |
95
|
|
|
self::CACHE_CONTROL => [ |
96
|
|
|
self::OPTION_MAX_AGE => 86400, // 1 day |
97
|
|
|
], |
98
|
|
|
]; |
99
|
|
|
return $defaultValues[$module][$option]; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Array with the inputs to show as settings for the module |
104
|
|
|
* |
105
|
|
|
* @return array<array> List of settings for the module, each entry is an array with property => value |
106
|
|
|
*/ |
107
|
|
|
public function getSettings(string $module): array |
108
|
|
|
{ |
109
|
|
|
$moduleSettings = parent::getSettings($module); |
110
|
|
|
// Do the if one by one, so that the SELECT do not get evaluated unless needed |
111
|
|
|
if ($module == self::CACHE_CONTROL) { |
112
|
|
|
$option = self::OPTION_MAX_AGE; |
113
|
|
|
$moduleSettings[] = [ |
114
|
|
|
Properties::INPUT => $option, |
115
|
|
|
Properties::NAME => $this->getSettingOptionName( |
116
|
|
|
$module, |
117
|
|
|
$option |
118
|
|
|
), |
119
|
|
|
Properties::TITLE => \__('Default max-age', 'graphql-api'), |
120
|
|
|
Properties::DESCRIPTION => \__('Default max-age value (in seconds) for the Cache-Control header, for all fields and directives in the schema', 'graphql-api'), |
121
|
|
|
Properties::TYPE => Properties::TYPE_INT, |
122
|
|
|
Properties::MIN_NUMBER => 0, |
123
|
|
|
]; |
124
|
|
|
} |
125
|
|
|
return $moduleSettings; |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
|