andyharis/yii2-apigql
yii2-apigql provides methods to work with database on CRUD operations, (*1)
Usage
API docs, (*2)
For example we have 3 table/models: Users, Messages, Post., (*3)
-
Users
-> hasMany Post
-
Post
-> hasMany Messages
We want to access clients with all messages including post:, (*4)
{
"username": "",
"avatarUrl": "",
"post": {
"postName":"",
"messages": {
"textMessage": "",
"dateAdded": ""
}
}
}
Make request and get all data with the same format you provided., (*5)
GET /clients?select={"username": "","avatarUrl": "","post": {"postName": "","messages": {"textMessage": "","dateAdded": ""}}}
, (*6)
// response
{
"username": "Andyhar",
"avatarUrl": "http://example.com/andyhar.png",
"post": [
{
"postName": "Post about API",
"dateAdded": "1500276204",
"messages": [
{
"textMessage": "Hey what a nice post!",
"dateAdded": "1500276704",
},
{
"textMessage": "Make more posts like this!",
"dateAdded": "1500279841",
}
]
},
]
}
- Access main model and nested relations data with one query.
- Sort by nested relations:
-
/clients?select={...}&sort=post.postName
- sort by post.postName ASC
-
/clients?select={...}&sort=!post.messages.dateAdded
- sort by post.messaged.dateAdded DESC
- Filter data with nested conditions:
-
/clients?select={"username":"=Andyhar"}
- where username equals Andyhar
-
/clients?select={"post":{"messages":{"textMessage":"~Rocks"}}}
- where post.messages.textMessage like Rocks
-
/clients?select={"post":{"likes":">35"}}
- where post.likes > 35
Installation
The preferred way to install this extension is through composer.
Either run, (*7)
composer require andyharis/yii2apigql
Getting started
After installation you should enable this module extension in your config file:
1. Open your frontend/config/main.php
2. Add module gql
to bootstrap
section, (*8)
// main.php
return [
'id' => 'app-frontend',
'basePath' => dirname(__DIR__),
'bootstrap' => ['log', 'gql'],
// your code
];
Then you need to initialize component itself.
Just add new component gql
to list of your components:, (*9)
// main.php
'components' => [
'gql' => [
'class' => "andyharis\yii2apigql\Bootstrap",
'relations' => require 'models.php'
],
// your code
]
Creating file models.php
As you can see we require 'models.php'
to let component know which models to use., (*10)
So you probably want create a separate file to store you models for this case., (*11)
// models.php
use andyharis\yii2apigql\components\api\Relations;
// Initializing component relations class which will handle dependencies
$object = new Relations();
// Add all models you need to work with
$object
->addModel(String $name, String $className)
...
...
->addModel('clients', \frontend\models\Clients::className())
->addModel('job', \frontend\models\Job::className());
// we need to return this object with relations
return $object;
Where:
* clients
- indicates name for your model
* \frontend\models\Clients::className()
- indicates what model should use to fetch and update data, (*12)
Almost there
Another important thing is to extend all your models with Yii2ApigqlRecord
component., (*13)
// frontend/models/Clients.php
namespace frontend\models;
use andyharis\yii2apigql\components\Yii2ApigqlRecord;
// This is important, because Yii2ApigqlRecord has some methods which use your models to make magic.
// Of course you can extend it with your class but don't forget to extend Yii2ApigqlRecord
class Clients extends Yii2ApigqlRecord
That's it. Now you can work with yii2apigql
., (*14)
For more info please visit Wiki for API documentation., (*15)