DataSource.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. <?php
  2. namespace GraphQL\Examples\Blog\Data;
  3. /**
  4. * Class DataSource
  5. *
  6. * This is just a simple in-memory data holder for the sake of example.
  7. * Data layer for real app may use Doctrine or query the database directly (e.g. in CQRS style)
  8. *
  9. * @package GraphQL\Examples\Blog
  10. */
  11. class DataSource
  12. {
  13. private static $users = [];
  14. private static $stories = [];
  15. private static $storyLikes = [];
  16. private static $comments = [];
  17. private static $storyComments = [];
  18. private static $commentReplies = [];
  19. private static $storyMentions = [];
  20. public static function init()
  21. {
  22. self::$users = [
  23. '1' => new User([
  24. 'id' => '1',
  25. 'email' => 'john@example.com',
  26. 'firstName' => 'John',
  27. 'lastName' => 'Doe'
  28. ]),
  29. '2' => new User([
  30. 'id' => '2',
  31. 'email' => 'jane@example.com',
  32. 'firstName' => 'Jane',
  33. 'lastName' => 'Doe'
  34. ]),
  35. '3' => new User([
  36. 'id' => '3',
  37. 'email' => 'john@example.com',
  38. 'firstName' => 'John',
  39. 'lastName' => 'Doe'
  40. ]),
  41. ];
  42. self::$stories = [
  43. '1' => new Story(['id' => '1', 'authorId' => '1', 'body' => '<h1>GraphQL is awesome!</h1>']),
  44. '2' => new Story(['id' => '2', 'authorId' => '1', 'body' => '<a>Test this</a>']),
  45. '3' => new Story(['id' => '3', 'authorId' => '3', 'body' => "This\n<br>story\n<br>spans\n<br>newlines"]),
  46. ];
  47. self::$storyLikes = [
  48. '1' => ['1', '2', '3'],
  49. '2' => [],
  50. '3' => ['1']
  51. ];
  52. self::$comments = [
  53. // thread #1:
  54. '100' => new Comment(['id' => '100', 'authorId' => '3', 'storyId' => '1', 'body' => 'Likes']),
  55. '110' => new Comment(['id' =>'110', 'authorId' =>'2', 'storyId' => '1', 'body' => 'Reply <b>#1</b>', 'parentId' => '100']),
  56. '111' => new Comment(['id' => '111', 'authorId' => '1', 'storyId' => '1', 'body' => 'Reply #1-1', 'parentId' => '110']),
  57. '112' => new Comment(['id' => '112', 'authorId' => '3', 'storyId' => '1', 'body' => 'Reply #1-2', 'parentId' => '110']),
  58. '113' => new Comment(['id' => '113', 'authorId' => '2', 'storyId' => '1', 'body' => 'Reply #1-3', 'parentId' => '110']),
  59. '114' => new Comment(['id' => '114', 'authorId' => '1', 'storyId' => '1', 'body' => 'Reply #1-4', 'parentId' => '110']),
  60. '115' => new Comment(['id' => '115', 'authorId' => '3', 'storyId' => '1', 'body' => 'Reply #1-5', 'parentId' => '110']),
  61. '116' => new Comment(['id' => '116', 'authorId' => '1', 'storyId' => '1', 'body' => 'Reply #1-6', 'parentId' => '110']),
  62. '117' => new Comment(['id' => '117', 'authorId' => '2', 'storyId' => '1', 'body' => 'Reply #1-7', 'parentId' => '110']),
  63. '120' => new Comment(['id' => '120', 'authorId' => '3', 'storyId' => '1', 'body' => 'Reply #2', 'parentId' => '100']),
  64. '130' => new Comment(['id' => '130', 'authorId' => '3', 'storyId' => '1', 'body' => 'Reply #3', 'parentId' => '100']),
  65. '200' => new Comment(['id' => '200', 'authorId' => '2', 'storyId' => '1', 'body' => 'Me2']),
  66. '300' => new Comment(['id' => '300', 'authorId' => '3', 'storyId' => '1', 'body' => 'U2']),
  67. # thread #2:
  68. '400' => new Comment(['id' => '400', 'authorId' => '2', 'storyId' => '2', 'body' => 'Me too']),
  69. '500' => new Comment(['id' => '500', 'authorId' => '2', 'storyId' => '2', 'body' => 'Nice!']),
  70. ];
  71. self::$storyComments = [
  72. '1' => ['100', '200', '300'],
  73. '2' => ['400', '500']
  74. ];
  75. self::$commentReplies = [
  76. '100' => ['110', '120', '130'],
  77. '110' => ['111', '112', '113', '114', '115', '116', '117'],
  78. ];
  79. self::$storyMentions = [
  80. '1' => [
  81. self::$users['2']
  82. ],
  83. '2' => [
  84. self::$stories['1'],
  85. self::$users['3']
  86. ]
  87. ];
  88. }
  89. public static function findUser($id)
  90. {
  91. return isset(self::$users[$id]) ? self::$users[$id] : null;
  92. }
  93. public static function findStory($id)
  94. {
  95. return isset(self::$stories[$id]) ? self::$stories[$id] : null;
  96. }
  97. public static function findComment($id)
  98. {
  99. return isset(self::$comments[$id]) ? self::$comments[$id] : null;
  100. }
  101. public static function findLastStoryFor($authorId)
  102. {
  103. $storiesFound = array_filter(self::$stories, function(Story $story) use ($authorId) {
  104. return $story->authorId == $authorId;
  105. });
  106. return !empty($storiesFound) ? $storiesFound[count($storiesFound) - 1] : null;
  107. }
  108. public static function findLikes($storyId, $limit)
  109. {
  110. $likes = isset(self::$storyLikes[$storyId]) ? self::$storyLikes[$storyId] : [];
  111. $result = array_map(
  112. function($userId) {
  113. return self::$users[$userId];
  114. },
  115. $likes
  116. );
  117. return array_slice($result, 0, $limit);
  118. }
  119. public static function isLikedBy($storyId, $userId)
  120. {
  121. $subscribers = isset(self::$storyLikes[$storyId]) ? self::$storyLikes[$storyId] : [];
  122. return in_array($userId, $subscribers);
  123. }
  124. public static function getUserPhoto($userId, $size)
  125. {
  126. return new Image([
  127. 'id' => $userId,
  128. 'type' => Image::TYPE_USERPIC,
  129. 'size' => $size,
  130. 'width' => rand(100, 200),
  131. 'height' => rand(100, 200)
  132. ]);
  133. }
  134. public static function findLatestStory()
  135. {
  136. return array_pop(self::$stories);
  137. }
  138. public static function findStories($limit, $afterId = null)
  139. {
  140. $start = $afterId ? (int) array_search($afterId, array_keys(self::$stories)) + 1 : 0;
  141. return array_slice(array_values(self::$stories), $start, $limit);
  142. }
  143. public static function findComments($storyId, $limit = 5, $afterId = null)
  144. {
  145. $storyComments = isset(self::$storyComments[$storyId]) ? self::$storyComments[$storyId] : [];
  146. $start = isset($after) ? (int) array_search($afterId, $storyComments) + 1 : 0;
  147. $storyComments = array_slice($storyComments, $start, $limit);
  148. return array_map(
  149. function($commentId) {
  150. return self::$comments[$commentId];
  151. },
  152. $storyComments
  153. );
  154. }
  155. public static function findReplies($commentId, $limit = 5, $afterId = null)
  156. {
  157. $commentReplies = isset(self::$commentReplies[$commentId]) ? self::$commentReplies[$commentId] : [];
  158. $start = isset($after) ? (int) array_search($afterId, $commentReplies) + 1: 0;
  159. $commentReplies = array_slice($commentReplies, $start, $limit);
  160. return array_map(
  161. function($replyId) {
  162. return self::$comments[$replyId];
  163. },
  164. $commentReplies
  165. );
  166. }
  167. public static function countComments($storyId)
  168. {
  169. return isset(self::$storyComments[$storyId]) ? count(self::$storyComments[$storyId]) : 0;
  170. }
  171. public static function countReplies($commentId)
  172. {
  173. return isset(self::$commentReplies[$commentId]) ? count(self::$commentReplies[$commentId]) : 0;
  174. }
  175. public static function findStoryMentions($storyId)
  176. {
  177. return isset(self::$storyMentions[$storyId]) ? self::$storyMentions[$storyId] :[];
  178. }
  179. }