Download the PHP package gregorip02/restql without Composer
On this page you can find all versions of the php package gregorip02/restql. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download gregorip02/restql
More information about gregorip02/restql
Files in gregorip02/restql
Package restql
Short Description A data resolution package for Laravel
License LGPL-3.0
Informations about the package restql
RestQL
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 in the eloquent methods.
Please see the documentation.
Why?
Imagine you have an application that manages authors, these authors can publish articles and those articles can have comments from different authors. You have a web client, for example, that uses fetch to consume the data offered by your service and somewhere in your code, you need a list of the author's names only.
They wear something like that.
So, you have a route like this.
routes/api.php
Most likely you will use a controller, then use the author model and query the data. Finally you would have a response similar to this.
But what if you only need a author's names collection for example? Imagine that your application becomes huge and your user model handles a large number of attributes.
Get started
Install RestQL using composer.
Publish the package configuration.
Add the RestqlAttributes
trait to your eloquent models.
2.2 This is recomended because you can determine the selectable attributes with
onSelectFillable
.
Generate an authorizer
for your App\Models\Author
model.
This creates a new class in the namespace App\Restql\Authorizers\AuthorAuthorizer
like the following.
Then add the available HTTP methods to your authorizer as follows.
Then, set your schema definition.
Since version 2.x of this package the configuration has been updated to increase flexibility and internal behavior modification.
You must define your entire schema in the config file, RestQL will then interpret it and execute the queries based on this file. With this there is a possibility that you can remove internal functions, modify them or even create your own implementations. See Schema definition.
config/restql.php
The developer must specify the return type of the relationships defined in the eloquent model. You should set the return type of your associative methods (relationships). For example.
app/Models/Author.php
Configure your endpoint.
routes/api.php
Refactor time
Now, you can re-factor your client's code so that it sends a parameter in the request with the data it needs, in this case a collection of author names. They wear something like that.
The parameters of the query should be a parsed objects that defines the clauses accepted by your schema definition. For your web clients we recommend qs. Instead of having a long JSON response with unnecessary data, you would get something like this.
Likewise, this will considerably optimize your queries to the database management system. In this case, it will run just a select id, name from authors
for example.
The good parts
So, let's say you now have a requirement in your application that tells you to have the id
and title
of the last two articles published by each author.
You wold get a response like this.
How it works
Basically, RestQL filters the keys of the models received in the HTTP request and compares them with the keys of the user configuration. These keys represent a specific eloquent model. The values of these keys are eloquent clauses (methods) accepted by RestQL and the arguments of these clauses are sent as values.
For example, if a params like the following is sent in the request.
RestQL will interpret this as the following.
You can read more about the RestQL Clausules <a href="https://github.com/gregorip02/restql/wiki/Clausules" title="RestQL Documentation">here.
What's next?
Are you interested on contrib to this project? see the <a href="./NEXT.md" title="Next features">NEXT.md file.
Contributing
Thanks for consider contrib to this project, please see the development docs at this page.
Please support it
This is a personal project that can be very useful, if you believe it, help me develop new functionalities and create a pull request, I will be happy to review and add it. So, You can also contribute to the team by buying a coffee.
<img src="https://cdn.buymeacoffee.com/buttons/default-red.png" style="border-radius: 5px;" alt="Buy Us A Coffee" width="300" height="80"/>