-
-
Notifications
You must be signed in to change notification settings - Fork 6
Install
You can install the package from the terminal using composer.
composer require gregorip02/restql
If you are already using an old version of RestQL you can upgrade to the latest project compatible version.
This is optional, however it is good practice to keep your dependencies updated to prevent bugs.
composer update gregorip02/restql
Publish the package configuration using.
php artisan restql:schema
This will create a config file in config/restql.php
in which you must define
your scheme.
Then, add the RestqlAttributes
trait to your eloquent models.
This is mandatory only if your endpoint will accept create or update clausules.
<?php
use Illuminate\Database\Eloquent\Model;
use Restql\Traits\RestqlAttributes;
class Article extends Model
{
use RestqlAttributes;
// ...
}
The scheme is the starting point, you must define a list of models with
those that RestQL can interact with. Also, you can set middlewares
and authorizers
for requests that try to access these models.
For example, if you want to add your model named App\Book
to the schema, initially
You must define an authorizer that gives access to requests of type HTTP GET to this
resource of your application.
php artisan restql:authorizer BookAuthorizer
This will create a new authorizer. Authorizers have methods called equals
HTTP verbs that you can use, for example, to allow or deny requests
towards resources of type App\Book
.
The structure of this should look like the following.
<?php
namespace App\Restql\Authorizers;
use Restql\Authorizer;
final class BookAuthorizer extends Authorizer
{
/**
* Can get one or more resources.
*
* @param array $clausules
* @return bool
*/
public static function get(array $clausules = []): bool
{
// You could replace this with permission checks or validations.
return true;
}
}
Then edit the schema
key in the config/restql.php
file with something similar
to the next.
<?php
// config/restql.php
return [
// ...
'schema' => [
'books' => [
'class' => 'App\Book',
'authorizer' => 'App\Restql\Authorizers\BookAuthorizer'
]
]
// ...
];
RestQL can now interact with its App\Book
model using the policies
defined by the developer. You can also define middlewares
which are
fired just before starting to query the data.
<?php
// config/restql.php
return [
// ...
'schema' => [
'books' => [
// ...
'middlewares' => ['auth']
]
]
// ...
];
Add the RestQL trait in the models that you want to manipulate automatically.
<?php
use Illuminate\Database\Eloquent\Model;
use Restql\Traits\RestqlAttributes;
class Book extends Model
{
use RestqlAttributes;
// ...
}
In the routes file routes/api.php
add the following lines of code.
<?php
// routes/api.php
use Restql\Restql;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;
Route::any('restql', function (Request $request) {
return Restql::resolve($request);
});
From now on, your clients can start sending requests of any kind and RestQL will build the query and return the information.