index.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. const jwtSecret = 'AjnjLhjxM'
  2. const express = require('express');
  3. const express_graphql = require('express-graphql');
  4. const { buildSchema, printSchema } = require('graphql');
  5. const expand = require('mm-graphql/expand')
  6. const fs = require('fs')
  7. const uploadPath = `${__dirname}/public/images/`;
  8. const upload = require('multer')({ dest: uploadPath })
  9. ;(async () => {
  10. const {Savable, slice, getModels} = await require('./models.js')()
  11. const { jwtGQL, jwtCheck } = require('mm-graphql/jwt')
  12. const {anonSchema, anonResolvers} = require('./anon')({Savable, secret: jwtSecret})
  13. let schema = buildSchema(`
  14. type User {
  15. _id: String
  16. createdAt: String
  17. login: String
  18. nick : String
  19. avatar: Image
  20. likes: [Like]
  21. likesCount: Int
  22. }
  23. input UserInput {
  24. _id: String
  25. login: String
  26. nick : String
  27. avatar: ImageInput
  28. }
  29. type Like {
  30. _id: ID,
  31. post: Post,
  32. comment: Comment,
  33. direct: Direct,
  34. user: User,
  35. }
  36. input LikeInput {
  37. _id: ID,
  38. post: PostInput,
  39. comment: CommentInput,
  40. direct: DirectInput,
  41. user: UserInput,
  42. }
  43. type Post {
  44. _id: ID,
  45. createdAt: String
  46. text: String,
  47. images: [Image],
  48. comments: [Comment],
  49. directs: [Direct],
  50. collections: [Collection]
  51. likes: [Like]
  52. likesCount: Int
  53. }
  54. input PostInput {
  55. _id: ID,
  56. text: String,
  57. images: [ImageInput],
  58. comments: [CommentInput],
  59. directs: [DirectInput],
  60. collections: [CollectionInput]
  61. }
  62. type Image {
  63. _id: ID,
  64. text: String,
  65. url: String,
  66. originalFileName: String,
  67. userAvatar: User,
  68. posts: [Post],
  69. directs: [Direct]
  70. }
  71. input ImageInput {
  72. _id: ID,
  73. text: String,
  74. userAvatar: UserInput,
  75. posts: [PostInput],
  76. directs: [DirectInput]
  77. }
  78. type Comment {
  79. _id: ID,
  80. createdAt: String,
  81. text: String,
  82. post: Post,
  83. answers: [Comment],
  84. answerTo: Comment
  85. likes: [Like]
  86. likesCount: Int
  87. }
  88. input CommentInput {
  89. _id: ID,
  90. text: String,
  91. post: PostInput,
  92. answers: [CommentInput],
  93. answerTo: CommentInput
  94. }
  95. type Direct {
  96. _id: ID,
  97. createdAt: String,
  98. text: String,
  99. post: Post,
  100. image: Image,
  101. likes: [Like]
  102. likesCount: Int
  103. }
  104. input DirectInput {
  105. _id: ID,
  106. text: String,
  107. post: PostInput,
  108. image: ImageInput,
  109. likes: [LikeInput]
  110. }
  111. type Collection {
  112. _id: ID,
  113. text: String,
  114. posts: [Post]
  115. }
  116. input CollectionInput {
  117. _id: ID,
  118. text: String,
  119. posts: [PostInput]
  120. }
  121. `);
  122. schema = expand(schema)
  123. console.log(printSchema(schema))
  124. const app = express();
  125. app.use(express.static('public'));
  126. app.use('/graphql', express_graphql(jwtGQL({anonSchema, anonResolvers, schema, createContext: getModels, graphiql: true, secret: jwtSecret})))
  127. app.post('/upload', upload.single('photo'), async (req, res, next) => {
  128. let decoded;
  129. if (decoded = jwtCheck(req, jwtSecret)){
  130. console.log('SOME UPLOAD', decoded, req.file)
  131. let {models: {Image }} = await getModels(decoded.sub)
  132. let image = await Image.fromFileData(req.file)
  133. res.end(JSON.stringify({_id: image._id, url: image.url}))
  134. }
  135. else {
  136. res.status(503).send('permission denied')
  137. }
  138. })
  139. app.use(express.static('public'));
  140. app.listen(4000, () => console.log('Express GraphQL Server Now Running On localhost:4000/graphql'));
  141. })()