index.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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. if (await Post.count()){
  42. return;
  43. }
  44. var post1 = await Post.create( {
  45. title: 'First Post',
  46. text: 'First Post BLah-blah-blah'
  47. })
  48. var post2 = await Post.create(
  49. {
  50. title: 'Second Post',
  51. text: 'Second Post BLah-blah-blah'
  52. })
  53. var post3 = await Post.create(
  54. {
  55. title: 'Third Post',
  56. text: 'Third Post BLah-blah-blah'
  57. })
  58. var comment1 = await Comment.create({text: 'test comment for first post'})
  59. var comment11 = await Comment.create({text: 'test comment for first comment of first post'})
  60. var comment12 = await Comment.create({text: 'test comment 2 for first comment of first post'})
  61. post1.addComment(comment1)
  62. comment1.addComment(comment11);
  63. comment1.addComment(comment12);
  64. var comment2 = await Comment.create({text: 'test comment for second post'})
  65. var comment21 = await Comment.create({text: 'test comment for first comment of second post'})
  66. var comment22 = await Comment.create({text: 'test comment 2 for first comment of second post'})
  67. comment2.addComment(comment21);
  68. comment21.addComment(comment22);
  69. post2.addComment(comment2)
  70. var tag1 = await Tag.create({title: 'tag1'})
  71. var tag2 = await Tag.create({title: 'tag2'})
  72. var tag3 = await Tag.create({title: 'tag3'})
  73. tag1.addPosts([post1, post3])
  74. post1.addTags([tag2])
  75. post2.addTags([tag2, tag3])
  76. tag3.addPost(post3)
  77. }
  78. fillDB()
  79. var cookieSession = require('cookie-session')
  80. var express = require('express')
  81. var express = require('express');
  82. const cors = require('cors')
  83. var express_graphql = require('express-graphql');
  84. var { buildSchema } = require('graphql');
  85. // GraphQL schema
  86. //
  87. var schema = buildSchema(`
  88. type Query {
  89. post(id: Int!): Post
  90. comments(id: Int!): [Comment]
  91. subComments(id: Int!): [Comment]
  92. posts: [Post]
  93. }
  94. type Mutation {
  95. createPost(title: String!, text: String!): Post
  96. createComment(postID: Int!, text: String!, commentID: Int): Comment
  97. }
  98. type Post {
  99. id: Int
  100. title: String
  101. text: String
  102. age: String
  103. tagz: [String]
  104. comments: [Comment]
  105. timestamp: Int
  106. }
  107. type Comment {
  108. id: Int
  109. text: String
  110. age: String
  111. commentId: Int
  112. }
  113. `);
  114. async function getPost({id}, {session}){
  115. console.log(session)
  116. session.lastId = id;
  117. console.log(session)
  118. let post = await Post.findOne({
  119. where: {
  120. id
  121. }
  122. })
  123. return post;
  124. }
  125. async function getPosts(args, {session}){
  126. let posts = await Post.findAll({})
  127. for (let post of posts){
  128. post.timestamp = post.createdAt.getTime()/1000
  129. }
  130. return posts;
  131. }
  132. function getPostComments(args){
  133. let id = args.id
  134. return Post.findById(id)
  135. .then( post => post.getComments() )
  136. .then( comments => comments.filter( comment => !comment.commentId))
  137. }
  138. async function getSubComments(args){
  139. let id = args.id
  140. //return Comment.findById(id).then( comment => comment.getComments() )
  141. let comment = await Comment.findById(id)
  142. return comment.getComments()
  143. }
  144. async function createPost({title, text}){
  145. return Post.create({title, text})
  146. }
  147. async function createComment({postID, text, commentID}){
  148. let post = await Post.findById(postID)
  149. let comment = await Comment.create({text, commentId: commentID})
  150. post.addComment(comment)
  151. return comment
  152. }
  153. // Root resolver
  154. var root = {
  155. post: getPost,
  156. posts: getPosts,
  157. comments: getPostComments,
  158. subComments: getSubComments,
  159. createPost,
  160. createComment,
  161. };
  162. // Create an express server and a GraphQL endpoint
  163. var app = express();
  164. app.use(cors({origin: 'http://localhost:3000',
  165. credentials: true}))
  166. app.use(cookieSession({
  167. name: 'session',
  168. keys: ['SuperSecretKey'],
  169. // Cookie Options
  170. maxAge: 24 * 60 * 60 * 1000 // 24 hours
  171. }))
  172. app.use('/graphql', express_graphql(req => ({
  173. schema: schema,
  174. rootValue: root,
  175. graphiql: true,
  176. context: (console.log(req.session),req.session.counter = (req.session.counter || 0) +1, ({session: req.session}))
  177. })));
  178. app.listen(4000, '0.0.0.0', () => console.log('Express GraphQL Server Now Running On localhost:4000/graphql'));
  179. //query getPost($postID: Int!){
  180. //post(id:$postID){
  181. //text
  182. //timestamp
  183. //}
  184. //}
  185. //query getComments($postID: Int!){
  186. //comments(id:$postID){
  187. //text
  188. //}
  189. //}
  190. //query getPostWithComments($postID: Int!){
  191. //post(id:$postID){
  192. //text
  193. //comments {
  194. //text
  195. //age
  196. //}
  197. //}
  198. //}
  199. //query getSubComments($commentID: Int!){
  200. //subComments(id:$commentID){
  201. //text
  202. //}
  203. //}
  204. //mutation createPost($title: String!, $text:String!) {
  205. //createPost(title: $title, text: $text) {
  206. //title
  207. //text
  208. //}
  209. //}
  210. //mutation createComment($postID:Int!, $text:String!) {
  211. //createComment(postID: $postID, text: $text) {
  212. //text
  213. //}
  214. //}