Skip to content

Commit

Permalink
improved queries with lazy load
Browse files Browse the repository at this point in the history
  • Loading branch information
saqueib committed Aug 30, 2017
1 parent 12a9d29 commit 0cbb078
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 14 deletions.
4 changes: 2 additions & 2 deletions app/GraphQL/Query/TweetsQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}


Expand Down
9 changes: 2 additions & 7 deletions app/GraphQL/Query/UserQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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();
}
}
1 change: 0 additions & 1 deletion app/GraphQL/Type/ReplyType.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,4 @@ protected function resolveCreatedAtField($root, $args)
{
return (string) $root->created_at->diffForHumans();
}

}
7 changes: 4 additions & 3 deletions app/GraphQL/Type/UserType.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,6 @@ public function fields()
'is_following' => [
'type' => Type::boolean()
],
'is_followed' => [
'type' => Type::boolean()
],
'tweets' => [
'args' => [
'id' => [
Expand Down Expand Up @@ -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);
}
}
2 changes: 1 addition & 1 deletion app/Tweet.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit 0cbb078

Please sign in to comment.