index.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. const jwtSecret = 'CbymrfGfnB'
  2. const express = require('express');
  3. const express_graphql = require('express-graphql');
  4. const { buildSchema, printSchema } = require('graphql');
  5. const expand = require('./expand')
  6. ;(async () => {
  7. const {Savable, slice, getModels} = await require('./models.js')()
  8. const jwtGQL = require('./jwt').jwtGQL
  9. const {anonSchema, anonResolvers} = require('./anon')({Savable, secret: jwtSecret})
  10. let schema = buildSchema(`
  11. type User {
  12. _id: String
  13. createdAt: String
  14. login: String
  15. nick : String
  16. orders: [Order]
  17. }
  18. input UserInput {
  19. _id: String
  20. login: String
  21. nick : String
  22. }
  23. type Category {
  24. _id: ID,
  25. createdAt: String
  26. name: String!,
  27. goods: [Good]
  28. }
  29. input CategoryInput {
  30. _id: ID,
  31. name: String,
  32. goods: [GoodInput]
  33. }
  34. type Good {
  35. _id: ID,
  36. createdAt: String
  37. name: String!,
  38. description: String
  39. price: Float
  40. imgUrls: [String]
  41. orderGoods: [OrderGood]
  42. categories: [Category]
  43. }
  44. input GoodInput {
  45. _id: ID,
  46. name: String,
  47. description: String
  48. imgUrls: [String]
  49. price: Float
  50. categories: [CategoryInput]
  51. }
  52. type OrderGood {
  53. _id: ID,
  54. createdAt: String
  55. price: Float,
  56. count: Float,
  57. good: Good,
  58. order: Order
  59. total: Float
  60. }
  61. input OrderGoodInput {
  62. _id: ID,
  63. count: Int,
  64. good: GoodInput,
  65. order: OrderInput
  66. }
  67. type Order {
  68. _id: ID
  69. createdAt: String
  70. orderGoods: [OrderGood]
  71. total: Float
  72. }
  73. input OrderInput {
  74. _id: ID
  75. orderGoods: [OrderGoodInput]
  76. }
  77. `);
  78. schema = expand(schema)
  79. console.log(printSchema(schema))
  80. const app = express();
  81. app.use(express.static('public'));
  82. app.use('/graphql', express_graphql(jwtGQL({anonSchema, anonResolvers, schema, createContext: getModels, graphiql: true, secret: jwtSecret})))
  83. app.listen(4000, () => console.log('Express GraphQL Server Now Running On localhost:4000/graphql'));
  84. })()