Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions docs/master/api-reference/directives.md
Original file line number Diff line number Diff line change
Expand Up @@ -4362,6 +4362,10 @@ Make sure it has exactly two required fields to ensure the query is valid.

The documentation for this directive is found in [`Complex Where Conditions`](../eloquent/complex-where-conditions.md#whereconditions).

## @whereDoesntHaveConditions

The documentation for this directive is found in [`Complex Where Conditions`](../eloquent/complex-where-conditions.md#wheredoesnthaveconditions).

## @whereHasConditions

The documentation for this directive is found in [`Complex Where Conditions`](../eloquent/complex-where-conditions.md#wherehasconditions).
Expand Down
81 changes: 80 additions & 1 deletion docs/master/eloquent/complex-where-conditions.md
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,85 @@ This query would retrieve all persons, no matter if they have a role or not:

Lighthouse qualifies column names with the table of the related model, unless they already contain a table name.

### @whereDoesntHaveConditions

```graphql
"""
Allow clients to filter a query by the absence of a related model.
A dynamically controlled `WHERE` condition applies to the relationship.
"""
directive @whereDoesntHaveConditions(
"""
The Eloquent relationship that the conditions will be applied to.

This argument can be omitted if the argument name follows the naming convention `doesntHave{$RELATION}`.
For example, if the Eloquent relationship is named `posts`, the argument name must be `doesntHavePosts`.
"""
relation: String

"""
Restrict the allowed column names to a well-defined list.
This improves introspection capabilities and security.
Mutually exclusive with `columnsEnum`.
"""
columns: [String!]

"""
Use an existing enumeration type to restrict the allowed columns to a predefined list.
This allows you to re-use the same enum for multiple fields.
Mutually exclusive with `columns`.
"""
columnsEnum: String

"""
Reference a method that applies the client given conditions to the query builder.

Expected signature: `(
\Illuminate\Database\Query\Builder|\Illuminate\Database\Eloquent\Builder $builder,
array<string, mixed> $whereConditions
): void`

Consists of two parts: a class name and a method name, separated by an `@` symbol.
If you pass only a class name, the method name defaults to `__invoke`.
"""
handler: String = "\\Nuwave\\Lighthouse\\WhereConditions\\WhereConditionsHandler"
) on ARGUMENT_DEFINITION
```

This directive works like [@whereHasConditions](#wherehasconditions), except that it filters for the absence of related models that match the conditions:

```graphql
type Query {
people(
doesntHaveRole: _
@whereDoesntHaveConditions(columns: ["name", "access_level"])
): [Person!]! @all
}
```

A query for persons who do not have a role with an access level of at least 5 looks like this:

```graphql
{
people(doesntHaveRole: { column: ACCESS_LEVEL, operator: GTE, value: 5 }) {
name
}
}
```

The result also includes persons that do not have any role.
To retrieve only those, use an empty object as argument value:

```graphql
{
people(doesntHaveRole: {}) {
name
}
}
```

Using `null` as argument value does not have any effect on the query.

## Custom operator

If Lighthouse's default `SQLOperator` does not fit your use case, you can register a custom operator class.
Expand Down Expand Up @@ -407,7 +486,7 @@ Make sure to add it after Lighthouse's service provider:

## Custom handler

If you want to take advantage of the schema generation that [@whereConditions](#whereconditions) and [@whereHasConditions](#wherehasconditions) provide.
If you want to take advantage of the schema generation that [@whereConditions](#whereconditions), [@whereHasConditions](#wherehasconditions) and [@whereDoesntHaveConditions](#wheredoesnthaveconditions) provide.
Customize the application of arguments to the query builder, you can provide a custom handler.

```graphql
Expand Down
67 changes: 67 additions & 0 deletions src/WhereConditions/WhereDoesntHaveConditionsDirective.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php declare(strict_types=1);

namespace Nuwave\Lighthouse\WhereConditions;

class WhereDoesntHaveConditionsDirective extends WhereHasConditionsDirective
{
public static function definition(): string
{
return /** @lang GraphQL */ <<<'GRAPHQL'
"""
Allow clients to filter a query by the absence of a related model.
A dynamically controlled `WHERE` condition applies to the relationship.
"""
directive @whereDoesntHaveConditions(
"""
The Eloquent relationship that the conditions will be applied to.

This argument can be omitted if the argument name follows the naming convention `doesntHave{$RELATION}`.
For example, if the Eloquent relationship is named `posts`, the argument name must be `doesntHavePosts`.
"""
relation: String

"""
Restrict the allowed column names to a well-defined list.
This improves introspection capabilities and security.
Mutually exclusive with `columnsEnum`.
"""
columns: [String!]

"""
Use an existing enumeration type to restrict the allowed columns to a predefined list.
This allows you to re-use the same enum for multiple fields.
Mutually exclusive with `columns`.
"""
columnsEnum: String

"""
Reference a method that applies the client given conditions to the query builder.

Expected signature: `(
\Illuminate\Database\Query\Builder|\Illuminate\Database\Eloquent\Builder $builder,
array<string, mixed> $whereConditions
): void`

Consists of two parts: a class name and a method name, separated by an `@` symbol.
If you pass only a class name, the method name defaults to `__invoke`.
"""
handler: String = "\\Nuwave\\Lighthouse\\WhereConditions\\WhereConditionsHandler"
) on ARGUMENT_DEFINITION
GRAPHQL;
}

protected function relationNamePrefix(): string
{
return 'doesntHave';
}

protected function amountOperator(): string
{
return '<';
}

protected function generatedInputSuffix(): string
{
return 'WhereDoesntHaveConditions';
}
}
16 changes: 14 additions & 2 deletions src/WhereConditions/WhereHasConditionsDirective.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public function handleBuilder(QueryBuilder|EloquentBuilder|Relation $builder, $v
'HAS' => [
'relation' => $this->relationName(),
'amount' => WhereConditionsServiceProvider::DEFAULT_HAS_AMOUNT,
'operator' => '>=',
'operator' => $this->amountOperator(),
'condition' => $value,
],
],
Expand All @@ -89,13 +89,25 @@ protected function relationName(): string
// If the relation name is not set explicitly, we assume the argument name follows a convention and contains the relation name
if (is_null($relationName)) {
return lcfirst(
Str::after($this->nodeName(), 'has'),
Str::after($this->nodeName(), $this->relationNamePrefix()),
);
}

return $relationName;
}

/** Get the prefix of argument names that follow the naming convention for the relation name. */
protected function relationNamePrefix(): string
{
return 'has';
}

/** Get the comparison operator to test the number of related models that match the conditions against the amount. */
protected function amountOperator(): string
{
return '>=';
}

protected function generatedInputSuffix(): string
{
return 'WhereHasConditions';
Expand Down
Loading
Loading