Last active
February 20, 2017 23:04
-
-
Save RyanCCollins/3de8d8c0c5c8f73071aa70d7a0be9cb8 to your computer and use it in GitHub Desktop.
This file contains 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 'babel-polyfill'; | |
import path from 'path'; | |
import fs from 'fs'; | |
import * as express from 'express'; | |
import { graphql } from 'graphql'; | |
import { introspectionQuery } from 'graphql/utilities'; | |
import bodyParser from 'body-parser'; | |
import cors from 'cors'; | |
import { graphiqlExpress, graphqlExpress } from 'graphql-server-express'; | |
import schema from './graph'; | |
// In order to support the GraphQL tooling, we need to | |
// send the introspection query to generate a schema.json file | |
// that we will save to the server/graph directory. | |
function createSchema() { | |
return new Promise<string>((res, rej) => { | |
graphql(schema, introspectionQuery) | |
.then((json) => { | |
fs.writeFile( | |
path.join(__dirname, './graph/schema.json'), | |
JSON.stringify(json, null, 2), | |
(err) => { | |
if (err) { | |
rej(`Error occured while creating graphql schema. ${err}`); | |
} | |
res('Schema successfully created'); | |
}, | |
); | |
}); | |
}); | |
} | |
export default function graphqlEntry(app): Promise<express.Application> { | |
return new Promise<express.Application>(async(res, rej) => { | |
// Binding bodyParser is needed to ensure that the graphQL package can parse the queries. | |
app.use(bodyParser.json()); | |
app.use(bodyParser.urlencoded({ extended: true })); | |
// Bind the graphQLExpress package to the one /api endpoint | |
// so that we can send requests to it. | |
app.use('/api', cors(), graphqlExpress({ | |
schema, | |
})); | |
// Bind the GraphiQL ui app to the /graphql-ui endpoint | |
// and connect it to the graphql endpoint as defined above | |
app.use('/graphql-ui', graphiqlExpress({ | |
endpointURL: '/api', | |
})); | |
await createSchema().catch((err) => rej(err)); | |
res(app); | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment