forked from thecodingmachine/graphqlite
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInputField.php
More file actions
178 lines (155 loc) · 5.9 KB
/
InputField.php
File metadata and controls
178 lines (155 loc) · 5.9 KB
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
<?php
declare(strict_types=1);
namespace TheCodingMachine\GraphQLite;
use GraphQL\Error\ClientAware;
use GraphQL\Language\AST\InputValueDefinitionNode;
use GraphQL\Type\Definition\InputObjectField;
use GraphQL\Type\Definition\InputType;
use GraphQL\Type\Definition\ListOfType;
use GraphQL\Type\Definition\NonNull;
use GraphQL\Type\Definition\ResolveInfo;
use GraphQL\Type\Definition\Type;
use TheCodingMachine\GraphQLite\Exceptions\GraphQLAggregateException;
use TheCodingMachine\GraphQLite\Middlewares\ResolverInterface;
use TheCodingMachine\GraphQLite\Parameters\MissingArgumentException;
use TheCodingMachine\GraphQLite\Parameters\ParameterInterface;
use TheCodingMachine\GraphQLite\Parameters\SourceParameter;
use Throwable;
/**
* A GraphQL input field that maps to a PHP method automatically.
*
* @phpstan-import-type InputObjectFieldConfig from InputObjectField
* @phpstan-import-type ArgumentType from InputObjectField
*/
final class InputField extends InputObjectField
{
/** @var callable */
private $resolve;
/**
* @param (Type&InputType) $type
* @param array<string, ParameterInterface> $arguments Indexed by argument name.
* @param mixed|null $defaultValue the default value set for this field
* @param array{defaultValue?: mixed,description?: string|null,astNode?: InputValueDefinitionNode|null}|null $additionalConfig
*/
public function __construct(
string $name,
InputType $type,
array $arguments,
ResolverInterface $originalResolver,
callable $resolver,
private bool $forConstructorHydration,
string|null $comment,
bool $isUpdate,
bool $hasDefaultValue,
mixed $defaultValue,
array|null $additionalConfig = null,
) {
$config = [
'name' => $name,
'type' => $type,
'description' => $comment,
];
if (! (! $hasDefaultValue || $isUpdate)) {
$config['defaultValue'] = $defaultValue;
}
$this->resolve = function (object|null $source, array $args, $context, ResolveInfo $info) use ($arguments, $originalResolver, $resolver) {
if ($this->forConstructorHydration) {
$toPassArgs = [$arguments[$this->name]->resolve($source, $args, $context, $info)];
} else {
$toPassArgs = $this->paramsToArguments($arguments, $source, $args, $context, $info, $resolver);
}
$result = $resolver($source, ...$toPassArgs);
try {
$this->assertInputType($result);
} catch (TypeMismatchRuntimeException $e) {
$e->addInfo($this->name, $originalResolver->toString());
throw $e;
}
return $result;
};
if ($additionalConfig !== null) {
if (isset($additionalConfig['astNode'])) {
$config['astNode'] = $additionalConfig['astNode'];
}
if (isset($additionalConfig['defaultValue'])) {
$config['defaultValue'] = $additionalConfig['defaultValue'];
}
if (isset($additionalConfig['description'])) {
$config['description'] = $additionalConfig['description'];
}
}
parent::__construct($config);
}
public function getResolve(): callable
{
return $this->resolve;
}
private function assertInputType(mixed $input): void
{
$type = $this->removeNonNull($this->getType());
if (! $type instanceof ListOfType) {
return;
}
ResolveUtils::assertInnerInputType($input, $type);
}
private function removeNonNull(Type $type): Type
{
if ($type instanceof NonNull) {
return $type->getWrappedType();
}
return $type;
}
public function forConstructorHydration(): bool
{
return $this->forConstructorHydration;
}
private static function fromDescriptor(InputFieldDescriptor $fieldDescriptor): self
{
return new self(
$fieldDescriptor->getName(),
$fieldDescriptor->getType(),
$fieldDescriptor->getParameters(),
$fieldDescriptor->getOriginalResolver(),
$fieldDescriptor->getResolver(),
$fieldDescriptor->isForConstructorHydration(),
$fieldDescriptor->getComment(),
$fieldDescriptor->isUpdate(),
$fieldDescriptor->hasDefaultValue(),
$fieldDescriptor->getDefaultValue(),
);
}
public static function fromFieldDescriptor(InputFieldDescriptor $fieldDescriptor): self
{
$arguments = $fieldDescriptor->getParameters();
if ($fieldDescriptor->isInjectSource() === true) {
$arguments = ['__graphqlite_source' => new SourceParameter()] + $arguments;
}
$fieldDescriptor = $fieldDescriptor->withParameters($arguments);
return self::fromDescriptor($fieldDescriptor);
}
/**
* Casts parameters array into an array of arguments ready to be passed to the resolver.
*
* @param ParameterInterface[] $parameters
* @param array<string, mixed> $args
*
* @return array<int, mixed>
*/
private function paramsToArguments(array $parameters, object|null $source, array $args, mixed $context, ResolveInfo $info, callable $resolve): array
{
$toPassArgs = [];
/** @var (ClientAware&Throwable)[] $exceptions */
$exceptions = [];
foreach ($parameters as $parameter) {
try {
$toPassArgs[] = $parameter->resolve($source, $args, $context, $info);
} catch (MissingArgumentException $e) {
throw MissingArgumentException::wrapWithFieldContext($e, $this->name, $resolve);
} catch (ClientAware $e) {
$exceptions[] = $e;
}
}
GraphQLAggregateException::throwExceptions($exceptions);
return $toPassArgs;
}
}