PHPackages                             bitsmind/graphsql - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Database &amp; ORM](/categories/database)
4. /
5. bitsmind/graphsql

ActiveLibrary[Database &amp; ORM](/categories/database)

bitsmind/graphsql
=================

GraphSql is a Graphql like syntactical method to read data from SQL databases with an ease. It's built on top of Laravel Eloquent ORM.

v2.0.7(3mo ago)3173MITPHPPHP ^8.0

Since Jul 26Pushed 3mo ago1 watchersCompare

[ Source](https://github.com/Mohammad1745/graphsql-laravel)[ Packagist](https://packagist.org/packages/bitsmind/graphsql)[ RSS](/packages/bitsmind-graphsql/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (10)DependenciesVersions (25)Used By (0)

GraphSql
========

[](#graphsql)

Overview
--------

[](#overview)

GraphSql is a Graphql like syntactical method to read data from SQL databases with an ease. It's built on top of Laravel Eloquent ORM.

We typically face a dilemma while building api routes. We need to build multiple api for different purposes but the data is from same database table. Let's say for `products` table with 10 columns, We have 2 lists in our frontend app. List 1 shows just`name` and `image`. List 2 shows `name`,`description`. For this case, we may build 2 apis to return specific fields only or a single api to return all fields. It takes longer to build 2 apis. If we build a single api, look we need just two fields, but we are returning all 10 fields. This issue just scales up with our application grows.

Imagine you have a tool, you can ask backend for fields you need from frontend like `{name,image}`. The api will return list of products with `name` and `image` fields, or `{name,description}` to get `name` and `description` only just with a single product list api.

This is what **GraphSql** is.

#### Is GraphSql just limited to single table?

[](#is-graphsql-just-limited-to-single-table)

Hahah, here we go, we can ask for additional data from related tables too. Imagine, we need product list with category name of each product. Then we ask `{name,image,category{name}}`. The api will return a list of product with each `product` having its `category` with field `name` only.
Or, list of products with its variations (table: `product_variations`), `variations{*}` returns all fields.
Or, list of products with its variations\_count (table: `product_variations`), `variations.count`.

We may add conditions in a node graphString, like, `variations(status=1,color=Blue){*}` returns variations of `status 1`and `color Blue`. Allowed operators: `=`, `!=`, `>=`, ``, `prefix('/graph-sql-key')->group(function () {
       Route::get('/list', [GraphSqlKeyController::class, 'getList']);
       Route::post('/sync', [GraphSqlKeyController::class, 'sync']);
    });

    ```
2. Add Controller `app/Http/Controllers/GraphSqlKeyController.php````
    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()));
       }
    }

    ```
3. Add Service in `app/Http/Services/GraphSqlKeyService.php````
    namespace App\Http\Services;

    use Bitsmind\GraphSql\Models\GraphSqlKey;

    class GraphSqlKeyService
    {
       public function getList(): array
       {
           try {
               $graphSqlKeys = GraphSqlKey::orderBy('key','asc')->get();

                return [
                    'success' => true,
                    'data' => ['graphSqlKeys' => $graphSqlKeys]
                ];
           } catch (\Exception $exception) {
               return [
                    'success' => false,
                    'message' => $exception->getMessage()
                ];
           }
       }

       public function syncGraphSqlKey(array $data): array
       {
           try {
               $graphSqlKey = GraphSqlKey::where('key', $data['key'])->first();
               if ($graphSqlKey) {
                   $graphSqlKey->update([
                       'string' => $data['string']
                   ]);
               }
               else {
                   GraphSqlKey::create([
                       'key' => $data['key'],
                       'string' => $data['string']
                   ]);
               }

                return [
                    'success' => true,
                    'message' => 'GraphSql Key Synced Successfully'
                ];
           } catch (\Exception $exception) {
               return [
                    'success' => false,
                    'message' => $exception->getMessage()
                ];
           }
       }
    }

    ```

Use ` QueryAssist::queryGraphSQLByKey` instead of ` QueryAssist::queryGraphSQL`.

Now the api call

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

```

### 2. GraphSql String Encryption

[](#2-graphsql-string-encryption)

The graph string can be encrypted and send as query params. Remember, encryption is expensive.

1. Use this encryption function in frontend to encrypt the string first

    ```
     //js
     function encrypt (str, secret) {
         const refCharSet =',_.-=>
