Graphql-laravel: Subquery args validations

Created on 13 Mar 2020  路  1Comment  路  Source: rebing/graphql-laravel

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

SelectFields question

Most helpful comment

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

>All comments

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

Was this page helpful?
0 / 5 - 0 ratings

Related issues

constantinosergiou picture constantinosergiou  路  3Comments

benjamin-ndc picture benjamin-ndc  路  3Comments

niiapa picture niiapa  路  8Comments

timothyvictor picture timothyvictor  路  3Comments

nozols picture nozols  路  8Comments