Skip to content

Benefits

Gregori Piñeres edited this page May 30, 2020 · 1 revision

On this page you will know what are the benefits of packaging your Rest API with RestQL.

Get the data you need

This is one of the characteristics that make this package essential, clients can know in advance the data that the server will return. In addition, database queries and response size are greatly optimized.

The most practical case would be a request like the following.

{
  "users": {
    "where": ["status", "active"],
    "take": 10,
    "select": ["name", "email"]
  }
}

It is really easy to read the body of this query, basically we are trying to get the email and the name of 10 users whose status is equal to "active".

Eager loading by default

There is a special clause called "with", just like in Laravel this clause allows adding relations to the eloquent query constructor to avoid the N+1 problem. Sometimes you may need to eager load several different relationships in a single operation. To do so, just pass an associative array with the name of your relations as a key and the following clauses as a value.

{
  "users": {
    "where": ["status", "active"],
    "take": 10,
    "select": ["name", "email"],
    "with": {
      "posts": {
        "select": "title"
      }
    }
  }
}

As you will see, this will automatically load the "posts" relation of the "user" model and will obtain the title of the determined post. Finally we would have an answer like the following.

{
  "data": {
    "users": [
      {
        "name": "Jhon Dawl",
        "email": "[email protected]",
        "posts": [
          {
             "title": "Awesome post"
          },
          // ...
        ]
      },
      // ...
    ]
  }
}
Clone this wiki locally