diff --git a/app/GraphQL/Type/LikeType.php b/app/GraphQL/Type/LikeType.php index 5a17ccc..84c3608 100644 --- a/app/GraphQL/Type/LikeType.php +++ b/app/GraphQL/Type/LikeType.php @@ -7,54 +7,32 @@ class LikeType extends GraphQLType { - protected $attributes = [ - 'name' => 'Like', - 'description' => 'A user like on a tweet' - ]; - - /* - * Uncomment following line to make the type input object. - * http://graphql.org/learn/schema/#input-types - */ - // protected $inputObject = true; - public function fields() { return [ 'id' => [ - 'type' => Type::nonNull(Type::int()), - 'description' => 'The id of like' + 'type' => Type::nonNull(Type::int()) ], 'user_id' => [ - 'type' => Type::nonNull(Type::int()), - 'description' => 'The id of the user' + 'type' => Type::nonNull(Type::int()) ], 'tweet_id' => [ - 'type' => Type::nonNull(Type::int()), - 'description' => 'The id of the tweet' + 'type' => Type::nonNull(Type::int()) ], 'created_at' => [ - 'type' => Type::string(), - 'description' => 'Creation datetime' + 'type' => Type::string() ], 'updated_at' => [ - 'type' => Type::string(), - 'description' => 'Updating datetime' + 'type' => Type::string() ], // Nested Resource 'user' => [ - 'type' => GraphQL::type('User'), - 'description' => 'A User type', + 'type' => GraphQL::type('User') ] ]; } - protected function resolveUserField($root, $args) - { - return $root->user; - } - protected function resolveCreatedAtField($root, $args) { return (string) $root->created_at->diffForHumans(); diff --git a/app/GraphQL/Type/ProfileType.php b/app/GraphQL/Type/ProfileType.php index 143f516..4b1eaf8 100644 --- a/app/GraphQL/Type/ProfileType.php +++ b/app/GraphQL/Type/ProfileType.php @@ -7,55 +7,35 @@ class ProfileType extends GraphQLType { - protected $attributes = [ - 'name' => 'Profile', - 'description' => 'A user profile' - ]; - - /* - * Uncomment following line to make the type input object. - * http://graphql.org/learn/schema/#input-types - */ - // protected $inputObject = true; - public function fields() { return [ 'id' => [ - 'type' => Type::nonNull(Type::int()), - 'description' => 'The id of the user' + 'type' => Type::nonNull(Type::int()) ], 'bio' => [ - 'type' => Type::string(), - 'description' => 'The bio of user' + 'type' => Type::string() ], 'designation' => [ - 'type' => Type::string(), - 'description' => 'The designation of user' + 'type' => Type::string() ], 'company' => [ - 'type' => Type::string(), - 'description' => 'The company of user' + 'type' => Type::string() ], 'city' => [ - 'type' => Type::string(), - 'description' => 'The city of user' + 'type' => Type::string() ], 'country' => [ - 'type' => Type::string(), - 'description' => 'The country of user' + 'type' => Type::string() ], 'dob' => [ - 'type' => Type::string(), - 'description' => 'The date of birth of user' + 'type' => Type::string() ], 'created_at' => [ - 'type' => Type::string(), - 'description' => 'Creation datetime' + 'type' => Type::string() ], 'updated_at' => [ 'type' => Type::string(), - 'description' => 'Updating datetime' ] ]; } diff --git a/app/GraphQL/Type/ReplyType.php b/app/GraphQL/Type/ReplyType.php index 3fe91ba..209f092 100644 --- a/app/GraphQL/Type/ReplyType.php +++ b/app/GraphQL/Type/ReplyType.php @@ -7,43 +7,26 @@ class ReplyType extends GraphQLType { - protected $attributes = [ - 'name' => 'Reply', - 'description' => 'A reply for a Tweet' - ]; - - /* - * Uncomment following line to make the type input object. - * http://graphql.org/learn/schema/#input-types - */ - // protected $inputObject = true; - public function fields() { return [ 'id' => [ - 'type' => Type::nonNull(Type::int()), - 'description' => 'The id of reply' + 'type' => Type::nonNull(Type::int()) ], 'body' => [ - 'type' => Type::nonNull(Type::string()), - 'description' => 'Body of of a reply' + 'type' => Type::nonNull(Type::string()) ], 'user_id' => [ - 'type' => Type::nonNull(Type::int()), - 'description' => 'The id of the user of reply' + 'type' => Type::nonNull(Type::int()) ], 'tweet_id' => [ - 'type' => Type::nonNull(Type::int()), - 'description' => 'The id of the tweet which receives reply' + 'type' => Type::nonNull(Type::int()) ], 'created_at' => [ - 'type' => Type::string(), - 'description' => 'Creation datetime' + 'type' => Type::string() ], 'updated_at' => [ - 'type' => Type::string(), - 'description' => 'Updating datetime' + 'type' => Type::string() ], 'user' => [ 'type' => GraphQL::type('User') @@ -53,7 +36,7 @@ public function fields() // If you want to resolve the field yourself, you can declare a method // with the following format resolve[FIELD_NAME]Field() - protected function resolveEmailField($root, $args) + protected function resolveBodyField($root, $args) { return nl2br($root->body); } diff --git a/app/GraphQL/Type/TweetType.php b/app/GraphQL/Type/TweetType.php index f852de6..e6b64c1 100644 --- a/app/GraphQL/Type/TweetType.php +++ b/app/GraphQL/Type/TweetType.php @@ -7,81 +7,35 @@ class TweetType extends GraphQLType { - protected $attributes = [ - 'name' => 'Tweet', - 'description' => 'A Tweet' - ]; - - /* - * Uncomment following line to make the type input object. - * http://graphql.org/learn/schema/#input-types - */ - // protected $inputObject = true; - public function fields() { return [ 'id' => [ - 'type' => Type::nonNull(Type::int()), - 'description' => 'The id of the tweet' + 'type' => Type::nonNull(Type::int()) ], 'body' => [ - 'type' => Type::nonNull(Type::string()), - 'description' => 'Body of of a tweet' + 'type' => Type::nonNull(Type::string()) ], 'user_id' => [ - 'type' => Type::nonNull(Type::int()), - 'description' => 'The id of the user of reply' + 'type' => Type::nonNull(Type::int()) ], 'created_at' => [ - 'type' => Type::string(), - 'description' => 'Creation datetime' + 'type' => Type::string() ], 'updated_at' => [ - 'type' => Type::string(), - 'description' => 'Updating datetime' + 'type' => Type::string() ], // Counts 'replies_count' => [ - 'type' => Type::int(), - 'description' => 'count of replies' + 'type' => Type::int() ], 'likes_count' => [ - 'type' => Type::int(), - 'description' => 'count of likes' + 'type' => Type::int() ], // Nested Resource 'user' => [ 'type' => GraphQL::type('User') - ], - 'replies' => [ - 'args' => [ - 'id' => [ - 'type' => Type::int(), - 'description' => 'id of the reply', - ], - 'first' => [ - 'type' => Type::int(), - 'description' => 'limit result', - ], - ], - 'type' => Type::listOf(GraphQL::type('Reply')), - 'description' => 'reply description', - ], - 'likes' => [ - 'args' => [ - 'id' => [ - 'type' => Type::int(), - 'description' => 'id of the like', - ], - 'first' => [ - 'type' => Type::int(), - 'description' => 'limit result', - ], - ], - 'type' => Type::listOf(GraphQL::type('Like')), - 'description' => 'A Like type', ] ]; } @@ -89,6 +43,11 @@ public function fields() // If you want to resolve the field yourself, you can declare a method // with the following format resolve[FIELD_NAME]Field() + protected function resolveBodyField($root, $args) + { + return nl2br($root->body); + } + protected function resolveCreatedAtField($root, $args) { return (string) $root->created_at->diffForHumans(); diff --git a/app/GraphQL/Type/UserType.php b/app/GraphQL/Type/UserType.php index d851823..e8bf04d 100644 --- a/app/GraphQL/Type/UserType.php +++ b/app/GraphQL/Type/UserType.php @@ -7,126 +7,49 @@ class UserType extends GraphQLType { - protected $attributes = [ - 'name' => 'User', - 'description' => 'A user' - ]; - - /* - * Uncomment following line to make the type input object. - * http://graphql.org/learn/schema/#input-types - */ - // protected $inputObject = true; - public function fields() { return [ 'id' => [ - 'type' => Type::nonNull(Type::int()), - 'description' => 'The id of the user' + 'type' => Type::nonNull(Type::int()) ], 'name' => [ - 'type' => Type::string(), - 'description' => 'The name of user' + 'type' => Type::string() ], 'username' => [ - 'type' => Type::string(), - 'description' => 'The username of user' + 'type' => Type::string() ], 'email' => [ - 'type' => Type::string(), - 'description' => 'The email of user' + 'type' => Type::string() ], 'avatar' => [ - 'type' => Type::string(), - 'description' => 'The avatar of user' + 'type' => Type::string() ], 'cover' => [ - 'type' => Type::string(), - 'description' => 'The cover of user' + 'type' => Type::string() ], 'created_at' => [ - 'type' => Type::string(), - 'description' => 'Creation datetime' + 'type' => Type::string() ], 'updated_at' => [ - 'type' => Type::string(), - 'description' => 'Updating datetime' + 'type' => Type::string() ], // counts 'followers_count' => [ - 'type' => Type::int(), - 'description' => 'count for followers' + 'type' => Type::int() ], 'following_count' => [ - 'type' => Type::int(), - 'description' => 'count for following' - ], - 'is_following' => [ - 'type' => Type::boolean() - ], - 'is_followed' => [ - 'type' => Type::boolean() + 'type' => Type::int() ], 'likes_count' => [ - 'type' => Type::int(), - 'description' => 'count for likes' + 'type' => Type::int() ], 'tweets_count' => [ - 'type' => Type::int(), - 'description' => 'count for likes' - ], - // Nested Resource - 'tweets' => [ - 'args' => [ - 'id' => [ - 'type' => Type::int(), - 'description' => 'id of the tweet', - ], - 'first' => [ - 'type' => Type::int(), - 'description' => 'limit result', - ], - ], - 'type' => Type::listOf(GraphQL::type('Tweet')), - 'description' => 'tweet description', - ], - 'profile' => [ - 'type' => GraphQL::type('Profile'), - 'description' => 'user profile', - ], - 'followers' => [ - 'args' => [ - 'id' => [ - 'type' => Type::int(), - 'description' => 'id of the follower', - ], - 'first' => [ - 'type' => Type::int(), - 'description' => 'limit result', - ], - ], - 'type' => Type::listOf(GraphQL::type('User')), - 'description' => 'followers User', - ], - 'following' => [ - 'args' => [ - 'id' => [ - 'type' => Type::int(), - 'description' => 'id of the following', - ], - 'first' => [ - 'type' => Type::int(), - 'description' => 'limit result', - ], - 'offset' => [ - 'type' => Type::int(), - 'description' => 'offset result', - ], - ], - 'type' => Type::listOf(GraphQL::type('User')), - 'description' => 'following User', + 'type' => Type::int() ], + 'is_following' => [ + 'type' => Type::boolean() + ] ]; } @@ -142,60 +65,4 @@ protected function resolveCreatedAtField($root, $args) { return (string) $root->created_at->toFormattedDateString(); } - - // You can also resolve any nested resource filed in same way - protected function resolveIsFollowedField($root, $args) { - return $root->append('is_followed'); - } - - protected function resolveTweetsField($root, $args) - { - $tweets = $root->tweets(); - - if (isset($args['id'])) { - return $tweets->where('id', $args['id']); - } - - // check for limit - if( isset($args['first']) ) { - $tweets = $tweets->limit($args['first'])->latest(); - } - - // check for offset - if( isset($args['offset']) ) { - $tweets = $tweets->limit($args['offset']); - } - - return $tweets->get(); - } - - protected function resolveFollowersField($root, $args) { - $users = $root->followers(); - - if (isset($args['id'])) { - return $users->where('id', $args['id']); - } - - // check for limit - if( isset($args['first']) ) { - $users = $users->limit($args['first'])->latest(); - } - - return $users->get(); - } - - protected function resolveFollowingField($root, $args) { - $users = $root->following(); - - if (isset($args['id'])) { - return $users->where('id', $args['id']); - } - - // check for limit - if( isset($args['first']) ) { - $users = $users->limit($args['first'])->latest(); - } - - return $users->get(); - } } \ No newline at end of file