2017 © Pedro Peláez
 

yii2-extension yii2apigql

Yii2 API gql

image

andyharis/yii2apigql

Yii2 API gql

  • Tuesday, June 26, 2018
  • by andyharis
  • Repository
  • 1 Watchers
  • 0 Stars
  • 29 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 1 Forks
  • 0 Open issues
  • 18 Versions
  • 45 % Grown

The README.md

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)

The Versions

26/06 2018

dev-master

9999999-dev

Yii2 API gql

  Sources   Download

MIT

The Requires

 

by Avatar andyharis

extension yii2

26/06 2018

0.2.5

0.2.5.0

Yii2 API gql

  Sources   Download

MIT

The Requires

 

by Avatar andyharis

extension yii2

01/11 2017

0.2.4

0.2.4.0

Yii2 API gql

  Sources   Download

MIT

The Requires

 

by Avatar andyharis

extension yii2

17/10 2017

0.2.3

0.2.3.0

Yii2 API gql

  Sources   Download

MIT

The Requires

 

by Avatar andyharis

extension yii2

05/10 2017

0.2.2

0.2.2.0

Yii2 API gql

  Sources   Download

MIT

The Requires

 

by Avatar andyharis

extension yii2

13/09 2017

0.2.1

0.2.1.0

Yii2 API gql

  Sources   Download

MIT

The Requires

 

by Avatar andyharis

extension yii2

12/09 2017

0.2.0

0.2.0.0

Yii2 API gql

  Sources   Download

MIT

The Requires

 

by Avatar andyharis

extension yii2

12/09 2017

0.1.9

0.1.9.0

Yii2 API gql

  Sources   Download

MIT

The Requires

 

by Avatar andyharis

extension yii2

29/08 2017

0.1.8

0.1.8.0

Yii2 API gql

  Sources   Download

MIT

The Requires

 

by Avatar andyharis

extension yii2

11/08 2017

0.1.6

0.1.6.0

Yii2 API gql

  Sources   Download

MIT

The Requires

 

by Avatar andyharis

extension yii2

11/08 2017

0.1.7

0.1.7.0

Yii2 API gql

  Sources   Download

MIT

The Requires

 

by Avatar andyharis

extension yii2

10/08 2017

0.1.5

0.1.5.0

Yii2 API gql

  Sources   Download

MIT

The Requires

 

by Avatar andyharis

extension yii2

01/08 2017

0.1.3

0.1.3.0

Yii2 API gql

  Sources   Download

MIT

The Requires

 

by Avatar andyharis

extension yii2

01/08 2017

0.1.4

0.1.4.0

Yii2 API gql

  Sources   Download

MIT

The Requires

 

by Avatar andyharis

extension yii2

01/08 2017

1.0.0.x-dev

1.0.0.9999999-dev

Yii2 API gql

  Sources   Download

MIT

The Requires

 

by Avatar andyharis

extension yii2

31/07 2017

0.1.0

0.1.0.0

Yii2 API gql

  Sources   Download

MIT

The Requires

 

by Avatar andyharis

extension yii2

31/07 2017

0.1.1

0.1.1.0

Yii2 API gql

  Sources   Download

MIT

The Requires

 

by Avatar andyharis

extension yii2

31/07 2017

0.0.1

0.0.1.0

Yii2 API gql

  Sources   Download

MIT

The Requires

 

by Avatar andyharis

extension yii2