|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace TheCodingMachine\GraphQLite; |
| 6 | + |
| 7 | +use GraphQL\Error\ClientAware; |
| 8 | +use GraphQL\Type\Definition\InputObjectField; |
| 9 | +use GraphQL\Type\Definition\InputType; |
| 10 | +use GraphQL\Type\Definition\ListOfType; |
| 11 | +use GraphQL\Type\Definition\NonNull; |
| 12 | +use GraphQL\Type\Definition\ResolveInfo; |
| 13 | +use GraphQL\Type\Definition\Type; |
| 14 | +use GraphQL\Type\Definition\NullableType; |
| 15 | +use TheCodingMachine\GraphQLite\Exceptions\GraphQLAggregateException; |
| 16 | +use TheCodingMachine\GraphQLite\Middlewares\MissingAuthorizationException; |
| 17 | +use TheCodingMachine\GraphQLite\Middlewares\ResolverInterface; |
| 18 | +use TheCodingMachine\GraphQLite\Middlewares\SourceResolverInterface; |
| 19 | +use TheCodingMachine\GraphQLite\Parameters\InputTypeParameter; |
| 20 | +use TheCodingMachine\GraphQLite\Parameters\InputTypeProperty; |
| 21 | +use TheCodingMachine\GraphQLite\Parameters\MissingArgumentException; |
| 22 | +use TheCodingMachine\GraphQLite\Parameters\ParameterInterface; |
| 23 | +use TheCodingMachine\GraphQLite\Parameters\SourceParameter; |
| 24 | + |
| 25 | +/** |
| 26 | + * A GraphQL input field that maps to a PHP method automatically. |
| 27 | + * |
| 28 | + * @internal |
| 29 | + */ |
| 30 | +class InputField extends InputObjectField |
| 31 | +{ |
| 32 | + /** @var Callable */ |
| 33 | + private $resolve; |
| 34 | + |
| 35 | + /** @var bool */ |
| 36 | + private $forConstructorHydration = false; |
| 37 | + |
| 38 | + /** |
| 39 | + * @param InputType|(NullableType&Type) $type |
| 40 | + * @param array<string, ParameterInterface> $arguments Indexed by argument name. |
| 41 | + * @param ResolverInterface $originalResolver A pointer to the resolver being called (but not wrapped by any field middleware) |
| 42 | + * @param callable $resolver The resolver actually called |
| 43 | + * @param mixed|null $defaultValue the default value set for this field |
| 44 | + * @param array<string, mixed> $additionalConfig |
| 45 | + */ |
| 46 | + public function __construct(string $name, $type, array $arguments, ?ResolverInterface $originalResolver, ?callable $resolver, ?string $comment, bool $isUpdate,bool $hasDefaultValue, $defaultValue, array $additionalConfig = []) |
| 47 | + { |
| 48 | + $config = [ |
| 49 | + 'name' => $name, |
| 50 | + 'type' => $type, |
| 51 | + 'description' => $comment |
| 52 | + ]; |
| 53 | + |
| 54 | + if (!(!$hasDefaultValue || $isUpdate)) { |
| 55 | + $config['defaultValue'] = $defaultValue; |
| 56 | + } |
| 57 | + |
| 58 | + if ($originalResolver !== null && $resolver !== null){ |
| 59 | + $this->resolve = function ($source, array $args, $context, ResolveInfo $info) use ($arguments, $originalResolver, $resolver) { |
| 60 | + if ($originalResolver instanceof SourceResolverInterface) { |
| 61 | + $originalResolver->setObject($source); |
| 62 | + } |
| 63 | + $toPassArgs = $this->paramsToArguments($arguments, $source, $args, $context, $info, $resolver); |
| 64 | + $result = $resolver(...$toPassArgs); |
| 65 | + |
| 66 | + try { |
| 67 | + $this->assertInputType($result); |
| 68 | + } catch (TypeMismatchRuntimeException $e) { |
| 69 | + $e->addInfo($this->name, $originalResolver->toString()); |
| 70 | + throw $e; |
| 71 | + } |
| 72 | + |
| 73 | + return $result; |
| 74 | + }; |
| 75 | + } else { |
| 76 | + $this->forConstructorHydration = true; |
| 77 | + $this->resolve = function ($source, array $args, $context, ResolveInfo $info) use ($arguments) { |
| 78 | + $result = $arguments[$this->name]->resolve($source, $args, $context, $info); |
| 79 | + $this->assertInputType($result); |
| 80 | + return $result; |
| 81 | + }; |
| 82 | + } |
| 83 | + |
| 84 | + |
| 85 | + $config += $additionalConfig; |
| 86 | + parent::__construct($config); |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * @return Callable |
| 91 | + */ |
| 92 | + public function getResolve() |
| 93 | + { |
| 94 | + return $this->resolve; |
| 95 | + } |
| 96 | + |
| 97 | + /** |
| 98 | + * |
| 99 | + * @param mixed $input |
| 100 | + */ |
| 101 | + private function assertInputType($input): void |
| 102 | + { |
| 103 | + $type = $this->removeNonNull($this->getType()); |
| 104 | + if (! $type instanceof ListOfType) { |
| 105 | + return; |
| 106 | + } |
| 107 | + |
| 108 | + ResolveUtils::assertInnerInputType($input, $type); |
| 109 | + } |
| 110 | + |
| 111 | + private function removeNonNull(Type $type): Type |
| 112 | + { |
| 113 | + if ($type instanceof NonNull) { |
| 114 | + return $type->getWrappedType(); |
| 115 | + } |
| 116 | + |
| 117 | + return $type; |
| 118 | + } |
| 119 | + |
| 120 | + public function forConstructorHydration(): bool |
| 121 | + { |
| 122 | + return $this->forConstructorHydration; |
| 123 | + } |
| 124 | + |
| 125 | + /** |
| 126 | + * @param bool $isNotLogged False if the user is logged (and the error is a 403), true if the error is unlogged (the error is a 401) |
| 127 | + * |
| 128 | + * @return InputField |
| 129 | + */ |
| 130 | + public static function unauthorizedError(InputFieldDescriptor $fieldDescriptor, bool $isNotLogged): self |
| 131 | + { |
| 132 | + $callable = static function () use ($isNotLogged): void { |
| 133 | + if ($isNotLogged) { |
| 134 | + throw MissingAuthorizationException::unauthorized(); |
| 135 | + } |
| 136 | + throw MissingAuthorizationException::forbidden(); |
| 137 | + }; |
| 138 | + |
| 139 | + $fieldDescriptor->setResolver($callable); |
| 140 | + |
| 141 | + return self::fromDescriptor($fieldDescriptor); |
| 142 | + } |
| 143 | + |
| 144 | + private static function fromDescriptor(InputFieldDescriptor $fieldDescriptor): self |
| 145 | + { |
| 146 | + return new self( |
| 147 | + $fieldDescriptor->getName(), |
| 148 | + $fieldDescriptor->getType(), |
| 149 | + $fieldDescriptor->getParameters(), |
| 150 | + $fieldDescriptor->getOriginalResolver(), |
| 151 | + $fieldDescriptor->getResolver(), |
| 152 | + $fieldDescriptor->getComment(), |
| 153 | + $fieldDescriptor->isUpdate(), |
| 154 | + $fieldDescriptor->hasDefaultValue(), |
| 155 | + $fieldDescriptor->getDefaultValue(), |
| 156 | + ); |
| 157 | + } |
| 158 | + |
| 159 | + public static function fromFieldDescriptor(InputFieldDescriptor $fieldDescriptor): self |
| 160 | + { |
| 161 | + $arguments = $fieldDescriptor->getParameters(); |
| 162 | + if ($fieldDescriptor->isInjectSource() === true) { |
| 163 | + $arguments = ['__graphqlite_source' => new SourceParameter()] + $arguments; |
| 164 | + } |
| 165 | + $fieldDescriptor->setParameters($arguments); |
| 166 | + |
| 167 | + return self::fromDescriptor($fieldDescriptor); |
| 168 | + } |
| 169 | + |
| 170 | + /** |
| 171 | + * Casts parameters array into an array of arguments ready to be passed to the resolver. |
| 172 | + * |
| 173 | + * @param ParameterInterface[] $parameters |
| 174 | + * @param array<string, mixed> $args |
| 175 | + * @param mixed $context |
| 176 | + * |
| 177 | + * @return array<int, mixed> |
| 178 | + */ |
| 179 | + private function paramsToArguments(array $parameters, ?object $source, array $args, $context, ResolveInfo $info, callable $resolve): array |
| 180 | + { |
| 181 | + $toPassArgs = []; |
| 182 | + $exceptions = []; |
| 183 | + foreach ($parameters as $parameter) { |
| 184 | + try { |
| 185 | + $toPassArgs[] = $parameter->resolve($source, $args, $context, $info); |
| 186 | + } catch (MissingArgumentException $e) { |
| 187 | + throw MissingArgumentException::wrapWithFieldContext($e, $this->name, $resolve); |
| 188 | + } catch (ClientAware $e) { |
| 189 | + $exceptions[] = $e; |
| 190 | + } |
| 191 | + } |
| 192 | + GraphQLAggregateException::throwExceptions($exceptions); |
| 193 | + |
| 194 | + return $toPassArgs; |
| 195 | + } |
| 196 | +} |
0 commit comments