1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace GraphQLAPI\GraphQLAPI\SchemaConfigurators; |
6
|
|
|
|
7
|
|
|
use PoP\Hooks\Facades\HooksAPIFacade; |
8
|
|
|
use PoP\ComponentModel\Schema\HookHelpers; |
9
|
|
|
use GraphQLAPI\GraphQLAPI\General\BlockHelpers; |
10
|
|
|
use PoP\ComponentModel\Schema\SchemaDefinition; |
11
|
|
|
use GraphQLAPI\GraphQLAPI\Blocks\AbstractControlBlock; |
12
|
|
|
use GraphQLAPI\GraphQLAPI\Blocks\FieldDeprecationBlock; |
13
|
|
|
use GraphQLAPI\GraphQLAPI\Facades\ModuleRegistryFacade; |
14
|
|
|
use PoP\ComponentModel\Facades\Instances\InstanceManagerFacade; |
15
|
|
|
use GraphQLAPI\GraphQLAPI\SchemaConfigurators\AbstractGraphQLQueryConfigurator; |
16
|
|
|
use GraphQLAPI\GraphQLAPI\ModuleResolvers\VersioningFunctionalityModuleResolver; |
17
|
|
|
|
18
|
|
|
class FieldDeprecationGraphQLQueryConfigurator extends AbstractGraphQLQueryConfigurator |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* Extract the configuration items defined in the CPT, |
22
|
|
|
* and inject them into the service as to take effect in the current GraphQL query |
23
|
|
|
* |
24
|
|
|
* @return void |
25
|
|
|
*/ |
26
|
|
|
public function executeSchemaConfiguration(int $fdlPostID): void |
27
|
|
|
{ |
28
|
|
|
// Only if the module is not disabled |
29
|
|
|
$moduleRegistry = ModuleRegistryFacade::getInstance(); |
30
|
|
|
if (!$moduleRegistry->isModuleEnabled(VersioningFunctionalityModuleResolver::FIELD_DEPRECATION)) { |
31
|
|
|
return; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
$instanceManager = InstanceManagerFacade::getInstance(); |
35
|
|
|
/** |
36
|
|
|
* @var FieldDeprecationBlock |
37
|
|
|
*/ |
38
|
|
|
$block = $instanceManager->getInstance(FieldDeprecationBlock::class); |
39
|
|
|
$fdlBlockItems = BlockHelpers::getBlocksOfTypeFromCustomPost( |
40
|
|
|
$fdlPostID, |
41
|
|
|
$block |
42
|
|
|
); |
43
|
|
|
$hooksAPI = HooksAPIFacade::getInstance(); |
44
|
|
|
foreach ($fdlBlockItems as $fdlBlockItem) { |
45
|
|
|
if ($deprecationReason = $fdlBlockItem['attrs'][FieldDeprecationBlock::ATTRIBUTE_NAME_DEPRECATION_REASON] ?? null) { |
46
|
|
|
if ($typeFields = $fdlBlockItem['attrs'][AbstractControlBlock::ATTRIBUTE_NAME_TYPE_FIELDS] ?? null) { |
47
|
|
|
// Add a hook to override the schema for the selected fields |
48
|
|
|
foreach ($typeFields as $selectedField) { |
49
|
|
|
$entriesFromField = $this->getEntriesFromField($selectedField, $deprecationReason); |
50
|
|
|
foreach ($entriesFromField as $entry) { |
51
|
|
|
// Once getting the entry, we an obtain the type and field, |
52
|
|
|
// and we can modify the deprecated reason in the entry adding this information |
53
|
|
|
$typeOrFieldInterfaceResolverClass = $entry[0]; |
54
|
|
|
// If we had a module (eg: "Users") and saved an entry with it, |
55
|
|
|
// and then disable it, the typeResolveClass will be null |
56
|
|
|
if (is_null($typeOrFieldInterfaceResolverClass)) { |
57
|
|
|
continue; |
58
|
|
|
} |
59
|
|
|
$fieldName = $entry[1]; |
60
|
|
|
$hookName = HookHelpers::getSchemaDefinitionForFieldHookName( |
61
|
|
|
$typeOrFieldInterfaceResolverClass, |
62
|
|
|
$fieldName |
63
|
|
|
); |
64
|
|
|
$hooksAPI->addFilter( |
65
|
|
|
$hookName, |
66
|
|
|
function (array $schemaDefinition) use ($deprecationReason): array { |
67
|
|
|
$schemaDefinition[SchemaDefinition::ARGNAME_DEPRECATED] = true; |
68
|
|
|
$schemaDefinition[SchemaDefinition::ARGNAME_DEPRECATIONDESCRIPTION] = $deprecationReason; |
69
|
|
|
return $schemaDefinition; |
70
|
|
|
}, |
71
|
|
|
10, |
72
|
|
|
1 |
73
|
|
|
); |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|