Skip to content

Commit b8483e8

Browse files
committed
fix: resolve Boolean values correctly
When resolving `Boolean` values from arrays or objects `false` was incorrectly filtered out, resulting in a `null` value instead.
1 parent b5e987d commit b8483e8

File tree

5 files changed

+58
-2
lines changed

5 files changed

+58
-2
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [Unreleased]
99

10+
### Fixed
11+
- When resolving `Boolean` values from arrays or objects `false` was incorrectly filtered out, resulting in a `null` value instead.
12+
1013

1114
## [2.0.0] - 2019-05-17
1215

src/Concerns/HandlesGraphqlRequests.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,9 @@ public function fieldFromArray($source, $args, $context, ResolveInfo $info)
158158
->map(function ($propertyName) use ($source) {
159159
return $source[$propertyName] ?? null;
160160
})
161-
->filter()
161+
->reject(function ($value) {
162+
return is_null($value);
163+
})
162164
->first();
163165
}
164166
}
@@ -170,7 +172,9 @@ public function fieldFromObject($source, $args, $context, ResolveInfo $info)
170172
->map(function ($propertyName) use ($source) {
171173
return $source->{$propertyName} ?? null;
172174
})
173-
->filter()
175+
->reject(function ($value) {
176+
return is_null($value);
177+
})
174178
->first();
175179
}
176180
}

tests/HandlesGraphqlRequestsTest.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,33 @@ public function test_resolve_interface_from_type()
351351
);
352352
}
353353

354+
public function test_resolves_false_correctly()
355+
{
356+
$controller = $this->app->make(GraphqlController::class);
357+
$data = $controller(Request::create('/', 'POST', [
358+
'query' => 'query {
359+
resolvesFalseCorrectly {
360+
id
361+
requiredFlag
362+
}
363+
}'
364+
]));
365+
366+
$this->assertSame(
367+
[
368+
'data' => [
369+
'resolvesFalseCorrectly' => [
370+
['id' => '1', 'requiredFlag' => true],
371+
['id' => '2', 'requiredFlag' => false],
372+
['id' => '3', 'requiredFlag' => true],
373+
['id' => '4', 'requiredFlag' => false],
374+
],
375+
],
376+
],
377+
$data
378+
);
379+
}
380+
354381
public function test_various_casing()
355382
{
356383
$this->app->config->set('butler.graphql.include_debug_message', true);
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace Butler\Graphql\Tests\Queries;
4+
5+
class ResolvesFalseCorrectly
6+
{
7+
public function __invoke($root, $args, $context)
8+
{
9+
return [
10+
['id' => 1, 'requiredFlag' => true],
11+
['id' => 2, 'requiredFlag' => false],
12+
(object)['id' => 3, 'requiredFlag' => true],
13+
(object)['id' => 4, 'requiredFlag' => false],
14+
];
15+
}
16+
}

tests/stubs/schema.graphql

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ type Query {
1010
resolveInterfaceFromQuery: [Attachment!]!
1111
resolveInterfaceFromType: Thing!
1212
variousCasing: [VariousCasing!]!
13+
resolvesFalseCorrectly: [AnotherThing!]!
1314
}
1415

1516
type Mutation {
@@ -37,6 +38,11 @@ type SubThing {
3738
name: String!
3839
}
3940

41+
type AnotherThing {
42+
id: ID!
43+
requiredFlag: Boolean!
44+
}
45+
4046
interface Attachment {
4147
name: String!
4248
size: Int!

0 commit comments

Comments
 (0)