Skip to content

Instantly share code, notes, and snippets.

@ragasubekti
Created August 5, 2018 09:28
Show Gist options
  • Save ragasubekti/06f1c257e67eb46094b665a8d752520b to your computer and use it in GitHub Desktop.
Save ragasubekti/06f1c257e67eb46094b665a8d752520b to your computer and use it in GitHub Desktop.
GQL Query Pegawai
<?php
namespace App\GraphQL\Query;
use App\Model\Pegawai;
// use GraphQL\GraphQL;
use GraphQL\Type\Definition\ResolveInfo;
use GraphQL\Type\Definition\Type;
use Rebing\GraphQL\Support\Query;
use Rebing\GraphQL\Support\SelectFields;
// use Rebing\GraphQL\Support\Type;
class PegawaiQuery extends Query
{
protected $attributes = [
'name' => 'PegawaiQuery',
'model' => Pegawai::class,
];
public function type()
{
return \GraphQL::paginate('pegawai');
}
public function args()
{
return [
['name' => 'limit', 'type' => Type::int(), 'defaultValue' => 10],
['name' => 'page', 'type' => Type::int(), 'defaultValue' => 1],
['name' => 'search', 'type' => Type::string(), 'defaultValue' => ''],
];
}
public function resolve($root, $args, SelectFields $fields, ResolveInfo $info)
{
$select = $fields->getSelect();
$with = array_keys($fields->getRelations());
$pegawai = Pegawai::select($fields->getSelect())->with('unitKerja')
->when(strlen($args['search']), function ($query) use ($args) {
$query->where(function ($q) use ($args) {
$q
->where('nip', 'like', '%' . $args['search'] . '%')
->orWhere('nama', 'like', '%' . $args['search'] . '%');
});
})->paginate($args['limit'], ['*'], 'page', $args['page']);
return $pegawai;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment