目的

  • 紀錄GraphQL in Laravel 建置流程
  • GraphQL 基礎使用方式

紀錄

程式

1.composer.json

$ composer require folklore/graphql

2.set service & facade

Folklore\GraphQL\ServiceProvider::class,
'GraphQL' => Folklore\GraphQL\Support\Facades\GraphQL::class,

3.laravel

$ php artisan vendor:publish --provider="Folklore\GraphQL\ServiceProvider"

4.set graphql.php config

'types' => [
        'UserType' => 'App\GraphQL\Type\UserType',
    ],
'schemas' => [
        'default' => [
            'query' => [
                'UserQuery' =>'App\GraphQL\Query\UserQuery',
            ],
            'mutation' => [
                
            ]
        ]
    ],

5.Type & Query

<?php
namespace App\GraphQL\Type;

use GraphQL;
use GraphQL\Type\Definition\Type;
use Folklore\GraphQL\Support\Type as GraphQLType;

class UserType extends GraphQLType
{
    protected $attributes = [
        'id' => 'UserId',
        'description' => '使用者ID'
    ];
    
    public function fields()
    {
        return [
            'id' => [
                'type' => Type::nonNull(Type::string()),
                'description' => 'id'
            ],
            'name' => [
                'type' => Type::string(),
                'description' => '名稱'
            ],
            'email' => [
                'type' => Type::string(),
                'description' => '信箱'
            ]
        ];
    }
}
<?php
namespace App\GraphQL\Query;

use GraphQL;
use GraphQL\Type\Definition\Type;
use Folklore\GraphQL\Support\Query;
use App\Model\User as UserModel;

class UserQuery extends Query
{
    protected $attributes = [
        'name' => 'User'
    ];
    
    public function type()
    {
        return Type::listOf(GraphQL::type('UserType'));
    }
    
    public function args()
    {
        return [
            'id' => [
                'name' => 'id',
                'type' => Type::string()
            ],
            'name' => [
                'name' => 'name',
                'type' => Type::string()
            ],
            'email' => [
                'name' => 'email',
                'type' => Type::string()
            ]
        ];
    }
    
    public function resolve($root, $args)
    {
        $searchModel = UserModel::query();
        if (isset($args['id'])){
            $searchModel->where('id', $args['id']);
        }
        if (isset($args['name'])) {
            $searchModel->where('name', $args['name']);
        }
        if (isset($args['email'])) {
                    $searchModel->where('email', $args['email']);
        }
        return $searchModel->get();
    }
}

6.request GraphQL (http://domain/graphql) request1

query{
  MensShopDetail(id:"1"){
    id
    name
  }
}

response1

{
  "data": {
    "User": [
      {
        "id": "1",
        "name": "york",
      }
    ]
  }
}

request2

query{
  MensShopDetail(name:"york"){
    id
    name
  }
}

response2

{
  "data": {
    "User": [
      {
        "id": "1",
        "name": "york",
      },
      {
        "id": "345",
        "name": "york",
      },
      {
        "id": "777",
        "name": "york",
      }
    ]
  }
}

request3

query{
  MensShopDetail(name:"york",email:"jason945119@gmail.com"){
    name
    email
  }
}

response3

{
  "data": {
    "User": [
      {
        "id": "1",
        "name": "york",
        "email":"jason945119@gmail.com"
      },
      {
        "id": "777",
        "name": "york",
        "email":"jason945119@gmail.com"
      }
    ]
  }
}