Skip to content
Merged
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
29 changes: 29 additions & 0 deletions src/Fields/Field.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ class Field
*/
protected $nullable = false;

/**
* @var bool
*/
protected $nullableItems = false;

/**
* @var bool
*/
Expand Down Expand Up @@ -138,6 +143,7 @@ public function getType(): Type

$type->nullable($this->isNullable());
$type->list($this->isList());
$type->nullableItems($this->hasNullableItems());

return $type->setRegistry($this->getRegistry());
}
Expand Down Expand Up @@ -265,6 +271,29 @@ public function isNullable(): bool
return $this->nullable;
}

/**
* Set if the field has nullable items.
*
* @param bool $nullable
* @return \Bakery\Fields\Field
*/
public function nullableItems(bool $nullable = true): self
{
$this->nullableItems = $nullable;

return $this;
}

/**
* Return if the field has nullable items.
*
* @return bool
*/
public function hasNullableItems(): bool
{
return $this->nullableItems;
}

/**
* Set if the field is fillable.
*
Expand Down
2 changes: 1 addition & 1 deletion src/Types/CollectionFilterType.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public function fields(): array
->put('OR', $this->registry->field($this->registry->type($this->name()))->list());

return $fields->map(function (Field $type) {
return $type->nullable();
return $type->nullable()->nullableItems();
})->toArray();
}

Expand Down
2 changes: 1 addition & 1 deletion src/Types/Definitions/Type.php
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ public function hasNullableItems(): bool
*
* @param bool $value
*/
public function setNullableItems(bool $value = true)
public function nullableItems(bool $value = true)
{
$this->nullableItems = $value;
}
Expand Down
48 changes: 48 additions & 0 deletions tests/Feature/CollectionQueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,54 @@ public function it_can_filter_with_AND_and_OR_filters()
$response->assertJsonMissing(['id' => $articleThree->id]);
}

/** @test */
public function it_can_filter_with_null_AND_and_OR_filters()
{
$userOne = factory(Models\User::class)->create();
$userTwo = factory(Models\User::class)->create();
$articleOne = factory(Models\Article::class)->create([
'title' => 'Hello world',
'content' => 'Lorem ipsum',
'user_id' => $userOne->id,
]);
$articleTwo = factory(Models\Article::class)->create([
'title' => 'Hello world',
'content' => 'Lorem ipsum',
'user_id' => $userOne->id,
]);
$articleThree = factory(Models\Article::class)->create([
'title' => 'Hello world',
'content' => 'Lorem ipsum',
'user_id' => $userTwo->id,
]);

$query = '
query {
articles(filter: {
AND: [
{ user: { id: "'.$userOne->id.'" } },
null,
{ OR: [null] },
]
}) {
items {
id
}
pagination {
total
}
}
}
';

$response = $this->json('GET', '/graphql', ['query' => $query]);
$response->assertStatus(200);
$response->assertJsonFragment(['total' => 2]);
$response->assertJsonFragment(['id' => $articleOne->id]);
$response->assertJsonFragment(['id' => $articleTwo->id]);
$response->assertJsonMissing(['id' => $articleThree->id]);
}

/** @test */
public function it_can_order_by_field()
{
Expand Down