Skip to content

Commit

Permalink
fix(laravel): allow boolean cast (#6808)
Browse files Browse the repository at this point in the history
  • Loading branch information
amermchaudhary authored Nov 20, 2024
1 parent 7745900 commit 7ff9790
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ public function create(string $resourceClass, string $property, array $options =
$type = match ($builtinType) {
'integer' => new Type(Type::BUILTIN_TYPE_INT, $p['nullable']),
'double', 'real' => new Type(Type::BUILTIN_TYPE_FLOAT, $p['nullable']),
'boolean', 'bool' => new Type(Type::BUILTIN_TYPE_BOOL, $p['nullable']),
'datetime', 'date', 'timestamp' => new Type(Type::BUILTIN_TYPE_OBJECT, $p['nullable'], \DateTime::class),
'immutable_datetime', 'immutable_date' => new Type(Type::BUILTIN_TYPE_OBJECT, $p['nullable'], \DateTimeImmutable::class),
'collection', 'encrypted:collection' => new Type(Type::BUILTIN_TYPE_ITERABLE, $p['nullable'], Collection::class, true),
Expand Down
34 changes: 34 additions & 0 deletions src/Laravel/Tests/GraphQlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,38 @@ public function testGetBooksWithPaginationAndOrder(): void
$this->assertCount(3, $data['data']['books']['edges']);
$this->assertArrayNotHasKey('errors', $data);
}

public function testCreateBook(): void
{
/** @var \Workbench\App\Models\Author $author */
$author = AuthorFactory::new()->create();
$response = $this->postJson('/api/graphql', [
'query' => '
mutation createBook($book: createBookInput!){
createBook(input: $book){
book{
name
isAvailable
}
}
}
',
'variables' => [
'book' => [
'name' => fake()->name(),
'author' => 'api/authors/'.$author->id,
'isbn' => fake()->isbn13(),
'isAvailable' => 1 === random_int(0, 1),
],
],
], ['accept' => ['application/json']]);
$response->assertStatus(200);
$data = $response->json();
$this->assertArrayNotHasKey('errors', $data);
$this->assertArrayHasKey('data', $data);
$this->assertArrayHasKey('createBook', $data['data']);
$this->assertArrayHasKey('book', $data['data']['createBook']);
$this->assertArrayHasKey('isAvailable', $data['data']['createBook']['book']);
$this->assertIsBool($data['data']['createBook']['book']['isAvailable']);
}
}
9 changes: 7 additions & 2 deletions src/Laravel/workbench/app/Models/Book.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
use ApiPlatform\Metadata\Delete;
use ApiPlatform\Metadata\Get;
use ApiPlatform\Metadata\GetCollection;
use ApiPlatform\Metadata\GraphQl\Mutation;
use ApiPlatform\Metadata\GraphQl\Query;
use ApiPlatform\Metadata\GraphQl\QueryCollection;
use ApiPlatform\Metadata\Patch;
Expand Down Expand Up @@ -55,6 +56,7 @@
new QueryParameter(key: 'order[:property]', filter: OrderFilter::class),
],
),
new Mutation(name: 'create'),
]
)]
#[QueryParameter(key: 'isbn', filter: PartialSearchFilter::class, constraints: 'min:2')]
Expand All @@ -74,8 +76,11 @@ class Book extends Model
use HasFactory;
use HasUlids;

protected $visible = ['name', 'author', 'isbn', 'publication_date'];
protected $fillable = ['name'];
protected $visible = ['name', 'author', 'isbn', 'publication_date', 'is_available'];
protected $fillable = ['name', 'is_available'];
protected $casts = [
'is_available' => 'boolean',
];

public function author(): BelongsTo
{
Expand Down
1 change: 1 addition & 0 deletions src/Laravel/workbench/database/factories/BookFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public function definition(): array
'author_id' => AuthorFactory::new(),
'isbn' => fake()->isbn13(),
'publication_date' => fake()->optional()->date(),
'is_available' => 1 === random_int(0, 1),
'internal_note' => fake()->text(),
];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public function up(): void
$table->string('name');
$table->string('isbn');
$table->date('publication_date')->nullable();
$table->boolean('is_available')->default(true);
$table->text('internal_note')->nullable();
$table->integer('author_id')->unsigned();
$table->foreign('author_id')->references('id')->on('authors');
Expand Down

0 comments on commit 7ff9790

Please sign in to comment.