index.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. ;(async () => {
  7. const {Savable, slice, getModels} = await require('./models.js')()
  8. const jwtGQL = require('mm-graphql/jwt')
  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. avatar: Image
  17. likes: [Like]
  18. likesCount: Int
  19. }
  20. input UserInput {
  21. _id: String
  22. login: String
  23. nick : String
  24. avatar: ImageInput
  25. }
  26. type Like {
  27. _id: ID,
  28. post: Post,
  29. comment: Comment,
  30. direct: Direct,
  31. user: User,
  32. }
  33. input LikeInput {
  34. _id: ID,
  35. post: Post,
  36. comment: CommentInput,
  37. direct: DirectInput,
  38. user: UserInput,
  39. }
  40. type Post {
  41. _id: ID,
  42. createdAt: String
  43. text: String,
  44. images: [Image],
  45. comments: [Comment],
  46. directs: [Direct],
  47. collections: [Collection]
  48. likes: [Like]
  49. likesCount: Int
  50. }
  51. input PostInput {
  52. _id: ID,
  53. text: String,
  54. images: [ImageInput],
  55. comments: [CommentInput],
  56. directs: [DirectInput],
  57. collections: [CollectionInput]
  58. }
  59. type Image {
  60. _id: ID,
  61. text: String,
  62. url: String,
  63. originalFileName: String,
  64. userAvatar: User,
  65. posts: [Post],
  66. directs: [Direct]
  67. }
  68. input ImageInput {
  69. _id: ID,
  70. userAvatar: User,
  71. posts: [Post],
  72. directs: [Direct]
  73. }
  74. type Comment {
  75. _id: ID,
  76. createdAt: String,
  77. text: String,
  78. post: Post,
  79. answers: [Comment],
  80. answerTo: Comment
  81. likes: [Like]
  82. likesCount: Int
  83. }
  84. input CommentInput {
  85. _id: ID,
  86. text: String,
  87. post: PostInput,
  88. answers: [CommentInput],
  89. answerTo: CommentInput
  90. }
  91. type Direct {
  92. _id: ID,
  93. createdAt: String,
  94. text: String,
  95. post: Post,
  96. image: Image,
  97. likes: [Like]
  98. likesCount: Int
  99. }
  100. input DirectInput {
  101. _id: ID,
  102. text: String,
  103. post: PostInput,
  104. image: ImageInput,
  105. likes: [LikeInput]
  106. }
  107. type Collection {
  108. _id: ID,
  109. text: String,
  110. posts: [Post]
  111. }
  112. input CollectionInput {
  113. _id: ID,
  114. text: String,
  115. posts: [Post]
  116. }
  117. `);
  118. schema = expand(schema)
  119. console.log(printSchema(schema))
  120. const app = express();
  121. app.use(express.static('public'));
  122. app.use('/graphql', express_graphql(jwtGQL({anonSchema, anonResolvers, schema, createContext: getModels, graphiql: true, secret: jwtSecret})))
  123. app.listen(4000, () => console.log('Express GraphQL Server Now Running On localhost:4000/graphql'));
  124. })()