From 0cbb07862267d8c53767984799d63fcff80d1ce1 Mon Sep 17 00:00:00 2001 From: Mohd Saqueib Ansari Date: Wed, 30 Aug 2017 10:44:17 +0530 Subject: [PATCH] improved queries with lazy load --- app/GraphQL/Query/TweetsQuery.php | 4 ++-- app/GraphQL/Query/UserQuery.php | 9 ++------- app/GraphQL/Type/ReplyType.php | 1 - app/GraphQL/Type/UserType.php | 7 ++++--- app/Tweet.php | 2 +- 5 files changed, 9 insertions(+), 14 deletions(-) diff --git a/app/GraphQL/Query/TweetsQuery.php b/app/GraphQL/Query/TweetsQuery.php index 558a444..c54b774 100644 --- a/app/GraphQL/Query/TweetsQuery.php +++ b/app/GraphQL/Query/TweetsQuery.php @@ -30,11 +30,11 @@ public function args() public function resolve($root, $args) { if(isset($args['id'])) { - $tweet = Tweet::withCount('replies', 'likes')->find($args['id']); + $tweet = Tweet::withCount('replies', 'likes')->with('replies')->find($args['id']); } else { // Get all the latest tweet by followings user $followingUser = auth()->user()->following()->pluck('follow_id')->toArray() + [auth()->user()->id]; - $tweet = Tweet::withCount('replies', 'likes')->whereIn('user_id', $followingUser); + $tweet = Tweet::with('user')->withCount('replies', 'likes')->whereIn('user_id', $followingUser); } diff --git a/app/GraphQL/Query/UserQuery.php b/app/GraphQL/Query/UserQuery.php index acdd451..190522c 100644 --- a/app/GraphQL/Query/UserQuery.php +++ b/app/GraphQL/Query/UserQuery.php @@ -32,7 +32,7 @@ public function resolve($root, $args, $context, ResolveInfo $info) $fields = $info->getFieldSelection($depth = 3); - $user = User::query(); + $user = User::with('tweets', 'tweets.user'); // check for fields foreach ($fields as $field => $keys) { @@ -69,11 +69,6 @@ public function resolve($root, $args, $context, ResolveInfo $info) $user = $user->where('id' , $args['id']); } - $user = $user->first(); - - // check is current user following it - $user->is_following = auth()->user()->isFollowing($user->id); - - return $user; + return $user->first(); } } \ No newline at end of file diff --git a/app/GraphQL/Type/ReplyType.php b/app/GraphQL/Type/ReplyType.php index 209f092..e3a7666 100644 --- a/app/GraphQL/Type/ReplyType.php +++ b/app/GraphQL/Type/ReplyType.php @@ -45,5 +45,4 @@ protected function resolveCreatedAtField($root, $args) { return (string) $root->created_at->diffForHumans(); } - } \ No newline at end of file diff --git a/app/GraphQL/Type/UserType.php b/app/GraphQL/Type/UserType.php index 22b4ae4..d7ea279 100644 --- a/app/GraphQL/Type/UserType.php +++ b/app/GraphQL/Type/UserType.php @@ -50,9 +50,6 @@ public function fields() 'is_following' => [ 'type' => Type::boolean() ], - 'is_followed' => [ - 'type' => Type::boolean() - ], 'tweets' => [ 'args' => [ 'id' => [ @@ -103,4 +100,8 @@ protected function resolveCreatedAtField($root, $args) { return (string) $root->created_at->toFormattedDateString(); } + + protected function resolveIsFollowingField($root) { + return auth()->user()->isFollowing($root->id); + } } \ No newline at end of file diff --git a/app/Tweet.php b/app/Tweet.php index fa554c0..55858c7 100644 --- a/app/Tweet.php +++ b/app/Tweet.php @@ -24,7 +24,7 @@ public function user() public function replies() { - return $this->hasMany(Reply::class)->latest(); + return $this->hasMany(Reply::class)->with('user')->latest(); } public function likes()