Skip to content
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

Subquery args validations #602

Closed
illambo opened this issue Mar 13, 2020 · 2 comments · Fixed by #608 or #630
Closed

Subquery args validations #602

illambo opened this issue Mar 13, 2020 · 2 comments · Fixed by #608 or #630

Comments

@illambo
Copy link
Contributor

illambo commented Mar 13, 2020

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

@crissi
Copy link
Contributor

crissi commented Mar 14, 2020

confirmed that it does not seem to work. I will try take a deeper look

@ravnos
Copy link

ravnos commented Oct 14, 2021

I have the same problem here with the latest version when trying to use these nested validations with a PaginationType query, as a workaround I've extended validateFieldArguments in the query like this:

public function validateFieldArguments(array $fieldsAndArgumentsSelection): void
    {
        $argsRules = (new RulesInFields(GraphQL::type('mytype'), $fieldsAndArgumentsSelection['data']['fields']))->get();

        if (count($argsRules)) {
            $validator = $this->getValidator($fieldsAndArgumentsSelection['data']['fields'], $argsRules);

            if ($validator->fails()) {
                throw new ValidationError('validation', $validator);
            }
        }
    }

I'm not sure if we should take that into account in RulesInFields or if it the PaginationType should be responsible for dealing with it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
4 participants