Install the Package
composer require zatona-eg/zatona-eg-api
php artisan zatona-eg-api:install
Access the API
/api/shop/docs (shop) or /api/admin/docs (admin) for Swagger/OpenAPI docs./api/graphiql (shop) or /api/admin/graphiql (admin).First Use Case: Fetch Products
GET /api/shop/products (returns paginated product list).query {
products {
id
name
slug
}
}
app/Http/Resources/ (customize REST responses).config/graphql.php (extend types/resolvers).app/Http/Middleware/ (auth/validation layers).php artisan make:resource to extend default resources (e.g., ProductResource).
Example:
// app/Http/Resources/ProductResource.php
public function toArray($request, Product $product) {
return [
'id' => $product->id,
'name' => $product->name,
'custom_field' => $product->customField, // Add custom attributes
];
}
?filter[name][contains]=phone).
Override in Product entity:
#[ApiFilter(SearchFilter::class, properties: ['name', 'description'])]
class Product extends Model {}
config/graphql.php:
'types' => [
'Product' => [
'fields' => [
'customField' => [
'type' => 'String',
'resolve' => fn ($product) => $product->customField,
],
],
],
],
createProduct):
// app/GraphQL/Mutations/CreateProduct.php
class CreateProduct implements Mutation {
public function resolve() {
return Product::create($this->args['input']);
}
}
php artisan make:auth to generate API tokens for clients.
Protect routes in routes/api.php:
Route::middleware('auth:api')->group(function () {
Route::apiResource('orders', OrderController::class);
});
/api/shop/ (public) or /api/admin/ (admin-only).Http tests:
$response = $this->getJson('/api/shop/products');
$response->assertStatus(200);
graphql helper:
$result = $this->graphql('query { products { id name } }');
$result->assertNoErrors();
Version Mismatch:
composer.json for bagisto/bagisto version conflicts.GraphQL Caching:
php artisan graphql:cache) can cause issues if resolvers change.php artisan graphql:clear-cache
REST vs. GraphQL Duplication:
ProductService) for business logic.Pagination Limits:
ProductResource:
public function toArray($request) {
return $this->collection->through(fn ($product) => [...])->paginate(50);
}
REST Errors:
Check storage/logs/laravel.log for API Platform errors (e.g., validation failures).
Enable debug mode in config/api_platform.php:
'debug' => env('APP_DEBUG', true),
GraphQL Errors: Use the GraphQL Playground to inspect errors. For resolver issues, add:
try {
return $this->resolve();
} catch (\Exception $e) {
throw new GraphQLException($e->getMessage());
}
Custom API Platform State Providers:
Extend ApiStateProvider to modify API responses globally:
// app/Providers/ApiStateProvider.php
public function getState($resourceClass, array $collection = []): array {
return array_merge(parent::getState($resourceClass, $collection), [
'custom_metadata' => 'value',
]);
}
Webhook Integration:
Use api-platform/core events to trigger actions (e.g., on product creation):
// app/Providers/EventServiceProvider.php
public function boot() {
event(new ProductCreated($product));
}
Performance:
#[ApiResource(itemOperations: ['get'])] to disable unnecessary endpoints.#[ApiResource(useResultPager: true)].CORS:
Ensure config/cors.php allows API domains:
'paths' => ['api/*', 'graphiql'],
'allowed_methods' => ['*'],
'allowed_origins' => ['*'], // Restrict in production!
Rate Limiting:
Enable in app/Http/Kernel.php:
\Illuminate\Cache\RateLimiting\Limit::perMinute(60)->by($this->resolveRequest()->ip());
How can I help you explore Laravel packages today?