1 |
<?php
|
|
2 |
|
|
3 |
namespace Nuwave\Lighthouse\WhereConditions; |
|
4 |
|
|
5 |
use Illuminate\Support\Str; |
|
6 |
|
|
7 |
class WhereHasConditionsDirective extends WhereConditionsBaseDirective |
|
8 |
{
|
|
9 |
public static function definition(): string |
|
10 |
{
|
|
11 |
return /** @lang GraphQL */ <<<'GRAPHQL' |
|
12 |
"""
|
|
13 |
Allows clients to filter a query based on the existence of a related model, using
|
|
14 |
a dynamically controlled `WHERE` condition that applies to the relationship.
|
|
15 |
"""
|
|
16 |
directive @whereHasConditions(
|
|
17 |
"""
|
|
18 |
The Eloquent relationship that the conditions will be applied to.
|
|
19 |
|
|
20 |
This argument can be omitted if the argument name follows the naming
|
|
21 |
convention `has{$RELATION}`. For example, if the Eloquent relationship
|
|
22 |
is named `posts`, the argument name must be `hasPosts`.
|
|
23 |
"""
|
|
24 |
relation: String
|
|
25 |
|
|
26 |
"""
|
|
27 |
Restrict the allowed column names to a well-defined list.
|
|
28 |
This improves introspection capabilities and security.
|
|
29 |
Mutually exclusive with the `columnsEnum` argument.
|
|
30 |
"""
|
|
31 |
columns: [String!]
|
|
32 |
|
|
33 |
"""
|
|
34 |
Use an existing enumeration type to restrict the allowed columns to a predefined list.
|
|
35 |
This allowes you to re-use the same enum for multiple fields.
|
|
36 |
Mutually exclusive with the `columns` argument.
|
|
37 |
"""
|
|
38 |
columnsEnum: String
|
|
39 |
) on ARGUMENT_DEFINITION
|
|
40 |
GRAPHQL; |
|
41 |
}
|
|
42 |
|
|
43 |
/**
|
|
44 |
* @param \Illuminate\Database\Eloquent\Builder $builder The builder used to resolve the field.
|
|
45 |
* @param mixed $whereConditions The client given conditions
|
|
46 |
* @return \Illuminate\Database\Eloquent\Builder The modified builder.
|
|
47 |
*/
|
|
48 | 1 |
public function handleBuilder($builder, $whereConditions): object |
49 |
{
|
|
50 |
// The value `null` should be allowed but have no effect on the query.
|
|
51 | 1 |
if (is_null($whereConditions)) { |
52 | 1 |
return $builder; |
53 |
}
|
|
54 |
|
|
55 | 1 |
$this->handleHasCondition( |
56 | 1 |
$builder, |
57 | 1 |
$builder->getModel(), |
58 | 1 |
$this->getRelationName(), |
59 |
$whereConditions
|
|
60 |
);
|
|
61 |
|
|
62 | 1 |
return $builder; |
63 |
}
|
|
64 |
|
|
65 |
/**
|
|
66 |
* Get the name of the Eloquent relationship that is used for the query.
|
|
67 |
*/
|
|
68 | 1 |
public function getRelationName(): string |
69 |
{
|
|
70 | 1 |
$relationName = $this->directiveArgValue('relation'); |
71 |
|
|
72 |
// If the relation name is not set explicitly, we assume the argument
|
|
73 |
// name follows a convention and contains the relation name
|
|
74 | 1 |
if (is_null($relationName)) { |
75 | 1 |
$relationName = lcfirst( |
76 | 1 |
Str::after($this->nodeName(), 'has') |
77 |
);
|
|
78 |
}
|
|
79 |
|
|
80 | 1 |
return $relationName; |
81 |
}
|
|
82 |
|
|
83 | 1 |
protected function generatedInputSuffix(): string |
84 |
{
|
|
85 | 1 |
return 'WhereHasConditions'; |
86 |
}
|
|
87 |
}
|
Read our documentation on viewing source code .