-
-
Notifications
You must be signed in to change notification settings - Fork 270
validate field arguments #608
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
36f6c6e
2392bab
17f7194
9984377
f96922e
00b922d
689a115
a63ed17
427d6d9
cf8c536
b91e7df
82438f7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -20,6 +20,13 @@ | |
| */ | ||
| abstract class Field | ||
| { | ||
| /** | ||
| * The depth the SelectField and ResolveInfoFieldsAndArguments classes traverse. | ||
| * | ||
| * @var int | ||
| */ | ||
| protected $depth = 5; | ||
|
|
||
| protected $attributes = []; | ||
|
|
||
| /** | ||
|
|
@@ -86,6 +93,21 @@ public function getRules(array $arguments = []): array | |
| return array_merge($argsRules, $rules); | ||
| } | ||
|
|
||
| /** | ||
| * @param array<string,mixed> $fieldsAndArgumentsSelection | ||
| * @return void | ||
| */ | ||
| public function validateFieldArguments(array $fieldsAndArgumentsSelection): void | ||
| { | ||
| $argsRules = (new RulesInFields($this->type(), $fieldsAndArgumentsSelection))->get(); | ||
| if (count($argsRules)) { | ||
| $validator = $this->getValidator($fieldsAndArgumentsSelection, $argsRules); | ||
| if ($validator->fails()) { | ||
| throw new ValidationError('validation', $validator); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| public function getValidator(array $args, array $rules): ValidatorContract | ||
| { | ||
| // allow our error messages to be customised | ||
|
|
@@ -123,6 +145,11 @@ protected function getResolver(): ?Closure | |
| } | ||
| } | ||
|
|
||
| $fieldsAndArguments = (new ResolveInfoFieldsAndArguments($arguments[3]))->getFieldsAndArgumentsSelection($this->depth); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No the big prize question: how can we avoid resolving this if there aren't going to be any validations (they're optional after all)? Do you see any way to achieve this? Seems like a catch-22: only when we run There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I could probably write a function that checks if any arguments exists that would be less costly than the ResolveInfoFieldsAndArguments, and then only run ResolveInfoFieldsAndArguments if using selectfields and arguments... but not sure how much it will actually give. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You're right, let's scrap that for now! |
||
|
|
||
| // Validate arguments in fields | ||
| $this->validateFieldArguments($fieldsAndArguments); | ||
|
|
||
| $arguments[1] = $this->getArgs($arguments); | ||
|
|
||
| // Authorize | ||
|
|
@@ -134,21 +161,21 @@ protected function getResolver(): ?Closure | |
|
|
||
| $additionalParams = array_slice($method->getParameters(), 3); | ||
|
|
||
| $additionalArguments = array_map(function ($param) use ($arguments) { | ||
| $additionalArguments = array_map(function ($param) use ($arguments, $fieldsAndArguments) { | ||
| $className = null !== $param->getClass() ? $param->getClass()->getName() : null; | ||
|
|
||
| if (null === $className) { | ||
| throw new InvalidArgumentException("'{$param->name}' could not be injected"); | ||
| } | ||
|
|
||
| if (Closure::class === $param->getType()->getName()) { | ||
| return function (int $depth = null) use ($arguments): SelectFields { | ||
| return $this->instanciateSelectFields($arguments, $depth); | ||
| return function (int $depth = null) use ($arguments, $fieldsAndArguments): SelectFields { | ||
| return $this->instanciateSelectFields($arguments, $fieldsAndArguments, $depth); | ||
| }; | ||
| } | ||
|
|
||
| if (SelectFields::class === $className) { | ||
| return $this->instanciateSelectFields($arguments); | ||
| return $this->instanciateSelectFields($arguments, $fieldsAndArguments, null); | ||
| } | ||
|
|
||
| if (ResolveInfo::class === $className) { | ||
|
|
@@ -165,11 +192,22 @@ protected function getResolver(): ?Closure | |
| }; | ||
| } | ||
|
|
||
| private function instanciateSelectFields(array $arguments, int $depth = null): SelectFields | ||
| /** | ||
| * @param array<int,mixed> $arguments | ||
| * @param int $depth | ||
| * @param array<string,mixed> $fieldsAndArguments | ||
| * @return SelectFields | ||
| */ | ||
| private function instanciateSelectFields(array $arguments, array $fieldsAndArguments, int $depth = null): SelectFields | ||
| { | ||
| $ctx = $arguments[2] ?? null; | ||
|
|
||
| return new SelectFields($arguments[3], $this->type(), $arguments[1], $depth ?? 5, $ctx); | ||
| if ($depth !== null && $depth !== $this->depth) { | ||
| $fieldsAndArguments = (new ResolveInfoFieldsAndArguments($arguments[3])) | ||
| ->getFieldsAndArgumentsSelection($depth); | ||
crissi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| return new SelectFields($this->type(), $arguments[1], $ctx, $fieldsAndArguments); | ||
| } | ||
|
|
||
| protected function aliasArgs(array $arguments): array | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,93 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Rebing\GraphQL\Support; | ||
|
|
||
| use GraphQL\Type\Definition\Type; | ||
| use GraphQL\Type\Definition\WrappingType; | ||
|
|
||
| class RulesInFields | ||
| { | ||
| /** | ||
| * @var Type | ||
| */ | ||
| protected $parentType; | ||
|
|
||
| /** | ||
| * @var array<string,mixed> | ||
| */ | ||
| protected $fieldsAndArguments; | ||
|
|
||
| /** | ||
| * @param Type $parentType | ||
| * @param array<string,mixed> $fieldsAndArgumentsSelection | ||
| */ | ||
| public function __construct(Type $parentType, array $fieldsAndArgumentsSelection) | ||
| { | ||
| $this->parentType = $parentType instanceof WrappingType | ||
| ? $parentType->getWrappedType(true) | ||
| : $parentType; | ||
| $this->fieldsAndArguments = $fieldsAndArgumentsSelection; | ||
| } | ||
|
|
||
| /** | ||
| * @return array<array> | ||
| */ | ||
| public function get(): array | ||
| { | ||
| return $this->getRules($this->fieldsAndArguments, null, $this->parentType); | ||
| } | ||
|
|
||
| /** | ||
| * @param array<string,mixed>|string|callable $rules | ||
| * @param array<string,mixed> $arguments | ||
| * @return array<string,mixed>|string | ||
| */ | ||
| protected function resolveRules($rules, array $arguments) | ||
| { | ||
| if (is_callable($rules)) { | ||
| return $rules($arguments); | ||
| } | ||
|
|
||
| return $rules; | ||
| } | ||
|
|
||
| /** | ||
| * Get rules from fields. | ||
| * | ||
| * @param array<string,mixed> $fields | ||
| * @param string|null $prefix | ||
| * @param Type $parentType | ||
| * @return array<string,mixed> | ||
| */ | ||
| protected function getRules(array $fields, ?string $prefix, Type $parentType): array | ||
crissi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| { | ||
| $rules = []; | ||
|
|
||
| foreach ($fields as $name => $field) { | ||
| $key = $prefix === null ? $name : "{$prefix}.{$name}"; | ||
|
|
||
| //If field doesn't exist on definition we don't select it | ||
| if (! method_exists($parentType, 'getField')) { | ||
| continue; | ||
| } | ||
|
|
||
| $fieldObject = $parentType->getField($name); | ||
|
|
||
| if (is_array($field['fields'])) { | ||
| $rules = $rules + $this->getRules($field['fields'], $key.'.fields', $fieldObject->getType()); | ||
| } | ||
|
|
||
| $args = $fieldObject->config['args'] ?? []; | ||
|
|
||
| foreach ($args as $argName => $info) { | ||
| if (isset($info['rules'])) { | ||
| $rules[$key.'.args.'.$argName] = $this->resolveRules($info['rules'], $field['args']); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return $rules; | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.