RestQL Data Resolution Package for Laravel

News

April 30th, 2020

laravel-api-fallback-route-featured.png

RestQL is a data resolution package for your Laravel models by Gregori Piñeres:

RestQL is a Laravel eloquent-based data resolution package. This package tries to adopt GraphQL principles solving only the data that the client requests. RestQL uses your Laravel models as an entry point to add queries to then based on the eloquent methods.

This package has data resolution clauses, which are a way to improve queries based on parameters provided by the client. It’s RESTful but uses principles from GraphQL.

From the project’s readme, here’s an example of the RestQL route:

1use Restql\Restql;
2use Illuminate\Http\Request;
3 
4// The RestQL endpoint.
5Route::get('restql', function (Request $request) {
6 return Restql::resolve($request);
7});

And with that route, how you might use this package on the frontend to retrieve author names:

1axios.get('http://laravel.app/api/restql', {
2 params: {
3 authors: {
4 select: 'name'
5 }
6 }
7}).then(({ data: authors }) => {
8 // Do something...
9 console.log(authors)
10});

Finally, you’d get the following JSON back:

1{
2 "authors": [
3 { "id": 1, "name": "Kasey Yost" },
4 { "id": 2, "name": "Ike Barton" },
5 { "id": 3, "name": "Emie Daniel" },
6 {...}
7 ]
8}

Learn More

You can learn more about this package, get full installation instructions, and view the source code on GitHub at gregorip02/restql.

Filed in:

Paul Redmond

Full stack web developer. Author of Lumen Programming Guide and Docker for PHP Developers.