Skip to content

Commit

Permalink
Optimized queries
Browse files Browse the repository at this point in the history
  • Loading branch information
saqueib committed Aug 31, 2017
1 parent 0cbb078 commit e9f5b98
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 4 deletions.
5 changes: 3 additions & 2 deletions app/GraphQL/Mutation/Reply/CreateReplyMutation.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\GraphQL\Mutation\Reply;

use App\Reply;
use GraphQL;
use GraphQL\Type\Definition\Type;
use Folklore\GraphQL\Support\Mutation;
Expand Down Expand Up @@ -38,9 +39,9 @@ public function resolve($root, $args)
$data = [
'body' => $args['body'],
'tweet_id' => $args['tweet_id'],
'user_id' => auth()->id,
'user_id' => auth()->user()->id
];

return Tweet::replies()->create($data);
return Reply::create($data);
}
}
5 changes: 3 additions & 2 deletions app/GraphQL/Query/TweetsQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,10 @@ public function args()
public function resolve($root, $args)
{
if(isset($args['id'])) {
$tweet = Tweet::withCount('replies', 'likes')->with('replies')->find($args['id']);
// get a single tweet details
$tweet = Tweet::with('replies')->withCount('replies', 'likes')->find($args['id']);
} else {
// Get all the latest tweet by followings user
// Get all the latest tweet by followings user for authenticated user
$followingUser = auth()->user()->following()->pluck('follow_id')->toArray() + [auth()->user()->id];
$tweet = Tweet::with('user')->withCount('replies', 'likes')->whereIn('user_id', $followingUser);
}
Expand Down
6 changes: 6 additions & 0 deletions app/GraphQL/Type/ReplyType.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ public function fields()
'updated_at' => [
'type' => Type::string()
],
'replies_count' => [
'type' => Type::int()
],
'likes_count' => [
'type' => Type::int()
],
'user' => [
'type' => GraphQL::type('User')
]
Expand Down

0 comments on commit e9f5b98

Please sign in to comment.