1
<?php
2

3
namespace Nuwave\Lighthouse\Schema\Directives;
4

5
use Nuwave\Lighthouse\Support\Contracts\ArgBuilderDirective;
6

7
class WhereDirective extends BaseDirective implements ArgBuilderDirective
8
{
9 1
    public static function definition(): string
10
    {
11
        return /** @lang GraphQL */ <<<'GRAPHQL'
12 1
"""
13
Use an input value as a [where filter](https://laravel.com/docs/queries#where-clauses).
14
"""
15
directive @where(
16
  """
17
  Specify the operator to use within the WHERE condition.
18
  """
19
  operator: String = "="
20

21
  """
22
  Specify the database column to compare.
23
  Only required if database column has a different name than the attribute in your schema.
24
  """
25
  key: String
26

27
  """
28
  Use Laravel's where clauses upon the query builder.
29
  """
30
  clause: String
31
) repeatable on ARGUMENT_DEFINITION | INPUT_FIELD_DEFINITION
32
GRAPHQL;
33
    }
34

35
    /**
36
     * Add any "WHERE" clause to the builder.
37
     *
38
     * @param  \Illuminate\Database\Query\Builder|\Illuminate\Database\Eloquent\Builder  $builder
39
     * @return \Illuminate\Database\Query\Builder|\Illuminate\Database\Eloquent\Builder
40
     */
41 1
    public function handleBuilder($builder, $value): object
42
    {
43
        // Allow users to overwrite the default "where" clause, e.g. "whereYear"
44 1
        $clause = $this->directiveArgValue('clause', 'where');
45

46 1
        return $builder->{$clause}(
47 1
            $this->directiveArgValue('key', $this->nodeName()),
48 1
            $operator = $this->directiveArgValue('operator', '='),
49
            $value
50
        );
51
    }
52
}

Read our documentation on viewing source code .

Loading