|
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\Facades\ModuleRegistryFacade; |
|
10
|
|
|
use GraphQLAPI\GraphQLAPI\ModuleResolvers\ModuleResolverTrait; |
|
11
|
|
|
use GraphQLByPoP\GraphQLEndpointForWP\ComponentConfiguration as GraphQLEndpointForWPComponentConfiguration; |
|
12
|
|
|
|
|
13
|
|
|
class EndpointFunctionalityModuleResolver extends AbstractFunctionalityModuleResolver |
|
14
|
|
|
{ |
|
15
|
|
|
use ModuleResolverTrait; |
|
16
|
|
|
|
|
17
|
|
|
// public const MAIN = Plugin::NAMESPACE . '\main'; |
|
18
|
|
|
public const SINGLE_ENDPOINT = Plugin::NAMESPACE . '\single-endpoint'; |
|
19
|
|
|
public const PERSISTED_QUERIES = Plugin::NAMESPACE . '\persisted-queries'; |
|
20
|
|
|
public const CUSTOM_ENDPOINTS = Plugin::NAMESPACE . '\custom-endpoints'; |
|
21
|
|
|
public const API_HIERARCHY = Plugin::NAMESPACE . '\api-hierarchy'; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Setting options |
|
25
|
|
|
*/ |
|
26
|
|
|
public const OPTION_PATH = 'path'; |
|
27
|
|
|
|
|
28
|
|
|
public static function getModulesToResolve(): array |
|
29
|
|
|
{ |
|
30
|
|
|
return [ |
|
31
|
|
|
// self::MAIN, |
|
32
|
|
|
self::SINGLE_ENDPOINT, |
|
33
|
|
|
self::PERSISTED_QUERIES, |
|
34
|
|
|
self::CUSTOM_ENDPOINTS, |
|
35
|
|
|
self::API_HIERARCHY, |
|
36
|
|
|
]; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
public function getDependedModuleLists(string $module): array |
|
40
|
|
|
{ |
|
41
|
|
|
switch ($module) { |
|
42
|
|
|
case self::PERSISTED_QUERIES: |
|
43
|
|
|
case self::SINGLE_ENDPOINT: |
|
44
|
|
|
case self::CUSTOM_ENDPOINTS: |
|
45
|
|
|
return []; |
|
46
|
|
|
case self::API_HIERARCHY: |
|
47
|
|
|
return [ |
|
48
|
|
|
[ |
|
49
|
|
|
self::PERSISTED_QUERIES, |
|
50
|
|
|
self::CUSTOM_ENDPOINTS, |
|
51
|
|
|
], |
|
52
|
|
|
]; |
|
53
|
|
|
} |
|
54
|
|
|
return parent::getDependedModuleLists($module); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
// public function canBeDisabled(string $module): bool |
|
58
|
|
|
// { |
|
59
|
|
|
// switch ($module) { |
|
60
|
|
|
// case self::MAIN: |
|
61
|
|
|
// return false; |
|
62
|
|
|
// } |
|
63
|
|
|
// return parent::canBeDisabled($module); |
|
64
|
|
|
// } |
|
65
|
|
|
|
|
66
|
|
|
// public function isHidden(string $module): bool |
|
67
|
|
|
// { |
|
68
|
|
|
// switch ($module) { |
|
69
|
|
|
// case self::MAIN: |
|
70
|
|
|
// return true; |
|
71
|
|
|
// } |
|
72
|
|
|
// return parent::isHidden($module); |
|
73
|
|
|
// } |
|
74
|
|
|
|
|
75
|
|
|
public function getName(string $module): string |
|
76
|
|
|
{ |
|
77
|
|
|
$names = [ |
|
78
|
|
|
// self::MAIN => \__('Main', 'graphql-api'), |
|
79
|
|
|
self::SINGLE_ENDPOINT => \__('Single Endpoint', 'graphql-api'), |
|
80
|
|
|
self::PERSISTED_QUERIES => \__('Persisted Queries', 'graphql-api'), |
|
81
|
|
|
self::CUSTOM_ENDPOINTS => \__('Custom Endpoints', 'graphql-api'), |
|
82
|
|
|
self::API_HIERARCHY => \__('API Hierarchy', 'graphql-api'), |
|
83
|
|
|
]; |
|
84
|
|
|
return $names[$module] ?? $module; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
public function getDescription(string $module): string |
|
88
|
|
|
{ |
|
89
|
|
|
switch ($module) { |
|
90
|
|
|
// case self::MAIN: |
|
91
|
|
|
// return \__('Artificial module for defining the main settings', 'graphql-api'); |
|
92
|
|
|
case self::SINGLE_ENDPOINT: |
|
93
|
|
|
return \sprintf( |
|
94
|
|
|
\__('Expose a single GraphQL endpoint under <code>%s</code>, with unrestricted access', 'graphql-api'), |
|
95
|
|
|
GraphQLEndpointForWPComponentConfiguration::getGraphQLAPIEndpoint() |
|
96
|
|
|
); |
|
97
|
|
|
case self::PERSISTED_QUERIES: |
|
98
|
|
|
return \__('Expose predefined responses through a custom URL, akin to using GraphQL queries to publish REST endpoints', 'graphql-api'); |
|
99
|
|
|
case self::CUSTOM_ENDPOINTS: |
|
100
|
|
|
return \__('Expose different subsets of the schema for different targets, such as users (clients, employees, etc), applications (website, mobile app, etc), context (weekday, weekend, etc), and others', 'graphql-api'); |
|
101
|
|
|
case self::API_HIERARCHY: |
|
102
|
|
|
return \__('Create a hierarchy of API endpoints extending from other endpoints, and inheriting their properties', 'graphql-api'); |
|
103
|
|
|
} |
|
104
|
|
|
return parent::getDescription($module); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
public function isEnabledByDefault(string $module): bool |
|
108
|
|
|
{ |
|
109
|
|
|
switch ($module) { |
|
110
|
|
|
case self::SINGLE_ENDPOINT: |
|
111
|
|
|
return false; |
|
112
|
|
|
} |
|
113
|
|
|
return parent::isEnabledByDefault($module); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* Default value for an option set by the module |
|
118
|
|
|
* |
|
119
|
|
|
* @param string $module |
|
120
|
|
|
* @param string $option |
|
121
|
|
|
* @return mixed Anything the setting might be: an array|string|bool|int|null |
|
122
|
|
|
*/ |
|
123
|
|
|
public function getSettingsDefaultValue(string $module, string $option) |
|
124
|
|
|
{ |
|
125
|
|
|
$defaultValues = [ |
|
126
|
|
|
self::SINGLE_ENDPOINT => [ |
|
127
|
|
|
self::OPTION_PATH => '/graphql/', |
|
128
|
|
|
], |
|
129
|
|
|
self::CUSTOM_ENDPOINTS => [ |
|
130
|
|
|
self::OPTION_PATH => 'graphql', |
|
131
|
|
|
], |
|
132
|
|
|
self::PERSISTED_QUERIES => [ |
|
133
|
|
|
self::OPTION_PATH => 'graphql-query', |
|
134
|
|
|
], |
|
135
|
|
|
]; |
|
136
|
|
|
return $defaultValues[$module][$option]; |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
/** |
|
140
|
|
|
* Array with the inputs to show as settings for the module |
|
141
|
|
|
* |
|
142
|
|
|
* @param string $module |
|
143
|
|
|
* @return array |
|
144
|
|
|
*/ |
|
145
|
|
|
public function getSettings(string $module): array |
|
146
|
|
|
{ |
|
147
|
|
|
$moduleSettings = parent::getSettings($module); |
|
148
|
|
|
$moduleRegistry = ModuleRegistryFacade::getInstance(); |
|
|
|
|
|
|
149
|
|
|
// Do the if one by one, so that the SELECT do not get evaluated unless needed |
|
150
|
|
|
if ($module == self::SINGLE_ENDPOINT) { |
|
151
|
|
|
$option = self::OPTION_PATH; |
|
152
|
|
|
$moduleSettings[] = [ |
|
153
|
|
|
Properties::INPUT => $option, |
|
154
|
|
|
Properties::NAME => $this->getSettingOptionName( |
|
155
|
|
|
$module, |
|
156
|
|
|
$option |
|
157
|
|
|
), |
|
158
|
|
|
Properties::TITLE => \__('Endpoint path', 'graphql-api'), |
|
159
|
|
|
Properties::DESCRIPTION => \__('URL path to expose the single GraphQL endpoint', 'graphql-api'), |
|
160
|
|
|
Properties::TYPE => Properties::TYPE_STRING, |
|
161
|
|
|
]; |
|
162
|
|
|
} elseif ($module == self::CUSTOM_ENDPOINTS) { |
|
163
|
|
|
$option = self::OPTION_PATH; |
|
164
|
|
|
$moduleSettings[] = [ |
|
165
|
|
|
Properties::INPUT => $option, |
|
166
|
|
|
Properties::NAME => $this->getSettingOptionName( |
|
167
|
|
|
$module, |
|
168
|
|
|
$option |
|
169
|
|
|
), |
|
170
|
|
|
Properties::TITLE => \__('Base path', 'graphql-api'), |
|
171
|
|
|
Properties::DESCRIPTION => \__('URL base path to expose the Custom Endpoint', 'graphql-api'), |
|
172
|
|
|
Properties::TYPE => Properties::TYPE_STRING, |
|
173
|
|
|
]; |
|
174
|
|
|
} elseif ($module == self::PERSISTED_QUERIES) { |
|
175
|
|
|
$option = self::OPTION_PATH; |
|
176
|
|
|
$moduleSettings[] = [ |
|
177
|
|
|
Properties::INPUT => $option, |
|
178
|
|
|
Properties::NAME => $this->getSettingOptionName( |
|
179
|
|
|
$module, |
|
180
|
|
|
$option |
|
181
|
|
|
), |
|
182
|
|
|
Properties::TITLE => \__('Base path', 'graphql-api'), |
|
183
|
|
|
Properties::DESCRIPTION => \__('URL base path to expose the Persisted Query', 'graphql-api'), |
|
184
|
|
|
Properties::TYPE => Properties::TYPE_STRING, |
|
185
|
|
|
]; |
|
186
|
|
|
} |
|
187
|
|
|
return $moduleSettings; |
|
188
|
|
|
} |
|
189
|
|
|
} |
|
190
|
|
|
|