1
<?php
2

3
namespace Nuwave\Lighthouse\Schema\Directives;
4

5
use BadMethodCallException;
6
use Nuwave\Lighthouse\Exceptions\DefinitionException;
7
use Nuwave\Lighthouse\Support\Contracts\ArgBuilderDirective;
8

9
class ScopeDirective extends BaseDirective implements ArgBuilderDirective
10
{
11 1
    public static function definition(): string
12
    {
13
        return /** @lang GraphQL */ <<<'GRAPHQL'
14 1
"""
15
Adds a scope to the query builder.
16

17
The scope method will receive the client-given value of the argument as the second parameter.
18
"""
19
directive @scope(
20
  """
21
  The name of the scope.
22
  """
23
  name: String!
24
) repeatable on ARGUMENT_DEFINITION | INPUT_FIELD_DEFINITION
25
GRAPHQL;
26
    }
27

28
    /**
29
     * Add additional constraints to the builder based on the given argument value.
30
     *
31
     * @param  \Illuminate\Database\Query\Builder|\Illuminate\Database\Eloquent\Builder  $builder
32
     * @return \Illuminate\Database\Query\Builder|\Illuminate\Database\Eloquent\Builder
33
     *
34
     * @throws \Nuwave\Lighthouse\Exceptions\DefinitionException
35
     */
36 1
    public function handleBuilder($builder, $value): object
37
    {
38 1
        $scope = $this->directiveArgValue('name');
39
        try {
40 1
            return $builder->{$scope}($value);
41 1
        } catch (BadMethodCallException $exception) {
42 1
            throw new DefinitionException(
43 1
                $exception->getMessage()." in @{$this->name()} directive on {$this->nodeName()} argument.",
44 1
                $exception->getCode(),
45 1
                $exception->getPrevious()
46
            );
47
        }
48
    }
49
}

Read our documentation on viewing source code .

Loading