laravel graphql php,Laravel GraphQL之Query

穆景辉
2023-12-01

namespace App\GraphQL\Query;

use Folklore\GraphQL\Support\Query;

use GraphQL\Type\Definition\ResolveInfo;

use GraphQL\Type\Definition\Type;

use GraphQL;

use App\User;

class UsersQuery extends Query{

protected $attributes = [

'name' => 'UserQuery',

'description' => 'A query'

];

public function type()

{

return Type::listOf(GraphQL::type('User'));

}

public function args()

{

return [

'id'=>['name'=>'id','type'=>Type::string()],

'userAccount'=>['name'=>'userAccount','type'=>Type::string()],

'userPass'=>['name'=>'userPass','type'=>Type::string()],

'limit'=>['name'=>'limit','type'=>Type::int()]

];

}

public function resolve($root, $args, $context, ResolveInfo $info)

{

$qb = User::orderBy('id', 'desc');

if(isset($args['limit']))

{

$qb->limit($args['limit'])->latest('id');

}

if(isset($args['id']))

{

$qb->where('id',$args['id']);

}

if(isset($args['userAccount']))

{

$qb->where('userAccount',$args['userAccount']);

}

return $qb->get();

}

 类似资料: