-
-
Notifications
You must be signed in to change notification settings - Fork 39
/
CustomPostObjectTypeFieldResolver.php
executable file
·112 lines (100 loc) · 4.33 KB
/
CustomPostObjectTypeFieldResolver.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
<?php
declare(strict_types=1);
namespace PoPCMSSchema\Users\ConditionalOnModule\CustomPosts\FieldResolvers\ObjectType;
use PoPCMSSchema\CustomPosts\TypeResolvers\ObjectType\AbstractCustomPostObjectTypeResolver;
use PoPCMSSchema\Users\ConditionalOnModule\CustomPosts\TypeAPIs\CustomPostUserTypeAPIInterface;
use PoPCMSSchema\Users\FieldResolvers\InterfaceType\WithAuthorInterfaceTypeFieldResolver;
use PoP\ComponentModel\Feedback\ObjectTypeFieldResolutionFeedbackStore;
use PoP\ComponentModel\FieldResolvers\InterfaceType\InterfaceTypeFieldResolverInterface;
use PoP\ComponentModel\FieldResolvers\ObjectType\AbstractObjectTypeFieldResolver;
use PoP\ComponentModel\QueryResolution\FieldDataAccessorInterface;
use PoP\ComponentModel\TypeResolvers\ObjectType\ObjectTypeResolverInterface;
use PoP\GraphQLParser\Spec\Parser\Ast\FieldInterface;
class CustomPostObjectTypeFieldResolver extends AbstractObjectTypeFieldResolver
{
private ?CustomPostUserTypeAPIInterface $customPostUserTypeAPI = null;
private ?WithAuthorInterfaceTypeFieldResolver $withAuthorInterfaceTypeFieldResolver = null;
final public function setCustomPostUserTypeAPI(CustomPostUserTypeAPIInterface $customPostUserTypeAPI): void
{
$this->customPostUserTypeAPI = $customPostUserTypeAPI;
}
final protected function getCustomPostUserTypeAPI(): CustomPostUserTypeAPIInterface
{
if ($this->customPostUserTypeAPI === null) {
/** @var CustomPostUserTypeAPIInterface */
$customPostUserTypeAPI = $this->instanceManager->getInstance(CustomPostUserTypeAPIInterface::class);
$this->customPostUserTypeAPI = $customPostUserTypeAPI;
}
return $this->customPostUserTypeAPI;
}
final public function setWithAuthorInterfaceTypeFieldResolver(WithAuthorInterfaceTypeFieldResolver $withAuthorInterfaceTypeFieldResolver): void
{
$this->withAuthorInterfaceTypeFieldResolver = $withAuthorInterfaceTypeFieldResolver;
}
final protected function getWithAuthorInterfaceTypeFieldResolver(): WithAuthorInterfaceTypeFieldResolver
{
if ($this->withAuthorInterfaceTypeFieldResolver === null) {
/** @var WithAuthorInterfaceTypeFieldResolver */
$withAuthorInterfaceTypeFieldResolver = $this->instanceManager->getInstance(WithAuthorInterfaceTypeFieldResolver::class);
$this->withAuthorInterfaceTypeFieldResolver = $withAuthorInterfaceTypeFieldResolver;
}
return $this->withAuthorInterfaceTypeFieldResolver;
}
/**
* @return array<class-string<ObjectTypeResolverInterface>>
*/
public function getObjectTypeResolverClassesToAttachTo(): array
{
return [
AbstractCustomPostObjectTypeResolver::class,
];
}
/**
* @return array<InterfaceTypeFieldResolverInterface>
*/
public function getImplementedInterfaceTypeFieldResolvers(): array
{
return [
$this->getWithAuthorInterfaceTypeFieldResolver(),
];
}
/**
* @return string[]
*/
public function getFieldNamesToResolve(): array
{
return [
'author',
];
}
public function getFieldDescription(ObjectTypeResolverInterface $objectTypeResolver, string $fieldName): ?string
{
return match ($fieldName) {
'author' => $this->__('The post\'s author', ''),
default => parent::getFieldDescription($objectTypeResolver, $fieldName),
};
}
public function resolveValue(
ObjectTypeResolverInterface $objectTypeResolver,
object $object,
FieldDataAccessorInterface $fieldDataAccessor,
ObjectTypeFieldResolutionFeedbackStore $objectTypeFieldResolutionFeedbackStore,
): mixed {
switch ($fieldDataAccessor->getFieldName()) {
case 'author':
/** @var string|int */
return $this->getCustomPostUserTypeAPI()->getAuthorID($object);
}
return parent::resolveValue($objectTypeResolver, $object, $fieldDataAccessor, $objectTypeFieldResolutionFeedbackStore);
}
/**
* Since the return type is known for all the fields in this
* FieldResolver, there's no need to validate them
*/
public function validateResolvedFieldType(
ObjectTypeResolverInterface $objectTypeResolver,
FieldInterface $field,
): bool {
return false;
}
}