diff --git a/src/Laravel/Eloquent/Metadata/Factory/Property/EloquentPropertyMetadataFactory.php b/src/Laravel/Eloquent/Metadata/Factory/Property/EloquentPropertyMetadataFactory.php index c88c96585e2..6385a83568c 100644 --- a/src/Laravel/Eloquent/Metadata/Factory/Property/EloquentPropertyMetadataFactory.php +++ b/src/Laravel/Eloquent/Metadata/Factory/Property/EloquentPropertyMetadataFactory.php @@ -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), diff --git a/src/Laravel/Tests/GraphQlTest.php b/src/Laravel/Tests/GraphQlTest.php index 2c3889b84e1..c8044ec12fd 100644 --- a/src/Laravel/Tests/GraphQlTest.php +++ b/src/Laravel/Tests/GraphQlTest.php @@ -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']); + } } diff --git a/src/Laravel/workbench/app/Models/Book.php b/src/Laravel/workbench/app/Models/Book.php index 33bcf093424..421d21bd7ff 100644 --- a/src/Laravel/workbench/app/Models/Book.php +++ b/src/Laravel/workbench/app/Models/Book.php @@ -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; @@ -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')] @@ -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 { diff --git a/src/Laravel/workbench/database/factories/BookFactory.php b/src/Laravel/workbench/database/factories/BookFactory.php index 65af45ad5ae..d94b3f281c9 100644 --- a/src/Laravel/workbench/database/factories/BookFactory.php +++ b/src/Laravel/workbench/database/factories/BookFactory.php @@ -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(), ]; } diff --git a/src/Laravel/workbench/database/migrations/2023_07_15_231244_create_book_table.php b/src/Laravel/workbench/database/migrations/2023_07_15_231244_create_book_table.php index 398e576c3f0..0137554453c 100644 --- a/src/Laravel/workbench/database/migrations/2023_07_15_231244_create_book_table.php +++ b/src/Laravel/workbench/database/migrations/2023_07_15_231244_create_book_table.php @@ -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');