Skip to content

Subquery args validations #602

@illambo

Description

@illambo

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

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions