GraphSql

Serve Only Required Data With Graphsql From SQL Database

graphsql graphql laravel sql php rest api September 17, 2024
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 have to build multiple api's to serve data in different sections in a application 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 much more time to build 2 apis. If we build a single api, all we need just two fields, but we are returning all 10 other 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?

No really, 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.

Let's explore the magic of graphsql

Example 1:

Assume, we need list of product with category name

Ask in api query params:

Data from response:

Example 2:

Also need variations? Ask that:

Data from response:

Example 3:

Oh, no need all data for variations, just need count of variations. Ask that:

Data from response:

Example 4:

Also need the totoal sale. Ask that:

Data from response:

For Installation Please Visit GraphSql

Thanks for your time.

Author Mohammad Ali
=