1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace GraphQLAPI\GraphQLAPI\SchemaConfigurators; |
6
|
|
|
|
7
|
|
|
use WP_Post; |
8
|
|
|
use PoP\AccessControl\Schema\SchemaModes; |
9
|
|
|
use GraphQLAPI\GraphQLAPI\General\BlockHelpers; |
10
|
|
|
use PoP\Engine\Environment as EngineEnvironment; |
11
|
|
|
use GraphQLAPI\GraphQLAPI\Facades\ModuleRegistryFacade; |
12
|
|
|
use GraphQLAPI\GraphQLAPI\Blocks\SchemaConfigOptionsBlock; |
13
|
|
|
use GraphQLAPI\GraphQLAPI\Blocks\SchemaConfigurationBlock; |
14
|
|
|
use GraphQLAPI\GraphQLAPI\Facades\UserSettingsManagerFacade; |
15
|
|
|
use GraphQLByPoP\GraphQLServer\Configuration\MutationSchemes; |
16
|
|
|
use PoP\AccessControl\Environment as AccessControlEnvironment; |
17
|
|
|
use PoP\ComponentModel\Facades\Instances\InstanceManagerFacade; |
18
|
|
|
use PoP\ComponentModel\Environment as ComponentModelEnvironment; |
19
|
|
|
use GraphQLAPI\GraphQLAPI\Blocks\SchemaConfigAccessControlListBlock; |
20
|
|
|
use PoP\Engine\ComponentConfiguration as EngineComponentConfiguration; |
21
|
|
|
use GraphQLAPI\GraphQLAPI\Blocks\SchemaConfigFieldDeprecationListBlock; |
22
|
|
|
use GraphQLByPoP\GraphQLServer\Environment as GraphQLServerEnvironment; |
23
|
|
|
use PoP\ComponentModel\ComponentConfiguration\ComponentConfigurationHelpers; |
24
|
|
|
use GraphQLAPI\GraphQLAPI\ModuleResolvers\EndpointFunctionalityModuleResolver; |
25
|
|
|
use GraphQLAPI\GraphQLAPI\ModuleResolvers\VersioningFunctionalityModuleResolver; |
26
|
|
|
use GraphQLAPI\GraphQLAPI\ModuleResolvers\OperationalFunctionalityModuleResolver; |
27
|
|
|
use GraphQLAPI\GraphQLAPI\ModuleResolvers\AccessControlFunctionalityModuleResolver; |
28
|
|
|
use GraphQLAPI\GraphQLAPI\SchemaConfigurators\AccessControlGraphQLQueryConfigurator; |
29
|
|
|
use PoP\AccessControl\ComponentConfiguration as AccessControlComponentConfiguration; |
30
|
|
|
use PoP\ComponentModel\ComponentConfiguration as ComponentModelComponentConfiguration; |
31
|
|
|
use GraphQLAPI\GraphQLAPI\SchemaConfigurators\FieldDeprecationGraphQLQueryConfigurator; |
32
|
|
|
use GraphQLAPI\GraphQLAPI\ModuleResolvers\SchemaConfigurationFunctionalityModuleResolver; |
33
|
|
|
use GraphQLByPoP\GraphQLServer\ComponentConfiguration as GraphQLServerComponentConfiguration; |
34
|
|
|
|
35
|
|
|
abstract class AbstractQueryExecutionSchemaConfigurator implements SchemaConfiguratorInterface |
36
|
|
|
{ |
37
|
|
|
/** |
38
|
|
|
* Extract the items defined in the Schema Configuration, |
39
|
|
|
* and inject them into the service as to take effect in the current GraphQL query |
40
|
|
|
*/ |
41
|
|
|
public function executeSchemaConfiguration(int $customPostID): void |
42
|
|
|
{ |
43
|
|
|
// Check if it enabled by module |
44
|
|
|
$moduleRegistry = ModuleRegistryFacade::getInstance(); |
45
|
|
|
if (!$moduleRegistry->isModuleEnabled(SchemaConfigurationFunctionalityModuleResolver::SCHEMA_CONFIGURATION)) { |
46
|
|
|
return; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
if ($schemaConfigurationID = $this->getSchemaConfigurationID($customPostID)) { |
50
|
|
|
// Get that Schema Configuration, and load its settings |
51
|
|
|
$this->executeSchemaConfigurationItems($schemaConfigurationID); |
52
|
|
|
} |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Return the stored Schema Configuration ID |
57
|
|
|
*/ |
58
|
|
|
protected function getUserSettingSchemaConfigurationID(): ?int |
59
|
|
|
{ |
60
|
|
|
$userSettingsManager = UserSettingsManagerFacade::getInstance(); |
61
|
|
|
$schemaConfigurationID = $userSettingsManager->getSetting( |
62
|
|
|
SchemaConfigurationFunctionalityModuleResolver::SCHEMA_CONFIGURATION, |
63
|
|
|
SchemaConfigurationFunctionalityModuleResolver::OPTION_SCHEMA_CONFIGURATION_ID |
64
|
|
|
); |
65
|
|
|
// `null` is stored as OPTION_VALUE_NO_VALUE_ID |
66
|
|
|
if ($schemaConfigurationID == SchemaConfigurationFunctionalityModuleResolver::OPTION_VALUE_NO_VALUE_ID) { |
67
|
|
|
return null; |
68
|
|
|
} |
69
|
|
|
return $schemaConfigurationID; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Extract the Schema Configuration ID from the block stored in the post |
74
|
|
|
*/ |
75
|
|
|
protected function getSchemaConfigurationID(int $customPostID): ?int |
76
|
|
|
{ |
77
|
|
|
$instanceManager = InstanceManagerFacade::getInstance(); |
78
|
|
|
/** |
79
|
|
|
* @var SchemaConfigurationBlock |
80
|
|
|
*/ |
81
|
|
|
$block = $instanceManager->getInstance(SchemaConfigurationBlock::class); |
82
|
|
|
$schemaConfigurationBlockDataItem = BlockHelpers::getSingleBlockOfTypeFromCustomPost( |
83
|
|
|
$customPostID, |
84
|
|
|
$block |
85
|
|
|
); |
86
|
|
|
// If there was no schema configuration, then the default one has been selected |
87
|
|
|
// It is not saved in the DB, because it has been set as the default value in |
88
|
|
|
// blocks/schema-configuration/src/index.js |
89
|
|
|
if (is_null($schemaConfigurationBlockDataItem)) { |
90
|
|
|
return $this->getUserSettingSchemaConfigurationID(); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
$schemaConfiguration = $schemaConfigurationBlockDataItem['attrs'][SchemaConfigurationBlock::ATTRIBUTE_NAME_SCHEMA_CONFIGURATION] ?? null; |
94
|
|
|
// Check if $schemaConfiguration is one of the meta options (default, none, inherit) |
95
|
|
|
if ($schemaConfiguration == SchemaConfigurationBlock::ATTRIBUTE_VALUE_SCHEMA_CONFIGURATION_NONE) { |
96
|
|
|
return null; |
97
|
|
|
} elseif ($schemaConfiguration == SchemaConfigurationBlock::ATTRIBUTE_VALUE_SCHEMA_CONFIGURATION_DEFAULT) { |
98
|
|
|
return $this->getUserSettingSchemaConfigurationID(); |
99
|
|
|
} elseif ($schemaConfiguration == SchemaConfigurationBlock::ATTRIBUTE_VALUE_SCHEMA_CONFIGURATION_INHERIT) { |
100
|
|
|
// If disabled by module, then return nothing |
101
|
|
|
$moduleRegistry = ModuleRegistryFacade::getInstance(); |
102
|
|
|
if (!$moduleRegistry->isModuleEnabled(EndpointFunctionalityModuleResolver::API_HIERARCHY)) { |
103
|
|
|
return null; |
104
|
|
|
} |
105
|
|
|
// Return the schema configuration from the parent, or null if no parent exists |
106
|
|
|
/** |
107
|
|
|
* @var WP_Post|null |
108
|
|
|
*/ |
109
|
|
|
$customPost = \get_post($customPostID); |
110
|
|
|
if (!is_null($customPost) && $customPost->post_parent) { |
111
|
|
|
return $this->getSchemaConfigurationID($customPost->post_parent); |
112
|
|
|
} |
113
|
|
|
return null; |
114
|
|
|
} |
115
|
|
|
// It is already the ID, or null if blocks returned empty |
116
|
|
|
// (eg: because parent post was trashed) |
117
|
|
|
return $schemaConfiguration; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Apply all the settings defined in the Schema Configuration: |
122
|
|
|
* - Options |
123
|
|
|
* - Access Control Lists |
124
|
|
|
* - Field Deprecation Lists |
125
|
|
|
*/ |
126
|
|
|
protected function executeSchemaConfigurationItems(int $schemaConfigurationID): void |
127
|
|
|
{ |
128
|
|
|
$this->executeSchemaConfigurationOptions($schemaConfigurationID); |
129
|
|
|
$this->executeSchemaConfigurationAccessControlLists($schemaConfigurationID); |
130
|
|
|
$this->executeSchemaConfigurationFieldDeprecationLists($schemaConfigurationID); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Apply all the settings defined in the Schema Configuration for: |
135
|
|
|
* - Namespacing |
136
|
|
|
* - Mutation Scheme |
137
|
|
|
* - Options |
138
|
|
|
*/ |
139
|
|
|
protected function executeSchemaConfigurationOptions(int $schemaConfigurationID): void |
140
|
|
|
{ |
141
|
|
|
$this->executeSchemaConfigurationOptionsNamespacing($schemaConfigurationID); |
142
|
|
|
$this->executeSchemaConfigurationOptionsMutationScheme($schemaConfigurationID); |
143
|
|
|
$this->executeSchemaConfigurationOptionsDefaultSchemaMode($schemaConfigurationID); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* Apply the Namespacing settings |
148
|
|
|
*/ |
149
|
|
|
protected function executeSchemaConfigurationOptionsNamespacing(int $schemaConfigurationID): void |
150
|
|
|
{ |
151
|
|
|
// Check if it enabled by module |
152
|
|
|
$moduleRegistry = ModuleRegistryFacade::getInstance(); |
153
|
|
|
if (!$moduleRegistry->isModuleEnabled(SchemaConfigurationFunctionalityModuleResolver::SCHEMA_NAMESPACING)) { |
154
|
|
|
return; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
$instanceManager = InstanceManagerFacade::getInstance(); |
158
|
|
|
/** |
159
|
|
|
* @var SchemaConfigOptionsBlock |
160
|
|
|
*/ |
161
|
|
|
$block = $instanceManager->getInstance(SchemaConfigOptionsBlock::class); |
162
|
|
|
$schemaConfigOptionsBlockDataItem = BlockHelpers::getSingleBlockOfTypeFromCustomPost( |
163
|
|
|
$schemaConfigurationID, |
164
|
|
|
$block |
165
|
|
|
); |
166
|
|
|
if (!is_null($schemaConfigOptionsBlockDataItem)) { |
167
|
|
|
/** |
168
|
|
|
* Default value (if not defined in DB): `default`. Then do nothing |
169
|
|
|
*/ |
170
|
|
|
$useNamespacing = $schemaConfigOptionsBlockDataItem['attrs'][SchemaConfigOptionsBlock::ATTRIBUTE_NAME_USE_NAMESPACING] ?? null; |
171
|
|
|
// Only execute if it has value "enabled" or "disabled". |
172
|
|
|
// If "default", then the general settings will already take effect, so do nothing |
173
|
|
|
// (And if any other unsupported value, also do nothing) |
174
|
|
|
if ( |
175
|
|
|
!in_array($useNamespacing, [ |
176
|
|
|
SchemaConfigOptionsBlock::ATTRIBUTE_VALUE_USE_NAMESPACING_ENABLED, |
177
|
|
|
SchemaConfigOptionsBlock::ATTRIBUTE_VALUE_USE_NAMESPACING_DISABLED, |
178
|
|
|
]) |
179
|
|
|
) { |
180
|
|
|
return; |
181
|
|
|
} |
182
|
|
|
// Define the settings value through a hook. Execute last so it overrides the default settings |
183
|
|
|
$hookName = ComponentConfigurationHelpers::getHookName( |
184
|
|
|
ComponentModelComponentConfiguration::class, |
185
|
|
|
ComponentModelEnvironment::NAMESPACE_TYPES_AND_INTERFACES |
186
|
|
|
); |
187
|
|
|
\add_filter( |
188
|
|
|
$hookName, |
189
|
|
|
fn () => $useNamespacing == SchemaConfigOptionsBlock::ATTRIBUTE_VALUE_USE_NAMESPACING_ENABLED, |
190
|
|
|
PHP_INT_MAX |
191
|
|
|
); |
192
|
|
|
} |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* Apply the Mutation Scheme settings |
197
|
|
|
*/ |
198
|
|
|
protected function executeSchemaConfigurationOptionsMutationScheme(int $schemaConfigurationID): void |
199
|
|
|
{ |
200
|
|
|
// Check if it enabled by module |
201
|
|
|
$moduleRegistry = ModuleRegistryFacade::getInstance(); |
202
|
|
|
if (!$moduleRegistry->isModuleEnabled(OperationalFunctionalityModuleResolver::NESTED_MUTATIONS)) { |
203
|
|
|
return; |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
$instanceManager = InstanceManagerFacade::getInstance(); |
207
|
|
|
/** |
208
|
|
|
* @var SchemaConfigOptionsBlock |
209
|
|
|
*/ |
210
|
|
|
$block = $instanceManager->getInstance(SchemaConfigOptionsBlock::class); |
211
|
|
|
$schemaConfigOptionsBlockDataItem = BlockHelpers::getSingleBlockOfTypeFromCustomPost( |
212
|
|
|
$schemaConfigurationID, |
213
|
|
|
$block |
214
|
|
|
); |
215
|
|
|
if (!is_null($schemaConfigOptionsBlockDataItem)) { |
216
|
|
|
/** |
217
|
|
|
* Default value (if not defined in DB): `default`. Then do nothing |
218
|
|
|
*/ |
219
|
|
|
$mutationScheme = $schemaConfigOptionsBlockDataItem['attrs'][SchemaConfigOptionsBlock::ATTRIBUTE_NAME_MUTATION_SCHEME] ?? null; |
220
|
|
|
// Only execute if it has value "standard", "nested" or "lean_nested". |
221
|
|
|
// If "default", then the general settings will already take effect, so do nothing |
222
|
|
|
// (And if any other unsupported value, also do nothing) |
223
|
|
|
if ( |
224
|
|
|
!in_array($mutationScheme, [ |
225
|
|
|
MutationSchemes::STANDARD, |
226
|
|
|
MutationSchemes::NESTED_WITH_REDUNDANT_ROOT_FIELDS, |
227
|
|
|
MutationSchemes::NESTED_WITHOUT_REDUNDANT_ROOT_FIELDS, |
228
|
|
|
]) |
229
|
|
|
) { |
230
|
|
|
return; |
231
|
|
|
} |
232
|
|
|
// Define the settings value through a hook. Execute last so it overrides the default settings |
233
|
|
|
$hookName = ComponentConfigurationHelpers::getHookName( |
234
|
|
|
GraphQLServerComponentConfiguration::class, |
235
|
|
|
GraphQLServerEnvironment::ENABLE_NESTED_MUTATIONS |
236
|
|
|
); |
237
|
|
|
\add_filter( |
238
|
|
|
$hookName, |
239
|
|
|
fn () => $mutationScheme != MutationSchemes::STANDARD, |
240
|
|
|
PHP_INT_MAX |
241
|
|
|
); |
242
|
|
|
$hookName = ComponentConfigurationHelpers::getHookName( |
243
|
|
|
EngineComponentConfiguration::class, |
244
|
|
|
EngineEnvironment::DISABLE_REDUNDANT_ROOT_TYPE_MUTATION_FIELDS |
245
|
|
|
); |
246
|
|
|
\add_filter( |
247
|
|
|
$hookName, |
248
|
|
|
fn () => $mutationScheme == MutationSchemes::NESTED_WITHOUT_REDUNDANT_ROOT_FIELDS, |
249
|
|
|
PHP_INT_MAX |
250
|
|
|
); |
251
|
|
|
} |
252
|
|
|
} |
253
|
|
|
|
254
|
|
|
/** |
255
|
|
|
* Apply the default Schema mode settings |
256
|
|
|
*/ |
257
|
|
|
protected function executeSchemaConfigurationOptionsDefaultSchemaMode(int $schemaConfigurationID): void |
258
|
|
|
{ |
259
|
|
|
// Check if it enabled by module |
260
|
|
|
$moduleRegistry = ModuleRegistryFacade::getInstance(); |
261
|
|
|
if (!$moduleRegistry->isModuleEnabled(SchemaConfigurationFunctionalityModuleResolver::PUBLIC_PRIVATE_SCHEMA)) { |
262
|
|
|
return; |
263
|
|
|
} |
264
|
|
|
|
265
|
|
|
$instanceManager = InstanceManagerFacade::getInstance(); |
266
|
|
|
/** |
267
|
|
|
* @var SchemaConfigOptionsBlock |
268
|
|
|
*/ |
269
|
|
|
$block = $instanceManager->getInstance(SchemaConfigOptionsBlock::class); |
270
|
|
|
$schemaConfigOptionsBlockDataItem = BlockHelpers::getSingleBlockOfTypeFromCustomPost( |
271
|
|
|
$schemaConfigurationID, |
272
|
|
|
$block |
273
|
|
|
); |
274
|
|
|
if (!is_null($schemaConfigOptionsBlockDataItem)) { |
275
|
|
|
/** |
276
|
|
|
* Default value (if not defined in DB): `default`. Then do nothing |
277
|
|
|
*/ |
278
|
|
|
$defaultSchemaMode = $schemaConfigOptionsBlockDataItem['attrs'][SchemaConfigOptionsBlock::ATTRIBUTE_NAME_DEFAULT_SCHEMA_MODE] ?? null; |
279
|
|
|
// Only execute if it has value "public" or "private". |
280
|
|
|
// If "default", then the general settings will already take effect, so do nothing |
281
|
|
|
// (And if any other unsupported value, also do nothing) |
282
|
|
|
if ( |
283
|
|
|
!in_array($defaultSchemaMode, [ |
284
|
|
|
SchemaModes::PUBLIC_SCHEMA_MODE, |
285
|
|
|
SchemaModes::PRIVATE_SCHEMA_MODE, |
286
|
|
|
]) |
287
|
|
|
) { |
288
|
|
|
return; |
289
|
|
|
} |
290
|
|
|
// Define the settings value through a hook. Execute last so it overrides the default settings |
291
|
|
|
$hookName = ComponentConfigurationHelpers::getHookName( |
292
|
|
|
AccessControlComponentConfiguration::class, |
293
|
|
|
AccessControlEnvironment::USE_PRIVATE_SCHEMA_MODE |
294
|
|
|
); |
295
|
|
|
\add_filter( |
296
|
|
|
$hookName, |
297
|
|
|
fn () => $defaultSchemaMode == SchemaModes::PRIVATE_SCHEMA_MODE, |
298
|
|
|
PHP_INT_MAX |
299
|
|
|
); |
300
|
|
|
} |
301
|
|
|
} |
302
|
|
|
|
303
|
|
|
/** |
304
|
|
|
* Apply all the settings defined in the Schema Configuration for: |
305
|
|
|
* - Access Control Lists |
306
|
|
|
*/ |
307
|
|
|
protected function executeSchemaConfigurationAccessControlLists(int $schemaConfigurationID): void |
308
|
|
|
{ |
309
|
|
|
// Check it is enabled by module |
310
|
|
|
$moduleRegistry = ModuleRegistryFacade::getInstance(); |
311
|
|
|
if (!$moduleRegistry->isModuleEnabled(AccessControlFunctionalityModuleResolver::ACCESS_CONTROL)) { |
312
|
|
|
return; |
313
|
|
|
} |
314
|
|
|
$instanceManager = InstanceManagerFacade::getInstance(); |
315
|
|
|
/** |
316
|
|
|
* @var SchemaConfigAccessControlListBlock |
317
|
|
|
*/ |
318
|
|
|
$block = $instanceManager->getInstance(SchemaConfigAccessControlListBlock::class); |
319
|
|
|
$schemaConfigACLBlockDataItem = BlockHelpers::getSingleBlockOfTypeFromCustomPost( |
320
|
|
|
$schemaConfigurationID, |
321
|
|
|
$block |
322
|
|
|
); |
323
|
|
|
if (!is_null($schemaConfigACLBlockDataItem)) { |
324
|
|
|
if ($accessControlLists = $schemaConfigACLBlockDataItem['attrs'][SchemaConfigAccessControlListBlock::ATTRIBUTE_NAME_ACCESS_CONTROL_LISTS] ?? null) { |
325
|
|
|
$configurator = new AccessControlGraphQLQueryConfigurator(); |
326
|
|
|
foreach ($accessControlLists as $accessControlListID) { |
327
|
|
|
$configurator->executeSchemaConfiguration($accessControlListID); |
328
|
|
|
} |
329
|
|
|
} |
330
|
|
|
} |
331
|
|
|
} |
332
|
|
|
|
333
|
|
|
/** |
334
|
|
|
* Apply all the settings defined in the Schema Configuration for: |
335
|
|
|
* - Field Deprecation Lists |
336
|
|
|
*/ |
337
|
|
|
protected function executeSchemaConfigurationFieldDeprecationLists(int $schemaConfigurationID): void |
338
|
|
|
{ |
339
|
|
|
// Check it is enabled by module |
340
|
|
|
$moduleRegistry = ModuleRegistryFacade::getInstance(); |
341
|
|
|
if (!$moduleRegistry->isModuleEnabled(VersioningFunctionalityModuleResolver::FIELD_DEPRECATION)) { |
342
|
|
|
return; |
343
|
|
|
} |
344
|
|
|
$instanceManager = InstanceManagerFacade::getInstance(); |
345
|
|
|
/** |
346
|
|
|
* @var SchemaConfigFieldDeprecationListBlock |
347
|
|
|
*/ |
348
|
|
|
$block = $instanceManager->getInstance(SchemaConfigFieldDeprecationListBlock::class); |
349
|
|
|
$schemaConfigFDLBlockDataItem = BlockHelpers::getSingleBlockOfTypeFromCustomPost( |
350
|
|
|
$schemaConfigurationID, |
351
|
|
|
$block |
352
|
|
|
); |
353
|
|
|
if (!is_null($schemaConfigFDLBlockDataItem)) { |
354
|
|
|
if ($fieldDeprecationLists = $schemaConfigFDLBlockDataItem['attrs'][SchemaConfigFieldDeprecationListBlock::ATTRIBUTE_NAME_FIELD_DEPRECATION_LISTS] ?? null) { |
355
|
|
|
$configurator = new FieldDeprecationGraphQLQueryConfigurator(); |
356
|
|
|
foreach ($fieldDeprecationLists as $fieldDeprecationListID) { |
357
|
|
|
$configurator->executeSchemaConfiguration($fieldDeprecationListID); |
358
|
|
|
} |
359
|
|
|
} |
360
|
|
|
} |
361
|
|
|
} |
362
|
|
|
} |
363
|
|
|
|