simexis/laravel-graphql-filter

Define GraphQL schema for connection filtering using Laravel filterable models

dev-master 2022-04-12 09:36 UTC

This package is auto-updated.

Last update: 2024-10-12 15:02:23 UTC


README

Define GraphQL schema for connection filtering using Laravel filterable models.

Installation

composer require simexis/laravel-graphql-filter

Usage

Use Filterable trait on Model

namespace App;
use Simexis\Filterable\Filterable;

class User extends Model
{
  use \Simexis\Filterable\FilterableTrait;
  
  protected $filterable = [
    'name' => Filterable::String
  ];
}

Create GraphQL User type

namespace App\GraphQL\Type;

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

class User extends GraphQLType
{
  protected $attributes = [
    'name' => 'User'
  ];
  public function fields() {
    return [
      'id' => [
        'type' => Type::id(),
      ],
      'name' => [
        'type' => Type::string()
      ]
    ]
  }
}

Create GraphQL UserFilter type. The UserFilter fields defintion will be automatically populated with fields in User::$filterable.

namespace App\GraphQL\Type;

use App\User;
use Simexis\GraphQLFilter\Type\FilterType;

class UserFilter extends FilterType
{
  protected $model = User::class;
}

Create GraphQL query type to list User records using the User model's filterable settings.

namespace App\GraphQL\Type;

use App\User;
use GraphQL;
use GraphQL\Type\Definition\Type;
use Folklore\GraphQL\Support\Query;

class UsersQuery extends Query
{
  protected $attributes = [
    'name' => 'users'
  ];
  public function type() {
    return Type::listOf(GraphQL::type('User'));
  }
  public function args() {
    return [
      'filter' => [
        'type' => GraphQL::type('UserFilter')
      ]
    ];
  }
  public function resolve($root, $args, $context) {
    return User::filter($args['filter'])->orderBy('name')->limit(10)->get();
  }
}

A GraphQL query can now use the Filterable::String rules to perform searches.

query ListJohns {
  users (filter: { name_MATCH: 'john' }) {
    name
  }
}