Skip to content

Commit

Permalink
fix: resolve Boolean values correctly
Browse files Browse the repository at this point in the history
When resolving `Boolean` values from arrays or objects `false` was
incorrectly filtered out, resulting in a `null` value instead.
  • Loading branch information
wecc committed Jun 20, 2019
1 parent b5e987d commit b8483e8
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 2 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

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


## [2.0.0] - 2019-05-17

Expand Down
8 changes: 6 additions & 2 deletions src/Concerns/HandlesGraphqlRequests.php
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,9 @@ public function fieldFromArray($source, $args, $context, ResolveInfo $info)
->map(function ($propertyName) use ($source) {
return $source[$propertyName] ?? null;
})
->filter()
->reject(function ($value) {
return is_null($value);
})
->first();
}
}
Expand All @@ -170,7 +172,9 @@ public function fieldFromObject($source, $args, $context, ResolveInfo $info)
->map(function ($propertyName) use ($source) {
return $source->{$propertyName} ?? null;
})
->filter()
->reject(function ($value) {
return is_null($value);
})
->first();
}
}
Expand Down
27 changes: 27 additions & 0 deletions tests/HandlesGraphqlRequestsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,33 @@ public function test_resolve_interface_from_type()
);
}

public function test_resolves_false_correctly()
{
$controller = $this->app->make(GraphqlController::class);
$data = $controller(Request::create('/', 'POST', [
'query' => 'query {
resolvesFalseCorrectly {
id
requiredFlag
}
}'
]));

$this->assertSame(
[
'data' => [
'resolvesFalseCorrectly' => [
['id' => '1', 'requiredFlag' => true],
['id' => '2', 'requiredFlag' => false],
['id' => '3', 'requiredFlag' => true],
['id' => '4', 'requiredFlag' => false],
],
],
],
$data
);
}

public function test_various_casing()
{
$this->app->config->set('butler.graphql.include_debug_message', true);
Expand Down
16 changes: 16 additions & 0 deletions tests/stubs/Queries/ResolvesFalseCorrectly.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace Butler\Graphql\Tests\Queries;

class ResolvesFalseCorrectly
{
public function __invoke($root, $args, $context)
{
return [
['id' => 1, 'requiredFlag' => true],
['id' => 2, 'requiredFlag' => false],
(object)['id' => 3, 'requiredFlag' => true],
(object)['id' => 4, 'requiredFlag' => false],
];
}
}
6 changes: 6 additions & 0 deletions tests/stubs/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ type Query {
resolveInterfaceFromQuery: [Attachment!]!
resolveInterfaceFromType: Thing!
variousCasing: [VariousCasing!]!
resolvesFalseCorrectly: [AnotherThing!]!
}

type Mutation {
Expand Down Expand Up @@ -37,6 +38,11 @@ type SubThing {
name: String!
}

type AnotherThing {
id: ID!
requiredFlag: Boolean!
}

interface Attachment {
name: String!
size: Int!
Expand Down

0 comments on commit b8483e8

Please sign in to comment.