Advertisement
Guest User

Untitled

a guest
Apr 27th, 2017
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.31 KB | None | 0 0
  1. <?php
  2.  
  3. namespace App\GraphQL\Query;
  4.  
  5. use App\User;
  6. use GraphQL\Type\Definition\Type;
  7. use Rebing\GraphQL\Support\Facades\GraphQL;
  8. use Rebing\GraphQL\Support\Query;
  9. use Rebing\GraphQL\Support\SelectFields;
  10.  
  11. class UsersQuery extends Query
  12. {
  13. protected $attributes = [
  14. 'name' => 'Users Query',
  15. 'description' => 'A query of users'
  16. ];
  17.  
  18. public function type()
  19. {
  20. // result of query with pagination laravel
  21. return GraphQL::paginate('users');
  22. }
  23.  
  24. // arguments to filter query
  25. public function args()
  26. {
  27. return [
  28. 'id' => [
  29. 'name' => 'id',
  30. 'type' => Type::int()
  31. ],
  32. 'email' => [
  33. 'name' => 'email',
  34. 'type' => Type::string()
  35. ]
  36. ];
  37. }
  38.  
  39. public function resolve($root, $args, SelectFields $fields)
  40. {
  41. $where = function ($query) use ($args) {
  42. if (isset($args['id'])) {
  43. $query->where('id',$args['id']);
  44. }
  45.  
  46. if (isset($args['email'])) {
  47. $query->where('email',$args['email']);
  48. }
  49. };
  50. $user = User::with(array_keys($fields->getRelations()))
  51. ->where($where)
  52. ->select($fields->getSelect())
  53. ->paginate();
  54. return $user;
  55. }
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement