index.js 2.5 KB

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