Suggest a feature
×

Welcome to TagMyCode

Please login or create account to add a snippet.
0
0
 
0
Language: Javascript
Posted by: Mirko Jelic
Added: Jun 1, 2019 10:00 AM
Modified: Jun 1, 2019 10:05 AM
Views: 2
Tags: no tags
  1. import express from 'express'
  2. import path from 'path'
  3. import {createServer} from'http'
  4. import { ApolloServer,makeExecutableSchema,PubSub } from 'apollo-server-express'
  5. import cors from 'cors'
  6. import { importSchema } from 'graphql-import'
  7. import {Query} from './resolvers/Query'
  8. import {Mutation} from './resolvers/Mutation'
  9. import {Subscription} from './resolvers/Subscription'
  10.  
  11. const app = express()
  12. const httpServer = createServer(app)
  13. const pubsub = new PubSub()
  14.  
  15. app.use(cors())
  16. app.use(express.static('public'))
  17.  
  18. if (process.env.NODE_ENV === 'production'){
  19.   app.use(express.static('client/build'))
  20.   app.get('/*', (req,res) => {
  21.     res.sendFile(path.resolve(__dirname, 'client', 'build', 'index.html'))
  22.   })
  23. }
  24. const typeDefs = importSchema('./server/src/schema/schema.graphql')
  25. const schema = makeExecutableSchema({
  26.   typeDefs,
  27.   resolvers: {
  28.     Query,
  29.     Mutation,
  30.     Subscription
  31.   }
  32. })
  33. const server = new ApolloServer({
  34.   schema:schema,
  35.   context: {
  36.     pubsub
  37.   }
  38. });
  39. server.applyMiddleware({ app });
  40. server.installSubscriptionHandlers(httpServer);
  41. httpServer.listen(PORT, () => {
  42.   console.log(`Server ready at http://localhost:${PORT}${server.graphqlPath}`)
  43.   console.log(`Subscriptions ready at ws://localhost:${PORT}${server.subscriptionsPath}`)
  44. })