|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace GraphQLAPI\GraphQLAPI\ConditionalOnEnvironment\Admin\SchemaServices\FieldResolvers; |
|
6
|
|
|
|
|
7
|
|
|
use PoPSchema\CustomPosts\Facades\CustomPostTypeAPIFacade; |
|
8
|
|
|
use PoPSchema\CustomPosts\TypeResolvers\CustomPostTypeResolver; |
|
9
|
|
|
use PoP\Engine\TypeResolvers\RootTypeResolver; |
|
10
|
|
|
use PoP\ComponentModel\Schema\SchemaDefinition; |
|
11
|
|
|
use PoP\ComponentModel\Schema\TypeCastingHelpers; |
|
12
|
|
|
use PoP\Translation\Facades\TranslationAPIFacade; |
|
13
|
|
|
use PoP\ComponentModel\TypeResolvers\TypeResolverInterface; |
|
14
|
|
|
use PoP\ComponentModel\FieldResolvers\AbstractQueryableFieldResolver; |
|
15
|
|
|
use GraphQLAPI\GraphQLAPI\PostTypes\GraphQLCacheControlListPostType; |
|
16
|
|
|
use GraphQLAPI\GraphQLAPI\PostTypes\GraphQLAccessControlListPostType; |
|
17
|
|
|
use GraphQLAPI\GraphQLAPI\PostTypes\GraphQLSchemaConfigurationPostType; |
|
18
|
|
|
use GraphQLAPI\GraphQLAPI\PostTypes\GraphQLFieldDeprecationListPostType; |
|
19
|
|
|
use PoPSchema\CustomPosts\Types\Status; |
|
20
|
|
|
use PoPSchema\SchemaCommons\DataLoading\ReturnTypes; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* FieldResolver for the Custom Post Types from this plugin |
|
24
|
|
|
*/ |
|
25
|
|
|
class CPTFieldResolver extends AbstractQueryableFieldResolver |
|
26
|
|
|
{ |
|
27
|
|
|
/** |
|
28
|
|
|
* Option to tell the hook to not remove the private CPTs when querying |
|
29
|
|
|
*/ |
|
30
|
|
|
public const QUERY_OPTION_ALLOW_QUERYING_PRIVATE_CPTS = 'allow-querying-private-cpts'; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @return string[] |
|
34
|
|
|
*/ |
|
35
|
|
|
public static function getClassesToAttachTo(): array |
|
36
|
|
|
{ |
|
37
|
|
|
return [ |
|
38
|
|
|
RootTypeResolver::class, |
|
39
|
|
|
]; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @return string[] |
|
44
|
|
|
*/ |
|
45
|
|
|
public static function getFieldNamesToResolve(): array |
|
46
|
|
|
{ |
|
47
|
|
|
return [ |
|
48
|
|
|
'accessControlLists', |
|
49
|
|
|
'cacheControlLists', |
|
50
|
|
|
'fieldDeprecationLists', |
|
51
|
|
|
'schemaConfigurations', |
|
52
|
|
|
]; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* These fields are used only by the application, so no need to |
|
57
|
|
|
* expose them to the user |
|
58
|
|
|
* |
|
59
|
|
|
* @param TypeResolverInterface $typeResolver |
|
60
|
|
|
* @param string $fieldName |
|
61
|
|
|
* @return boolean |
|
62
|
|
|
*/ |
|
63
|
|
|
public function skipAddingToSchemaDefinition(TypeResolverInterface $typeResolver, string $fieldName): bool |
|
64
|
|
|
{ |
|
65
|
|
|
return true; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
public function getSchemaFieldType(TypeResolverInterface $typeResolver, string $fieldName): ?string |
|
69
|
|
|
{ |
|
70
|
|
|
$types = [ |
|
71
|
|
|
'accessControlLists' => TypeCastingHelpers::makeArray(SchemaDefinition::TYPE_ID), |
|
72
|
|
|
'cacheControlLists' => TypeCastingHelpers::makeArray(SchemaDefinition::TYPE_ID), |
|
73
|
|
|
'fieldDeprecationLists' => TypeCastingHelpers::makeArray(SchemaDefinition::TYPE_ID), |
|
74
|
|
|
'schemaConfigurations' => TypeCastingHelpers::makeArray(SchemaDefinition::TYPE_ID), |
|
75
|
|
|
]; |
|
76
|
|
|
return $types[$fieldName] ?? parent::getSchemaFieldType($typeResolver, $fieldName); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
public function getSchemaFieldDescription(TypeResolverInterface $typeResolver, string $fieldName): ?string |
|
80
|
|
|
{ |
|
81
|
|
|
$translationAPI = TranslationAPIFacade::getInstance(); |
|
82
|
|
|
$descriptions = [ |
|
83
|
|
|
'accessControlLists' => $translationAPI->__('Access Control Lists', 'graphql-api'), |
|
84
|
|
|
'cacheControlLists' => $translationAPI->__('Cache Control Lists', 'graphql-api'), |
|
85
|
|
|
'fieldDeprecationLists' => $translationAPI->__('Field Deprecation Lists', 'graphql-api'), |
|
86
|
|
|
'schemaConfigurations' => $translationAPI->__('Schema Configurations', 'graphql-api'), |
|
87
|
|
|
]; |
|
88
|
|
|
return $descriptions[$fieldName] ?? parent::getSchemaFieldDescription($typeResolver, $fieldName); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* @return array<string, mixed> |
|
93
|
|
|
*/ |
|
94
|
|
|
public function getSchemaFieldArgs(TypeResolverInterface $typeResolver, string $fieldName): array |
|
95
|
|
|
{ |
|
96
|
|
|
$schemaFieldArgs = parent::getSchemaFieldArgs($typeResolver, $fieldName); |
|
97
|
|
|
switch ($fieldName) { |
|
98
|
|
|
case 'accessControlLists': |
|
99
|
|
|
case 'cacheControlLists': |
|
100
|
|
|
case 'fieldDeprecationLists': |
|
101
|
|
|
case 'schemaConfigurations': |
|
102
|
|
|
$schemaFieldArgs = array_merge( |
|
103
|
|
|
$schemaFieldArgs, |
|
104
|
|
|
$this->getFieldArgumentsSchemaDefinitions($typeResolver, $fieldName) |
|
105
|
|
|
); |
|
106
|
|
|
// Remove the "customPostTypes" field argument |
|
107
|
|
|
$schemaFieldArgs = array_filter( |
|
108
|
|
|
$schemaFieldArgs, |
|
109
|
|
|
fn ($schemaFieldArg) => $schemaFieldArg[SchemaDefinition::ARGNAME_NAME] != 'customPostTypes' |
|
110
|
|
|
); |
|
111
|
|
|
break; |
|
112
|
|
|
} |
|
113
|
|
|
return $schemaFieldArgs; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
public function enableOrderedSchemaFieldArgs(TypeResolverInterface $typeResolver, string $fieldName): bool |
|
117
|
|
|
{ |
|
118
|
|
|
switch ($fieldName) { |
|
119
|
|
|
case 'accessControlLists': |
|
120
|
|
|
case 'cacheControlLists': |
|
121
|
|
|
case 'fieldDeprecationLists': |
|
122
|
|
|
case 'schemaConfigurations': |
|
123
|
|
|
return false; |
|
124
|
|
|
} |
|
125
|
|
|
return parent::enableOrderedSchemaFieldArgs($typeResolver, $fieldName); |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
/** |
|
129
|
|
|
* @param array<string, mixed> $fieldArgs |
|
130
|
|
|
* @return array<string, mixed> |
|
131
|
|
|
*/ |
|
132
|
|
|
protected function getQuery( |
|
133
|
|
|
TypeResolverInterface $typeResolver, |
|
134
|
|
|
object $resultItem, |
|
135
|
|
|
string $fieldName, |
|
136
|
|
|
array $fieldArgs = [] |
|
137
|
|
|
): array { |
|
138
|
|
|
switch ($fieldName) { |
|
139
|
|
|
case 'accessControlLists': |
|
140
|
|
|
case 'cacheControlLists': |
|
141
|
|
|
case 'fieldDeprecationLists': |
|
142
|
|
|
case 'schemaConfigurations': |
|
143
|
|
|
$query = [ |
|
144
|
|
|
'limit' => -1, |
|
145
|
|
|
'status' => [ |
|
146
|
|
|
Status::PUBLISHED, |
|
147
|
|
|
], |
|
148
|
|
|
]; |
|
149
|
|
|
return $query; |
|
150
|
|
|
} |
|
151
|
|
|
return []; |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
/** |
|
155
|
|
|
* @param array<string, mixed> $fieldArgs |
|
156
|
|
|
* @param array<string, mixed>|null $variables |
|
157
|
|
|
* @param array<string, mixed>|null $expressions |
|
158
|
|
|
* @param array<string, mixed> $options |
|
159
|
|
|
* @return mixed |
|
160
|
|
|
*/ |
|
161
|
|
|
public function resolveValue( |
|
162
|
|
|
TypeResolverInterface $typeResolver, |
|
163
|
|
|
object $resultItem, |
|
164
|
|
|
string $fieldName, |
|
165
|
|
|
array $fieldArgs = [], |
|
166
|
|
|
?array $variables = null, |
|
167
|
|
|
?array $expressions = null, |
|
168
|
|
|
array $options = [] |
|
169
|
|
|
) { |
|
170
|
|
|
$customPostTypeAPI = CustomPostTypeAPIFacade::getInstance(); |
|
171
|
|
|
switch ($fieldName) { |
|
172
|
|
|
case 'accessControlLists': |
|
173
|
|
|
case 'cacheControlLists': |
|
174
|
|
|
case 'fieldDeprecationLists': |
|
175
|
|
|
case 'schemaConfigurations': |
|
176
|
|
|
// Remove the "customPostTypes" field argument |
|
177
|
|
|
unset($fieldArgs['customPostTypes']); |
|
178
|
|
|
$query = $this->getQuery($typeResolver, $resultItem, $fieldName, $fieldArgs); |
|
179
|
|
|
// Execute for the corresponding field name |
|
180
|
|
|
$customPostTypes = [ |
|
181
|
|
|
'accessControlLists' => GraphQLAccessControlListPostType::POST_TYPE, |
|
182
|
|
|
'cacheControlLists' => GraphQLCacheControlListPostType::POST_TYPE, |
|
183
|
|
|
'fieldDeprecationLists' => GraphQLFieldDeprecationListPostType::POST_TYPE, |
|
184
|
|
|
'schemaConfigurations' => GraphQLSchemaConfigurationPostType::POST_TYPE, |
|
185
|
|
|
]; |
|
186
|
|
|
$query['custompost-types'] = [ |
|
187
|
|
|
$customPostTypes[$fieldName], |
|
188
|
|
|
]; |
|
189
|
|
|
$options = [ |
|
190
|
|
|
'return-type' => ReturnTypes::IDS, |
|
191
|
|
|
// Do not use the limit set in the settings for custom posts |
|
192
|
|
|
'skip-max-limit' => true, |
|
193
|
|
|
// With this flag, the hook will not remove the private CPTs |
|
194
|
|
|
self::QUERY_OPTION_ALLOW_QUERYING_PRIVATE_CPTS => true, |
|
195
|
|
|
]; |
|
196
|
|
|
$this->addFilterDataloadQueryArgs($options, $typeResolver, $fieldName, $fieldArgs); |
|
197
|
|
|
return $customPostTypeAPI->getCustomPosts($query, $options); |
|
198
|
|
|
} |
|
199
|
|
|
|
|
200
|
|
|
return parent::resolveValue( |
|
201
|
|
|
$typeResolver, |
|
202
|
|
|
$resultItem, |
|
203
|
|
|
$fieldName, |
|
204
|
|
|
$fieldArgs, |
|
205
|
|
|
$variables, |
|
206
|
|
|
$expressions, |
|
207
|
|
|
$options |
|
208
|
|
|
); |
|
209
|
|
|
} |
|
210
|
|
|
|
|
211
|
|
|
public function resolveFieldTypeResolverClass( |
|
212
|
|
|
TypeResolverInterface $typeResolver, |
|
213
|
|
|
string $fieldName |
|
214
|
|
|
): ?string { |
|
215
|
|
|
switch ($fieldName) { |
|
216
|
|
|
case 'accessControlLists': |
|
217
|
|
|
case 'cacheControlLists': |
|
218
|
|
|
case 'fieldDeprecationLists': |
|
219
|
|
|
case 'schemaConfigurations': |
|
220
|
|
|
return CustomPostTypeResolver::class; |
|
221
|
|
|
} |
|
222
|
|
|
|
|
223
|
|
|
return parent::resolveFieldTypeResolverClass($typeResolver, $fieldName); |
|
224
|
|
|
} |
|
225
|
|
|
} |
|
226
|
|
|
|