Skip to content

Commit

Permalink
Handle explicit null in @in directive (#2445)
Browse files Browse the repository at this point in the history
  • Loading branch information
spawnia authored Sep 9, 2023
1 parent 9ac69a3 commit d2fdb8f
Show file tree
Hide file tree
Showing 3 changed files with 140 additions and 0 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ You can find and compare releases at the [GitHub release page](https://github.co

## Unreleased

## v6.18.1

### Fixed

- Handle explicit `null` in `@in` directive https://github.com/nuwave/lighthouse/pull/2445

## v6.18.0

### Added
Expand Down
4 changes: 4 additions & 0 deletions src/Schema/Directives/InDirective.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ public static function definition(): string

public function handleBuilder(QueryBuilder|EloquentBuilder|Relation $builder, $value): QueryBuilder|EloquentBuilder|Relation
{
if ($value === null) {
return $builder;
}

return $builder->whereIn(
$this->directiveArgValue('key', $this->nodeName()),
$value,
Expand Down
130 changes: 130 additions & 0 deletions tests/Integration/Schema/Directives/InDirectiveTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
<?php declare(strict_types=1);

namespace Schema\Directives;

use Tests\DBTestCase;
use Tests\Utils\Models\User;

final class InDirectiveTest extends DBTestCase
{
public function testInIDs(): void
{
$user1 = factory(User::class)->create();
assert($user1 instanceof User);

factory(User::class)->create();

$this->schema = /** @lang GraphQL */ '
type User {
id: ID!
}
type Query {
users(ids: [ID!] @in(key: "id")): [User!]! @all
}
';

$user1ID = (string) $user1->id;

$this
->graphQL(/** @lang GraphQL */ '
query ($ids: [ID!]) {
users(ids: $ids) {
id
}
}
', [
'ids' => [$user1ID],
])
->assertJson([
'data' => [
'users' => [
[
'id' => $user1ID,
],
],
],
]);
}

public function testExplicitNull(): void
{
$users = factory(User::class, 2)->create();

$this->schema = /** @lang GraphQL */ '
type User {
id: ID!
}
type Query {
users(ids: [ID!] @in(key: "id")): [User!]! @all
}
';

$this
->graphQL(/** @lang GraphQL */ '
query ($ids: [ID!]) {
users(ids: $ids) {
id
}
}
', [
'ids' => null,
])
->assertJsonCount($users->count(), 'data.users');
}

public function testExplicitNullInArray(): void
{
factory(User::class, 2)->create();

$this->schema = /** @lang GraphQL */ '
type User {
id: ID!
}
type Query {
users(ids: [ID] @in(key: "id")): [User!]! @all
}
';

$this
->graphQL(/** @lang GraphQL */ '
query ($ids: [ID]) {
users(ids: $ids) {
id
}
}
', [
'ids' => [null],
])
->assertJsonCount(0, 'data.users');
}

public function testEmptyArray(): void
{
factory(User::class, 2)->create();

$this->schema = /** @lang GraphQL */ '
type User {
id: ID!
}
type Query {
users(ids: [ID!] @in(key: "id")): [User!]! @all
}
';

$this
->graphQL(/** @lang GraphQL */ '
query ($ids: [ID!]) {
users(ids: $ids) {
id
}
}
', [
'ids' => [],
])
->assertJsonCount(0, 'data.users');
}
}

0 comments on commit d2fdb8f

Please sign in to comment.