Created
September 23, 2020 03:06
-
-
Save coderinblack08/ec2859c79f42f56a4d1ecc43fea4cca0 to your computer and use it in GitHub Desktop.
Simple type-graphql, typeorm index file
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'reflect-metadata'; | |
import cors from 'cors'; | |
import express from 'express'; | |
import { createConnection } from 'typeorm'; | |
import { port, __prod__ } from './constants'; | |
import { ApolloServer } from 'apollo-server-express'; | |
import { buildSchema } from 'type-graphql'; | |
import { HelloResolver } from './resolvers/HelloResolver'; | |
const main = async () => { | |
await createConnection(); | |
const app = express(); | |
app.use( | |
cors({ | |
origin: __prod__ | |
? 'https://contestpug.vercel.app' | |
: 'http://localhost:3000', | |
credentials: true, | |
}) | |
); | |
const apolloServer = new ApolloServer({ | |
schema: await buildSchema({ | |
resolvers: [HelloResolver], | |
validate: false, | |
}), | |
context: ({ req, res }) => ({ req, res }), | |
}); | |
apolloServer.applyMiddleware({ app, cors: false }); | |
app.get('/', (_req, res) => res.redirect('/graphql')); | |
app.listen(port, () => | |
console.log(`Graphql server lisenting on port ${port}`) | |
); | |
}; | |
main().catch((err) => console.error(err)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment