index.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. var Sequelize = require('sequelize');
  2. var sequelize = new Sequelize('test', '', '',
  3. {
  4. host: 'localhost',
  5. dialect: 'mysql',
  6. pool: {
  7. max: 5,
  8. min: 0,
  9. idle: 10000
  10. },
  11. //logging: false
  12. }
  13. );
  14. var Post = sequelize.define('post', {
  15. title: Sequelize.STRING,
  16. text: Sequelize.TEXT,
  17. },{
  18. getterMethods: {
  19. tagz() {
  20. return this.getTags().then(tagz => tagz.map(tag => tag.title))
  21. },
  22. age() {
  23. return (((new Date).getTime() - this.createdAt.getTime()) / 3600000).toFixed(0) + ' hrs ago';
  24. }
  25. },
  26. })
  27. var Comment = sequelize.define('comment',{
  28. text: Sequelize.TEXT
  29. })
  30. Post.hasMany(Comment)
  31. Comment.belongsTo(Post)
  32. Comment.hasMany(Comment)
  33. Comment.belongsTo(Comment)
  34. var Tag = sequelize.define('tag', {
  35. title: Sequelize.STRING
  36. })
  37. Tag.belongsToMany(Post, {through: 'PostTag'})
  38. Post.belongsToMany(Tag, {through: 'PostTag'})
  39. async function fillDB(){
  40. await sequelize.sync()
  41. var post1 = await Post.create( {
  42. title: 'First Post',
  43. text: 'First Post BLah-blah-blah'
  44. })
  45. var post2 = await Post.create(
  46. {
  47. title: 'Second Post',
  48. text: 'Second Post BLah-blah-blah'
  49. })
  50. var post3 = await Post.create(
  51. {
  52. title: 'Third Post',
  53. text: 'Third Post BLah-blah-blah'
  54. })
  55. var comment1 = await Comment.create({text: 'test comment for first post'})
  56. var comment11 = await Comment.create({text: 'test comment for first comment of first post'})
  57. var comment12 = await Comment.create({text: 'test comment 2 for first comment of first post'})
  58. post1.addComment(comment1)
  59. comment1.addComment(comment11);
  60. comment1.addComment(comment12);
  61. var comment2 = await Comment.create({text: 'test comment for second post'})
  62. var comment21 = await Comment.create({text: 'test comment for first comment of second post'})
  63. var comment22 = await Comment.create({text: 'test comment 2 for first comment of second post'})
  64. comment2.addComment(comment21);
  65. comment21.addComment(comment22);
  66. post2.addComment(comment2)
  67. var tag1 = await Tag.create({title: 'tag1'})
  68. var tag2 = await Tag.create({title: 'tag2'})
  69. var tag3 = await Tag.create({title: 'tag3'})
  70. tag1.addPosts([post1, post3])
  71. post1.addTags([tag2])
  72. post2.addTags([tag2, tag3])
  73. tag3.addPost(post3)
  74. }
  75. //fillDB()
  76. //
  77. //
  78. async function getData(){
  79. var tag1 = await Tag.findOne({
  80. where: {
  81. title: 'tag1'
  82. }
  83. })
  84. //console.log(await tag1.getPosts())
  85. for (let post of await tag1.getPosts()){
  86. console.log('TAG1', post.title)
  87. }
  88. var comment1 = await Comment.findOne({
  89. where: {
  90. id: 1
  91. }
  92. })
  93. for (let comment of await comment1.getComments()){
  94. console.log('comment1 sub comment:', comment.text)
  95. console.log('parent', (await comment.getComment()).text)
  96. }
  97. var post1 =(await comment1.getPost());
  98. console.log(`${post1.text} at ${post1.createdAt}`)
  99. console.log(await post1.tagz)
  100. console.log(post1.age)
  101. }
  102. getData()
  103. var express = require('express');
  104. var express_graphql = require('express-graphql');
  105. var { buildSchema } = require('graphql');
  106. // GraphQL schema
  107. //
  108. var schema = buildSchema(`
  109. type Query {
  110. post(id: Int!): Post
  111. comments(id: Int!): [Comment]
  112. subComments(id: Int!): [Comment]
  113. }
  114. type Mutation {
  115. createPost(title: String!, text: String!): Post
  116. createComment(postID: Int!, text: String!): Post
  117. }
  118. type Post {
  119. id: Int
  120. title: String
  121. text: String
  122. age: String
  123. tagz: [String]
  124. comments: [Comment]
  125. timestamp: Int
  126. }
  127. type Comment {
  128. id: Int
  129. text: String
  130. age: String
  131. }
  132. `);
  133. async function getPost(args){
  134. let id = args.id
  135. //return Post.findById(id).then( post => (post.comments = post.getComments(), post) )
  136. let post = await Post.findById(id)
  137. post.comments = await post.getComments()
  138. post.timestamp = post.createdAt.getTime()/1000
  139. //console.log(post.createdAt, typeof post.createdAt, post.createdAt.getTime())
  140. return post;
  141. }
  142. function getPostComments(args){
  143. let id = args.id
  144. return Post.findById(id).then( post => post.getComments() )
  145. }
  146. async function getSubComments(args){
  147. let id = args.id
  148. //return Comment.findById(id).then( comment => comment.getComments() )
  149. let comment = await Comment.findById(id)
  150. return comment.getComments()
  151. }
  152. async function createPost({title, text}){
  153. return Post.create({title, text})
  154. }
  155. async function createComment({postID, text}){
  156. let post = await Post.findById(postID)
  157. let comment = await Comment.create({text})
  158. post.addComment(comment)
  159. return comment
  160. }
  161. // Root resolver
  162. var root = {
  163. post: getPost,
  164. comments: getPostComments,
  165. subComments: getSubComments,
  166. createPost,
  167. createComment,
  168. };
  169. // Create an express server and a GraphQL endpoint
  170. var app = express();
  171. app.use('/graphql', express_graphql({
  172. schema: schema,
  173. rootValue: root,
  174. graphiql: true
  175. }));
  176. app.listen(4000, () => console.log('Express GraphQL Server Now Running On localhost:4000/graphql'));
  177. //query getPost($postID: Int!){
  178. //post(id:$postID){
  179. //text
  180. //timestamp
  181. //}
  182. //}
  183. //query getComments($postID: Int!){
  184. //comments(id:$postID){
  185. //text
  186. //}
  187. //}
  188. //query getPostWithComments($postID: Int!){
  189. //post(id:$postID){
  190. //text
  191. //comments {
  192. //text
  193. //age
  194. //}
  195. //}
  196. //}
  197. //query getSubComments($commentID: Int!){
  198. //subComments(id:$commentID){
  199. //text
  200. //}
  201. //}
  202. //mutation createPost($title: String!, $text:String!) {
  203. //createPost(title: $title, text: $text) {
  204. //title
  205. //text
  206. //}
  207. //}
  208. //mutation createComment($postID:Int!, $text:String!) {
  209. //createComment(postID: $postID, text: $text) {
  210. //text
  211. //}
  212. //}