From e9f5b9821b659cde76a1dcf365f0987b964335e9 Mon Sep 17 00:00:00 2001 From: Mohd Saqueib Ansari Date: Thu, 31 Aug 2017 06:58:34 +0530 Subject: [PATCH] Optimized queries --- app/GraphQL/Mutation/Reply/CreateReplyMutation.php | 5 +++-- app/GraphQL/Query/TweetsQuery.php | 5 +++-- app/GraphQL/Type/ReplyType.php | 6 ++++++ 3 files changed, 12 insertions(+), 4 deletions(-) diff --git a/app/GraphQL/Mutation/Reply/CreateReplyMutation.php b/app/GraphQL/Mutation/Reply/CreateReplyMutation.php index 593ecd4..8033bfa 100644 --- a/app/GraphQL/Mutation/Reply/CreateReplyMutation.php +++ b/app/GraphQL/Mutation/Reply/CreateReplyMutation.php @@ -2,6 +2,7 @@ namespace App\GraphQL\Mutation\Reply; +use App\Reply; use GraphQL; use GraphQL\Type\Definition\Type; use Folklore\GraphQL\Support\Mutation; @@ -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); } } \ No newline at end of file diff --git a/app/GraphQL/Query/TweetsQuery.php b/app/GraphQL/Query/TweetsQuery.php index c54b774..17b09d9 100644 --- a/app/GraphQL/Query/TweetsQuery.php +++ b/app/GraphQL/Query/TweetsQuery.php @@ -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); } diff --git a/app/GraphQL/Type/ReplyType.php b/app/GraphQL/Type/ReplyType.php index e3a7666..feb1aec 100644 --- a/app/GraphQL/Type/ReplyType.php +++ b/app/GraphQL/Type/ReplyType.php @@ -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') ]