index.ts 1.5 KB

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