|
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 GraphQLAPI\GraphQLAPI\ModuleTypeResolvers\ModuleTypeResolver; |
|
12
|
|
|
use GraphQLByPoP\GraphQLClientsForWP\ComponentConfiguration as GraphQLClientsForWPComponentConfiguration; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Modules exposing clients to interact with the API |
|
16
|
|
|
* |
|
17
|
|
|
* @author Leonardo Losoviz <[email protected]> |
|
18
|
|
|
*/ |
|
19
|
|
|
class ClientFunctionalityModuleResolver extends AbstractFunctionalityModuleResolver |
|
20
|
|
|
{ |
|
21
|
|
|
use ModuleResolverTrait; |
|
22
|
|
|
|
|
23
|
|
|
public const GRAPHIQL_FOR_SINGLE_ENDPOINT = Plugin::NAMESPACE . '\graphiql-for-single-endpoint'; |
|
24
|
|
|
public const GRAPHIQL_FOR_CUSTOM_ENDPOINTS = Plugin::NAMESPACE . '\graphiql-for-custom-endpoints'; |
|
25
|
|
|
public const INTERACTIVE_SCHEMA_FOR_SINGLE_ENDPOINT = Plugin::NAMESPACE . '\interactive-schema-for-single-endpoint'; |
|
26
|
|
|
public const INTERACTIVE_SCHEMA_FOR_CUSTOM_ENDPOINTS = Plugin::NAMESPACE . '\interactive-schema-for-custom-endpoints'; |
|
27
|
|
|
public const GRAPHIQL_EXPLORER = Plugin::NAMESPACE . '\graphiql-explorer'; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Setting options |
|
31
|
|
|
*/ |
|
32
|
|
|
public const OPTION_USE_GRAPHIQL_EXPLORER_IN_ADMIN_CLIENT = 'graphiql-use-explorer-in-admin-client'; |
|
33
|
|
|
public const OPTION_USE_GRAPHIQL_EXPLORER_IN_ADMIN_PERSISTED_QUERIES = 'graphiql-use-explorer-in-admin-persisted-queries'; |
|
34
|
|
|
public const OPTION_USE_GRAPHIQL_EXPLORER_IN_PUBLIC_CLIENT_FOR_SINGLE_ENDPOINT = 'graphiql-use-explorer-in-public-client-for-single-endpoint'; |
|
35
|
|
|
public const OPTION_USE_GRAPHIQL_EXPLORER_IN_PUBLIC_CLIENT_FOR_CUSTOM_ENDPOINTS = 'graphiql-use-explorer-in-public-client-for-custom-endpoints'; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @return string[] |
|
39
|
|
|
*/ |
|
40
|
|
|
public static function getModulesToResolve(): array |
|
41
|
|
|
{ |
|
42
|
|
|
return [ |
|
43
|
|
|
self::GRAPHIQL_FOR_SINGLE_ENDPOINT, |
|
44
|
|
|
self::INTERACTIVE_SCHEMA_FOR_SINGLE_ENDPOINT, |
|
45
|
|
|
self::GRAPHIQL_FOR_CUSTOM_ENDPOINTS, |
|
46
|
|
|
self::INTERACTIVE_SCHEMA_FOR_CUSTOM_ENDPOINTS, |
|
47
|
|
|
self::GRAPHIQL_EXPLORER, |
|
48
|
|
|
]; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Enable to customize a specific UI for the module |
|
53
|
|
|
*/ |
|
54
|
|
|
public function getModuleType(string $module): string |
|
55
|
|
|
{ |
|
56
|
|
|
return ModuleTypeResolver::CLIENT; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @return array<array> List of entries that must be satisfied, each entry is an array where at least 1 module must be satisfied |
|
61
|
|
|
*/ |
|
62
|
|
|
public function getDependedModuleLists(string $module): array |
|
63
|
|
|
{ |
|
64
|
|
|
switch ($module) { |
|
65
|
|
|
case self::GRAPHIQL_FOR_SINGLE_ENDPOINT: |
|
66
|
|
|
case self::INTERACTIVE_SCHEMA_FOR_SINGLE_ENDPOINT: |
|
67
|
|
|
return [ |
|
68
|
|
|
[ |
|
69
|
|
|
EndpointFunctionalityModuleResolver::SINGLE_ENDPOINT, |
|
70
|
|
|
], |
|
71
|
|
|
]; |
|
72
|
|
|
case self::GRAPHIQL_FOR_CUSTOM_ENDPOINTS: |
|
73
|
|
|
case self::INTERACTIVE_SCHEMA_FOR_CUSTOM_ENDPOINTS: |
|
74
|
|
|
return [ |
|
75
|
|
|
[ |
|
76
|
|
|
EndpointFunctionalityModuleResolver::CUSTOM_ENDPOINTS, |
|
77
|
|
|
], |
|
78
|
|
|
]; |
|
79
|
|
|
case self::GRAPHIQL_EXPLORER: |
|
80
|
|
|
return []; |
|
81
|
|
|
} |
|
82
|
|
|
return parent::getDependedModuleLists($module); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
public function areRequirementsSatisfied(string $module): bool |
|
86
|
|
|
{ |
|
87
|
|
|
switch ($module) { |
|
88
|
|
|
case self::GRAPHIQL_FOR_SINGLE_ENDPOINT: |
|
89
|
|
|
case self::INTERACTIVE_SCHEMA_FOR_SINGLE_ENDPOINT: |
|
90
|
|
|
/** |
|
91
|
|
|
* Permalink structure must be enabled |
|
92
|
|
|
*/ |
|
93
|
|
|
return !empty(\get_option('permalink_structure')); |
|
94
|
|
|
} |
|
95
|
|
|
return parent::areRequirementsSatisfied($module); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
public function getName(string $module): string |
|
99
|
|
|
{ |
|
100
|
|
|
$names = [ |
|
101
|
|
|
self::GRAPHIQL_FOR_SINGLE_ENDPOINT => \__('GraphiQL for Single Endpoint', 'graphql-api'), |
|
102
|
|
|
self::GRAPHIQL_FOR_CUSTOM_ENDPOINTS => \__('GraphiQL for Custom Endpoints', 'graphql-api'), |
|
103
|
|
|
self::INTERACTIVE_SCHEMA_FOR_SINGLE_ENDPOINT => \__('Interactive Schema for Single Endpoint', 'graphql-api'), |
|
104
|
|
|
self::INTERACTIVE_SCHEMA_FOR_CUSTOM_ENDPOINTS => \__('Interactive Schema for Custom Endpoints', 'graphql-api'), |
|
105
|
|
|
self::GRAPHIQL_EXPLORER => \__('GraphiQL Explorer', 'graphql-api'), |
|
106
|
|
|
]; |
|
107
|
|
|
return $names[$module] ?? $module; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
public function getDescription(string $module): string |
|
111
|
|
|
{ |
|
112
|
|
|
switch ($module) { |
|
113
|
|
|
case self::GRAPHIQL_FOR_SINGLE_ENDPOINT: |
|
114
|
|
|
return \sprintf( |
|
115
|
|
|
\__('Make a public GraphiQL client available under <code>%s</code>, to execute queries against the single endpoint. It requires pretty permalinks enabled', 'graphql-api'), |
|
116
|
|
|
GraphQLClientsForWPComponentConfiguration::getGraphiQLClientEndpoint() |
|
117
|
|
|
); |
|
118
|
|
|
case self::GRAPHIQL_FOR_CUSTOM_ENDPOINTS: |
|
119
|
|
|
return \__('Enable custom endpoints to be attached their own GraphiQL client, to execute queries against them', 'graphql-api'); |
|
120
|
|
|
case self::INTERACTIVE_SCHEMA_FOR_SINGLE_ENDPOINT: |
|
121
|
|
|
return \sprintf( |
|
122
|
|
|
\__('Make a public Interactive Schema client available under <code>%s</code>, to visualize the schema accessible through the single endpoint. It requires pretty permalinks enabled', 'graphql-api'), |
|
123
|
|
|
GraphQLClientsForWPComponentConfiguration::getVoyagerClientEndpoint() |
|
124
|
|
|
); |
|
125
|
|
|
case self::INTERACTIVE_SCHEMA_FOR_CUSTOM_ENDPOINTS: |
|
126
|
|
|
return \__('Enable custom endpoints to be attached their own Interactive schema client, to visualize the custom schema subset', 'graphql-api'); |
|
127
|
|
|
case self::GRAPHIQL_EXPLORER: |
|
128
|
|
|
return \__('Add the Explorer widget to the GraphiQL client, to simplify coding the query (by point-and-clicking on the fields)', 'graphql-api'); |
|
129
|
|
|
} |
|
130
|
|
|
return parent::getDescription($module); |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* Default value for an option set by the module |
|
135
|
|
|
* |
|
136
|
|
|
* @param string $module |
|
137
|
|
|
* @param string $option |
|
138
|
|
|
* @return mixed Anything the setting might be: an array|string|bool|int|null |
|
139
|
|
|
*/ |
|
140
|
|
|
public function getSettingsDefaultValue(string $module, string $option) |
|
141
|
|
|
{ |
|
142
|
|
|
$defaultValues = [ |
|
143
|
|
|
self::GRAPHIQL_FOR_SINGLE_ENDPOINT => [ |
|
144
|
|
|
EndpointFunctionalityModuleResolver::OPTION_PATH => '/graphiql/', |
|
145
|
|
|
], |
|
146
|
|
|
self::INTERACTIVE_SCHEMA_FOR_SINGLE_ENDPOINT => [ |
|
147
|
|
|
EndpointFunctionalityModuleResolver::OPTION_PATH => '/schema/', |
|
148
|
|
|
], |
|
149
|
|
|
self::GRAPHIQL_EXPLORER => [ |
|
150
|
|
|
self::OPTION_USE_GRAPHIQL_EXPLORER_IN_ADMIN_CLIENT => true, |
|
151
|
|
|
self::OPTION_USE_GRAPHIQL_EXPLORER_IN_ADMIN_PERSISTED_QUERIES => true, |
|
152
|
|
|
self::OPTION_USE_GRAPHIQL_EXPLORER_IN_PUBLIC_CLIENT_FOR_SINGLE_ENDPOINT => true, |
|
153
|
|
|
self::OPTION_USE_GRAPHIQL_EXPLORER_IN_PUBLIC_CLIENT_FOR_CUSTOM_ENDPOINTS => true, |
|
154
|
|
|
], |
|
155
|
|
|
]; |
|
156
|
|
|
return $defaultValues[$module][$option]; |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
/** |
|
160
|
|
|
* Array with the inputs to show as settings for the module |
|
161
|
|
|
* |
|
162
|
|
|
* @return array<array> List of settings for the module, each entry is an array with property => value |
|
163
|
|
|
*/ |
|
164
|
|
|
public function getSettings(string $module): array |
|
165
|
|
|
{ |
|
166
|
|
|
$moduleSettings = parent::getSettings($module); |
|
167
|
|
|
$moduleRegistry = ModuleRegistryFacade::getInstance(); |
|
168
|
|
|
// Do the if one by one, so that the SELECT do not get evaluated unless needed |
|
169
|
|
|
if ($module == self::GRAPHIQL_FOR_SINGLE_ENDPOINT) { |
|
170
|
|
|
$option = EndpointFunctionalityModuleResolver::OPTION_PATH; |
|
171
|
|
|
$moduleSettings[] = [ |
|
172
|
|
|
Properties::INPUT => $option, |
|
173
|
|
|
Properties::NAME => $this->getSettingOptionName( |
|
174
|
|
|
$module, |
|
175
|
|
|
$option |
|
176
|
|
|
), |
|
177
|
|
|
Properties::TITLE => \__('Client path', 'graphql-api'), |
|
178
|
|
|
Properties::DESCRIPTION => \__('URL path to access the public GraphiQL client', 'graphql-api'), |
|
179
|
|
|
Properties::TYPE => Properties::TYPE_STRING, |
|
180
|
|
|
]; |
|
181
|
|
|
} elseif ($module == self::INTERACTIVE_SCHEMA_FOR_SINGLE_ENDPOINT) { |
|
182
|
|
|
$option = EndpointFunctionalityModuleResolver::OPTION_PATH; |
|
183
|
|
|
$moduleSettings[] = [ |
|
184
|
|
|
Properties::INPUT => $option, |
|
185
|
|
|
Properties::NAME => $this->getSettingOptionName( |
|
186
|
|
|
$module, |
|
187
|
|
|
$option |
|
188
|
|
|
), |
|
189
|
|
|
Properties::TITLE => \__('Client path', 'graphql-api'), |
|
190
|
|
|
Properties::DESCRIPTION => \__('URL path to access the public Interactive Schema client', 'graphql-api'), |
|
191
|
|
|
Properties::TYPE => Properties::TYPE_STRING, |
|
192
|
|
|
]; |
|
193
|
|
|
} elseif ($module == self::GRAPHIQL_EXPLORER) { |
|
194
|
|
|
$option = self::OPTION_USE_GRAPHIQL_EXPLORER_IN_ADMIN_CLIENT; |
|
195
|
|
|
$moduleSettings[] = [ |
|
196
|
|
|
Properties::INPUT => $option, |
|
197
|
|
|
Properties::NAME => $this->getSettingOptionName( |
|
198
|
|
|
$module, |
|
199
|
|
|
$option |
|
200
|
|
|
), |
|
201
|
|
|
Properties::TITLE => \__('Admin client', 'graphql-api'), |
|
202
|
|
|
Properties::DESCRIPTION => \__('Use the Explorer in the GraphiQL client in the admin area?', 'graphql-api'), |
|
203
|
|
|
Properties::TYPE => Properties::TYPE_BOOL, |
|
204
|
|
|
]; |
|
205
|
|
|
if ($moduleRegistry->isModuleEnabled(EndpointFunctionalityModuleResolver::PERSISTED_QUERIES)) { |
|
206
|
|
|
$option = self::OPTION_USE_GRAPHIQL_EXPLORER_IN_ADMIN_PERSISTED_QUERIES; |
|
207
|
|
|
$moduleSettings[] = [ |
|
208
|
|
|
Properties::INPUT => $option, |
|
209
|
|
|
Properties::NAME => $this->getSettingOptionName( |
|
210
|
|
|
$module, |
|
211
|
|
|
$option |
|
212
|
|
|
), |
|
213
|
|
|
Properties::TITLE => \__('Persisted queries', 'graphql-api'), |
|
214
|
|
|
Properties::DESCRIPTION => \__('Use the Explorer when creating persisted queries in the admin area?', 'graphql-api'), |
|
215
|
|
|
Properties::TYPE => Properties::TYPE_BOOL, |
|
216
|
|
|
]; |
|
217
|
|
|
} |
|
218
|
|
|
if ($moduleRegistry->isModuleEnabled(self::GRAPHIQL_FOR_SINGLE_ENDPOINT)) { |
|
219
|
|
|
$option = self::OPTION_USE_GRAPHIQL_EXPLORER_IN_PUBLIC_CLIENT_FOR_SINGLE_ENDPOINT; |
|
220
|
|
|
$moduleSettings[] = [ |
|
221
|
|
|
Properties::INPUT => $option, |
|
222
|
|
|
Properties::NAME => $this->getSettingOptionName( |
|
223
|
|
|
$module, |
|
224
|
|
|
$option |
|
225
|
|
|
), |
|
226
|
|
|
Properties::TITLE => \__('Single endpoint public client', 'graphql-api'), |
|
227
|
|
|
Properties::DESCRIPTION => \__('Use the Explorer in the single endpoint\'s public GraphiQL client?', 'graphql-api'), |
|
228
|
|
|
Properties::TYPE => Properties::TYPE_BOOL, |
|
229
|
|
|
]; |
|
230
|
|
|
} |
|
231
|
|
|
if ($moduleRegistry->isModuleEnabled(self::GRAPHIQL_FOR_CUSTOM_ENDPOINTS)) { |
|
232
|
|
|
$option = self::OPTION_USE_GRAPHIQL_EXPLORER_IN_PUBLIC_CLIENT_FOR_CUSTOM_ENDPOINTS; |
|
233
|
|
|
$moduleSettings[] = [ |
|
234
|
|
|
Properties::INPUT => $option, |
|
235
|
|
|
Properties::NAME => $this->getSettingOptionName( |
|
236
|
|
|
$module, |
|
237
|
|
|
$option |
|
238
|
|
|
), |
|
239
|
|
|
Properties::TITLE => \__('Custom endpoint public clients', 'graphql-api'), |
|
240
|
|
|
Properties::DESCRIPTION => \__('Use the Explorer in the custom endpoints\' public GraphiQL client?', 'graphql-api'), |
|
241
|
|
|
Properties::TYPE => Properties::TYPE_BOOL, |
|
242
|
|
|
]; |
|
243
|
|
|
} |
|
244
|
|
|
} |
|
245
|
|
|
return $moduleSettings; |
|
246
|
|
|
} |
|
247
|
|
|
} |
|
248
|
|
|
|