-
-
Notifications
You must be signed in to change notification settings - Fork 270
Labels
Description
Versions:
- graphql-laravel Version: 5.0.0-rc2
- Laravel Version: 6.18.1
- PHP Version: 7.2.22
Question:
How is it possible to validate the subquery args parameters?
Example:
class UserType extends GraphQLType
{
// ...
public function fields(): array
{
return [
// ...
// Relation
'posts' => [
'type' => Type::listOf(GraphQL::type('post')),
'description' => 'A list of posts written by the user',
'args' => [
'date_from' => [
'type' => Type::string(),
'rules' => [ // this parameter has no effect
'filled',
'date'
]
],
],
'rules' => [ // this parameter has no effect
'date_from' => [
'filled',
'date'
]
],
// $args are the local arguments passed to the relation
// $query is the relation builder object
// $ctx is the GraphQL context (can be customized by overriding `\Rebing\GraphQL\GraphQLController::queryContext`
'query' => function(array $args, $query, $ctx) {
return $query->where('posts.created_at', '>', $args['date_from']);
}
]
];
}
}
Current solution (verbose):
class UserType extends GraphQLType
{
// ...
public function fields(): array
{
return [
// ...
// Relation
'posts' => [
'type' => Type::listOf(GraphQL::type('post')),
'description' => 'A list of posts written by the user',
'args' => [
'date_from' => [
'type' => Type::string(),
],
],
// $args are the local arguments passed to the relation
// $query is the relation builder object
// $ctx is the GraphQL context (can be customized by overriding `\Rebing\GraphQL\GraphQLController::queryContext`
'query' => function(array $args, $query, $ctx) {
$validator = \Illuminate\Support\Facades\Validator::make($args, [
'date_from' => [
'filled',
'date'
]
]);
if ($validator->fails()) throw new \Rebing\GraphQL\Error\ValidationError('validation', $validator);
return $query->where('posts.created_at', '>', $args['date_from']);
}
]
];
}
}
There is a better way?
Thanks