index.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. const jwtSecret = 'AjnjLhjxM'
  2. const jwt = require('jsonwebtoken')
  3. const express = require('express');
  4. const express_graphql = require('express-graphql');
  5. const { buildSchema, printSchema, GraphQLString } = require('graphql');
  6. const expand = require('mm-graphql/expand')
  7. const fs = require('fs')
  8. const uploadPath = `${__dirname}/public/images/`;
  9. const upload = require('multer')({ dest: uploadPath })
  10. ;(async () => {
  11. const {Savable, slice, getModels} = await require('./models.js')()
  12. const { jwtGQLAnon, jwtCheck } = require('mm-graphql/jwt')
  13. let schema = buildSchema(`
  14. type User {
  15. _id: String
  16. createdAt: String
  17. login: String
  18. nick : String
  19. avatar: Image
  20. acl: [String]
  21. }
  22. input UserInput {
  23. _id: String
  24. login: String
  25. nick : String
  26. password: String
  27. acl: [String]
  28. avatar: ImageInput
  29. }
  30. type Image {
  31. _id: ID,
  32. createdAt: String
  33. text: String,
  34. url: String,
  35. originalFileName: String,
  36. userAvatar: User,
  37. good: Good
  38. category: Category
  39. owner: User
  40. }
  41. input ImageInput {
  42. _id: ID,
  43. text: String,
  44. userAvatar: UserInput,
  45. good: GoodInput
  46. category: CategoryInput
  47. }
  48. type Category {
  49. _id: ID,
  50. createdAt: String
  51. name: String,
  52. goods: [Good]
  53. image: Image
  54. owner: User
  55. parent: Category
  56. subCategories: [Category]
  57. }
  58. input CategoryInput {
  59. _id: ID,
  60. name: String!,
  61. goods: [GoodInput]
  62. image: ImageInput
  63. parent: CategoryInput
  64. subCategories: [CategoryInput]
  65. }
  66. type Good {
  67. _id: ID,
  68. createdAt: String
  69. name: String,
  70. description: String
  71. price: Float
  72. orderGoods: [OrderGood]
  73. categories: [Category]
  74. images: [Image]
  75. owner: User
  76. }
  77. input GoodInput {
  78. _id: ID,
  79. name: String,
  80. description: String
  81. price: Float
  82. categories: [CategoryInput]
  83. images: [ImageInput]
  84. }
  85. type OrderGood {
  86. _id: ID,
  87. createdAt: String
  88. price: Float,
  89. count: Float,
  90. good: Good,
  91. order: Order
  92. owner: User
  93. total: Float
  94. }
  95. input OrderGoodInput {
  96. _id: ID,
  97. count: Int!,
  98. good: GoodInput,
  99. order: OrderInput
  100. }
  101. type Order {
  102. _id: ID
  103. createdAt: String
  104. total: Float
  105. orderGoods: [OrderGood]
  106. owner: User
  107. }
  108. input OrderInput {
  109. _id: ID
  110. orderGoods: [OrderGoodInput]
  111. }
  112. `);
  113. schema = expand(schema, {
  114. login:{
  115. type: GraphQLString,
  116. args: {login: {type: GraphQLString},
  117. password: {type: GraphQLString},
  118. },
  119. async resolve(root, {login, password}, context, info){
  120. const Savable = context.models.Savable
  121. if (!login || !password) return null;
  122. const user = await Savable.m.User.findOne({login, password})
  123. console.log(user, {login, password})
  124. if (!user)
  125. return null;
  126. const token = jwt.sign({ sub: {id: user._id, login, acl: user.acl}}, jwtSecret); //подписывам токен нашим ключем
  127. return token
  128. }
  129. }
  130. })
  131. console.log(printSchema(schema))
  132. const app = express();
  133. app.use(require('cors')())
  134. app.use(express.static('public'));
  135. app.use('/graphql', express_graphql(jwtGQLAnon({schema, createContext: getModels, graphiql: true, secret: jwtSecret})))
  136. app.post('/upload', upload.single('photo'), async (req, res, next) => {
  137. let decoded;
  138. if (decoded = jwtCheck(req, jwtSecret)){
  139. console.log('SOME UPLOAD', decoded, req.file)
  140. let {models: {Image }} = await getModels(decoded.sub)
  141. if (req.file){
  142. let image = await Image.fromFileData(req.file)
  143. res.end(JSON.stringify({_id: image._id, url: image.url}))
  144. }
  145. else {
  146. res.end('дичь')
  147. }
  148. }
  149. else {
  150. res.status(503).send('permission denied')
  151. }
  152. })
  153. app.use(express.static('public'));
  154. let socketPath = 3030
  155. app.listen(socketPath, () => {
  156. console.log(`Express GraphQL Server Now Running On ${socketPath}/graphql`);
  157. //fs.chmodSync(socketPath, '777');
  158. });
  159. })()
  160. process.on('uncaughtException', (error) => {
  161. console.log('UNCAUGHT EXCEPTION', error)
  162. })