index.ts 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. import dotenv from 'dotenv'
  2. dotenv.config()
  3. import express from 'express'
  4. import cookieParser from 'cookie-parser'
  5. import { ApolloServer, ApolloServerExpressConfig } from 'apollo-server-express'
  6. import { merge } from 'lodash'
  7. import { importSchema } from 'graphql-import'
  8. import { db, populateUser } from './src/db'
  9. import { authenticate } from './src/user/authenticate'
  10. import file from './src/file'
  11. import user from './src/user'
  12. import history from './src/history'
  13. import training from './src/training'
  14. const resolvers = merge(
  15. file.resolvers,
  16. training.resolvers,
  17. user.resolvers,
  18. history.resolvers
  19. //google.resolvers
  20. )
  21. const typeDefs = importSchema('./schema.graphql').replace('scalar Upload', '')
  22. // scalar Upload has to be in schema, or importSchema will complain.
  23. // But when it's in the schema, Apollo Server will still add it.
  24. // Workaround: remove it manually here after the import.
  25. // https://github.com/ardatan/graphql-import/issues/286
  26. const corsOptions = {
  27. credentials: true,
  28. /*origin: process.env.FRONTEND_URL*/
  29. }
  30. const app = express()
  31. app.use(cookieParser())
  32. app.use(authenticate)
  33. app.use(populateUser)
  34. const server = new ApolloServer({
  35. typeDefs,
  36. resolvers,
  37. playground: true,
  38. context: (req: any) => ({
  39. ...req,
  40. db,
  41. }),
  42. debug: false,
  43. engine: { debugPrintReports: false },
  44. } as ApolloServerExpressConfig)
  45. server.applyMiddleware({ app, cors: corsOptions, path: '/' })
  46. app.listen({ port: process.env.PORT }, () => {
  47. console.log(`Server ready on port ${process.env.PORT} 🚀!`)
  48. })
  49. export default app