index.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. }
  56. input CategoryInput {
  57. _id: ID,
  58. name: String!,
  59. goods: [GoodInput]
  60. image: ImageInput
  61. }
  62. type Good {
  63. _id: ID,
  64. createdAt: String
  65. name: String,
  66. description: String
  67. price: Float
  68. orderGoods: [OrderGood]
  69. categories: [Category]
  70. images: [Image]
  71. owner: User
  72. }
  73. input GoodInput {
  74. _id: ID,
  75. name: String,
  76. description: String
  77. price: Float
  78. categories: [CategoryInput]
  79. images: [ImageInput]
  80. }
  81. type OrderGood {
  82. _id: ID,
  83. createdAt: String
  84. price: Float,
  85. count: Float,
  86. good: Good,
  87. order: Order
  88. owner: User
  89. total: Float
  90. }
  91. input OrderGoodInput {
  92. _id: ID,
  93. count: Int!,
  94. good: GoodInput,
  95. order: OrderInput
  96. }
  97. type Order {
  98. _id: ID
  99. createdAt: String
  100. total: Float
  101. orderGoods: [OrderGood]
  102. owner: User
  103. }
  104. input OrderInput {
  105. _id: ID
  106. orderGoods: [OrderGoodInput]
  107. }
  108. `);
  109. schema = expand(schema, {
  110. login:{
  111. type: GraphQLString,
  112. args: {login: {type: GraphQLString},
  113. password: {type: GraphQLString},
  114. },
  115. async resolve(root, {login, password}, context, info){
  116. const Savable = context.models.Savable
  117. const user = await Savable.m.User.findOne({login, password})
  118. if (!user)
  119. return null;
  120. const token = jwt.sign({ sub: {id: user._id, login, acl: user.acl}}, jwtSecret); //подписывам токен нашим ключем
  121. return token
  122. }
  123. }
  124. })
  125. console.log(printSchema(schema))
  126. const app = express();
  127. app.use(express.static('public'));
  128. app.use('/graphql', express_graphql(jwtGQLAnon({schema, createContext: getModels, graphiql: true, secret: jwtSecret})))
  129. app.post('/upload', upload.single('photo'), async (req, res, next) => {
  130. let decoded;
  131. if (decoded = jwtCheck(req, jwtSecret)){
  132. console.log('SOME UPLOAD', decoded, req.file)
  133. let {models: {Image }} = await getModels(decoded.sub)
  134. let image = await Image.fromFileData(req.file)
  135. res.end(JSON.stringify({_id: image._id, url: image.url}))
  136. }
  137. else {
  138. res.status(503).send('permission denied')
  139. }
  140. })
  141. app.use(express.static('public'));
  142. let socketPath = "/home/asmer/node_hosts/shop-roles"
  143. app.listen(socketPath, () => {
  144. console.log(`Express GraphQL Server Now Running On ${socketPath}/graphql`);
  145. fs.chmodSync(socketPath, '777');
  146. });
  147. })()