1 |
<?php
|
|
2 |
|
|
3 |
namespace Nuwave\Lighthouse\Schema\Directives; |
|
4 |
|
|
5 |
use GraphQL\Type\Definition\ResolveInfo; |
|
6 |
use Nuwave\Lighthouse\Execution\DataLoader\RelationLoader; |
|
7 |
use Nuwave\Lighthouse\Execution\DataLoader\SimpleRelationLoader; |
|
8 |
use Nuwave\Lighthouse\Support\Contracts\FieldMiddleware; |
|
9 |
|
|
10 |
class WithDirective extends WithRelationDirective implements FieldMiddleware |
|
11 |
{
|
|
12 | 1 |
public static function definition(): string |
13 |
{
|
|
14 |
return /** @lang GraphQL */ <<<'GRAPHQL' |
|
15 | 1 |
"""
|
16 |
Eager-load an Eloquent relation.
|
|
17 |
"""
|
|
18 |
directive @with(
|
|
19 |
"""
|
|
20 |
Specify the relationship method name in the model class,
|
|
21 |
if it is named different from the field in the schema.
|
|
22 |
"""
|
|
23 |
relation: String
|
|
24 |
|
|
25 |
"""
|
|
26 |
Apply scopes to the underlying query.
|
|
27 |
"""
|
|
28 |
scopes: [String!]
|
|
29 |
) repeatable on FIELD_DEFINITION
|
|
30 |
GRAPHQL; |
|
31 |
}
|
|
32 |
|
|
33 | 1 |
protected function relationName(): string |
34 |
{
|
|
35 | 1 |
return $this->directiveArgValue('relation') |
36 | 1 |
?? $this->nodeName(); |
37 |
}
|
|
38 |
|
|
39 | 1 |
protected function relationLoader(ResolveInfo $resolveInfo): RelationLoader |
40 |
{
|
|
41 | 1 |
return new SimpleRelationLoader( |
42 | 1 |
$this->decorateBuilder($resolveInfo) |
43 |
);
|
|
44 |
}
|
|
45 |
}
|
Read our documentation on viewing source code .