From d2fdb8f9f2f37e668751d092be7124c4432d33d5 Mon Sep 17 00:00:00 2001 From: Benedikt Franke Date: Sat, 9 Sep 2023 12:21:32 +0200 Subject: [PATCH] Handle explicit `null` in `@in` directive (#2445) --- CHANGELOG.md | 6 + src/Schema/Directives/InDirective.php | 4 + .../Schema/Directives/InDirectiveTest.php | 130 ++++++++++++++++++ 3 files changed, 140 insertions(+) create mode 100644 tests/Integration/Schema/Directives/InDirectiveTest.php diff --git a/CHANGELOG.md b/CHANGELOG.md index 9fe56d2057..043853e340 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/src/Schema/Directives/InDirective.php b/src/Schema/Directives/InDirective.php index 9e44c855f7..b158c7145a 100644 --- a/src/Schema/Directives/InDirective.php +++ b/src/Schema/Directives/InDirective.php @@ -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, diff --git a/tests/Integration/Schema/Directives/InDirectiveTest.php b/tests/Integration/Schema/Directives/InDirectiveTest.php new file mode 100644 index 0000000000..818c93efea --- /dev/null +++ b/tests/Integration/Schema/Directives/InDirectiveTest.php @@ -0,0 +1,130 @@ +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'); + } +}