1
<?php
2

3
namespace Nuwave\Lighthouse\Schema\Directives;
4

5
use GraphQL\Type\Definition\ResolveInfo;
6
use Illuminate\Database\Eloquent\Collection;
7
use Nuwave\Lighthouse\Schema\Values\FieldValue;
8
use Nuwave\Lighthouse\Support\Contracts\FieldResolver;
9
use Nuwave\Lighthouse\Support\Contracts\GraphQLContext;
10

11
class AllDirective extends BaseDirective implements FieldResolver
12
{
13 1
    public static function definition(): string
14
    {
15
        return /** @lang GraphQL */ <<<'GRAPHQL'
16 1
"""
17
Fetch all Eloquent models and return the collection as the result.
18
"""
19
directive @all(
20
  """
21
  Specify the class name of the model to use.
22
  This is only needed when the default model detection does not work.
23
  """
24
  model: String
25

26
  """
27
  Apply scopes to the underlying query.
28
  """
29
  scopes: [String!]
30
) on FIELD_DEFINITION
31
GRAPHQL;
32
    }
33

34 1
    public function resolveField(FieldValue $fieldValue): FieldValue
35
    {
36 1
        return $fieldValue->setResolver(
37
            function ($root, array $args, GraphQLContext $context, ResolveInfo $resolveInfo): Collection {
38
                return $resolveInfo
39 1
                    ->argumentSet
40 1
                    ->enhanceBuilder(
41 1
                        $this->getModelClass()::query(),
42 1
                        $this->directiveArgValue('scopes', [])
43
                    )
44 1
                    ->get();
45
            }
46
        );
47
    }
48
}

Read our documentation on viewing source code .

Loading