Skip to content

Commit 7ff9790

Browse files
fix(laravel): allow boolean cast (#6808)
1 parent 7745900 commit 7ff9790

File tree

5 files changed

+44
-2
lines changed

5 files changed

+44
-2
lines changed

src/Laravel/Eloquent/Metadata/Factory/Property/EloquentPropertyMetadataFactory.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ public function create(string $resourceClass, string $property, array $options =
7777
$type = match ($builtinType) {
7878
'integer' => new Type(Type::BUILTIN_TYPE_INT, $p['nullable']),
7979
'double', 'real' => new Type(Type::BUILTIN_TYPE_FLOAT, $p['nullable']),
80+
'boolean', 'bool' => new Type(Type::BUILTIN_TYPE_BOOL, $p['nullable']),
8081
'datetime', 'date', 'timestamp' => new Type(Type::BUILTIN_TYPE_OBJECT, $p['nullable'], \DateTime::class),
8182
'immutable_datetime', 'immutable_date' => new Type(Type::BUILTIN_TYPE_OBJECT, $p['nullable'], \DateTimeImmutable::class),
8283
'collection', 'encrypted:collection' => new Type(Type::BUILTIN_TYPE_ITERABLE, $p['nullable'], Collection::class, true),

src/Laravel/Tests/GraphQlTest.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,4 +66,38 @@ public function testGetBooksWithPaginationAndOrder(): void
6666
$this->assertCount(3, $data['data']['books']['edges']);
6767
$this->assertArrayNotHasKey('errors', $data);
6868
}
69+
70+
public function testCreateBook(): void
71+
{
72+
/** @var \Workbench\App\Models\Author $author */
73+
$author = AuthorFactory::new()->create();
74+
$response = $this->postJson('/api/graphql', [
75+
'query' => '
76+
mutation createBook($book: createBookInput!){
77+
createBook(input: $book){
78+
book{
79+
name
80+
isAvailable
81+
}
82+
}
83+
}
84+
',
85+
'variables' => [
86+
'book' => [
87+
'name' => fake()->name(),
88+
'author' => 'api/authors/'.$author->id,
89+
'isbn' => fake()->isbn13(),
90+
'isAvailable' => 1 === random_int(0, 1),
91+
],
92+
],
93+
], ['accept' => ['application/json']]);
94+
$response->assertStatus(200);
95+
$data = $response->json();
96+
$this->assertArrayNotHasKey('errors', $data);
97+
$this->assertArrayHasKey('data', $data);
98+
$this->assertArrayHasKey('createBook', $data['data']);
99+
$this->assertArrayHasKey('book', $data['data']['createBook']);
100+
$this->assertArrayHasKey('isAvailable', $data['data']['createBook']['book']);
101+
$this->assertIsBool($data['data']['createBook']['book']['isAvailable']);
102+
}
69103
}

src/Laravel/workbench/app/Models/Book.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
use ApiPlatform\Metadata\Delete;
2424
use ApiPlatform\Metadata\Get;
2525
use ApiPlatform\Metadata\GetCollection;
26+
use ApiPlatform\Metadata\GraphQl\Mutation;
2627
use ApiPlatform\Metadata\GraphQl\Query;
2728
use ApiPlatform\Metadata\GraphQl\QueryCollection;
2829
use ApiPlatform\Metadata\Patch;
@@ -55,6 +56,7 @@
5556
new QueryParameter(key: 'order[:property]', filter: OrderFilter::class),
5657
],
5758
),
59+
new Mutation(name: 'create'),
5860
]
5961
)]
6062
#[QueryParameter(key: 'isbn', filter: PartialSearchFilter::class, constraints: 'min:2')]
@@ -74,8 +76,11 @@ class Book extends Model
7476
use HasFactory;
7577
use HasUlids;
7678

77-
protected $visible = ['name', 'author', 'isbn', 'publication_date'];
78-
protected $fillable = ['name'];
79+
protected $visible = ['name', 'author', 'isbn', 'publication_date', 'is_available'];
80+
protected $fillable = ['name', 'is_available'];
81+
protected $casts = [
82+
'is_available' => 'boolean',
83+
];
7984

8085
public function author(): BelongsTo
8186
{

src/Laravel/workbench/database/factories/BookFactory.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ public function definition(): array
3737
'author_id' => AuthorFactory::new(),
3838
'isbn' => fake()->isbn13(),
3939
'publication_date' => fake()->optional()->date(),
40+
'is_available' => 1 === random_int(0, 1),
4041
'internal_note' => fake()->text(),
4142
];
4243
}

src/Laravel/workbench/database/migrations/2023_07_15_231244_create_book_table.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ public function up(): void
3232
$table->string('name');
3333
$table->string('isbn');
3434
$table->date('publication_date')->nullable();
35+
$table->boolean('is_available')->default(true);
3536
$table->text('internal_note')->nullable();
3637
$table->integer('author_id')->unsigned();
3738
$table->foreign('author_id')->references('id')->on('authors');

0 commit comments

Comments
 (0)