-
Versions:
Example
class CreateFoo extends Mutation {
protected $attributes = ['name' => 'createFoo'];
public function type(): Type {
return Type::string();
}
public function args(): array {
return [
'fooArg' => [
'type' => GraphQL::type('[FooInput]'),
],
];
}
public function resolve($root, $args) {
return \json_encode($args);
}
}
class FooInput extends InputType {
protected $attributes = ['name' => 'FooInput'];
public function fields(): array {
return [
'hasBar' => [
'type' => Type::boolean(),
],
'bar' => [
'type' => Type::string(),
'rules' => [
'exclude_if:hasBar,false',
'min:10',
],
],
];
}
} Query: mutation Foo {
createFoo(fooArg: [{
hasBar: false,
bar: "foobar",
}])
}
{
"errors": [
{
"message": "validation",
"extensions": {
"category": "validation",
"validation": {
"fooArg.0.bar": [
"The foo arg.0.bar must be at least 10 characters."
]
}
},
"locations": [
{
"line": 33,
"column": 3
}
],
"path": [
"createFoo"
]
}
],
"data": {
"createFoo": null
}
} To make it work I need to specify full field path for As a workaround we can use Laravel Validator directly in mutation's protected function rules(array $args = []): array {
return [
'fooArg.*.bar' => [
'exclude_if:fooArg.*.hasBar,false',
'min:10',
],
];
} But I don't like this approach, because
|
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
I basically agree and I don't have much of a solution to offer. I suffer similar things on other ends: I want great GraphQL validation (and that means: not just the types) so I too do also make use of the And I agree also in your scenario, it gets less useful if you can't couple the The ability to have @crissi did a lot of work to improve the situation, but as with TL;DR: doing it in your query/mutation |
Beta Was this translation helpful? Give feedback.
-
Remember rules can also take a callback which Include params and even parent params
|
Beta Was this translation helpful? Give feedback.
I basically agree and I don't have much of a solution to offer.
I suffer similar things on other ends: I want great GraphQL validation (and that means: not just the types) so I too do also make use of the
rules
. But GraphQL is just a layer, the business logic is spread over multiple modules, each which have to validate the input anyway again. So I end up trying my best to "share" the validations but that's not always possible with repeatable and dependable fields.And I agree also in your scenario, it gets less useful if you can't couple the
Input
in some way and doing it in the query/mutationresolve
needs implicit knowledge what the input is.The ability to have
rules
directly being val…