1. Go to this page and download the library: Download bitsmind/graphsql library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
bitsmind / graphsql example snippets
public function category():BelongsTo
{
return $this->belongsTo(Category::class, 'category_id', 'id');
}
public function variations (): HasMany
{
return $this->hasMany(ProductVariation::class, 'product_id', 'id');
}
namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
use App\Http\Services\ProductService;
class ProductController extends Controller
{
function __construct (private readonly ProductService $service) {}
public function getList ()
{
return response()->json( $this->service->getList());
}
public function getSingle ($id)
{
return response()->json( $this->service->getProduct($id));
}
namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
use App\Http\Services\ProductService;
use Illuminate\Http\Request;
class ProductController extends Controller
{
function __construct (private readonly ProductService $service) {}
public function getList (Request $request): JsonResponse
{
return response()->json( $this->service->getList( $request->query()));
}
public function getSingle ($id, Request $request)
{
return response()->json( $this->service->getProduct($id, $request->query()));
}
use Bitsmind\GraphSql\Facades\QueryAssist;
class ProductService extends Service
{
public function getList (array $query): array
{
try {
$dbQuery = Product::query();
$dbQuery = QueryAssist::queryGraphSQL($dbQuery, $query, new Product);
$products = $dbQuery->get();
return [
'success' => true,
'data' => ['products' => $products]
];
}
catch (\Exception $exception) {
return [
'success' => false,
'message' => $exception->getMessage()
];
}
}
.
.
.
}