PHP code example of bitsmind / graphsql

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\Services;
    
    class ProductService extends Service
    {    
        public function getList (): array
        {
            try {
                $dbQuery = Product::get();
            
                return [
                    'success' => true,
                    'data' => ['products' => $products]
                ];
            }
            catch (\Exception $exception) {
                return [
                    'success' => false,
                    'message' => $exception->getMessage()
                ];
            }
        }
    
        public function getSingle ($id): array
        {
            try {
                $dbQuery = Product::find($id);
            
                return [
                    'success' => true,
                    'data' => ['product' => $product]
                ];
            }
            catch (\Exception $exception) {
                return [
                    'success' => false,
                    'message' => $exception->getMessage()
                ];
            }
        }
    
    

php artisan serve --port=8800

    php artisan migrate
    

    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()
                ];
            }
        }
        .
        .
        .
   }
   

   use Bitsmind\GraphSql\Facades\QueryAssist;
   
   class ProductService extends Service
   {   
        public function getList (array $query): array
        {
            try {
   
                $dbQuery = Product::query();
                
                // graphSql
                $dbQuery = QueryAssist::queryGraphSQL($dbQuery, $query, new Product);  
                
                // sorting
                if (array_key_exists('order_by', $query)) {
                    [$column, $order] = explode(',',$query['order_by']);
                    $dbQuery = $dbQuery->orderby($column, $order);
                }
                else {
                    // default
                    $dbQuery = $dbQuery->orderby('id', 'desc');
                }
                
                // column filters
                if (array_key_exists('status', $query)) {
                    $dbQuery = $dbQuery->where('status', $query['status'])
                }
                if (array_key_exists('category_id', $query)) {
                    $dbQuery = $dbQuery->where('category_id', $query['category_id'])
                }
                
                // multi-options filters
                if (array_key_exists('brand', $query)) {
                    $options = explode(',', $query[$field]);
                    $dbQuery = $dbQuery->whereIn('brand', $options);
                }
                
                // pagination
                $count = $dbQuery->count();
                if (!array_key_exists('page', $query))      $query['page']      = 1;
                if (!array_key_exists('length', $query))    $query['length']    = 100;
                $offset = ($query['page']-1)*$query['length'];                
                $products = $dbQuery->offset($offset)->limit($query['length'])->get();
              
                return [
                    'success' => true,
                    'data' => [
                        'page' => $query['page'],
                        'length' => $query['length'],
                        'count' => $count,
                        'products' => $products
                    ]
                ];
            }
            catch (\Exception $exception) {
                return [
                    'success' => false,
                    'message' => $exception->getMessage()
                ];
            }
        }
   }
   

   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);           // graphSql
                $dbQuery = QueryAssist::queryOrderBy($dbQuery, $query, 'id', 'desc');           // sorting (default id,desc)
                $dbQuery = QueryAssist::queryWhere($dbQuery, $query, ['status','category_id']); // column filters
                $dbQuery = QueryAssist::queryWhereIn($dbQuery, $query, ['brand']);              // multi-option filters
                
                $count = $dbQuery->count();
                $products = QueryAssist::queryPagination($dbQuery, $query)->get();              // pagination
              
                return [
                    'success' => true,
                    'data' => [
                        'page' => $query['page'],
                        'length' => $query['length'],
                        'count' => $count,
                        'products' => $products
                    ]
                ];
            }
            catch (\Exception $exception) {
                return [
                    'success' => false,
                    'message' => $exception->getMessage()
                ];
            }
        }
   }
   

http://127.0.0.1:8800/api/product/2?graph={*,orderItems{*,order{*,user{*}}}}

    namespace App\Http\Controllers;
    
    use App\Http\Controllers\Controller;
    use App\Http\Services\ProductService;
    
    class GraphSqlKeyController extends Controller 
    {
       function __construct (private readonly GraphSqlKeyService $service) {}
   
       public function getList (): JsonResponse
       {
           return response()->json( $this->service->getList());
       }
   
       public function sync (GraphSqlKeySyncRequest $request): JsonResponse
       {
           return response()->json( $this->service->syncGraphSqlKey( $request->all()));
       }
    }
    

http://127.0.0.1:8800/api/product/list?graph_key=customer_product_list