Created
December 4, 2019 19:36
-
-
Save timsuchanek/effa559bf8eae18dfc45bd565f8fb8a7 to your computer and use it in GitHub Desktop.
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 { GraphQLSchema } from 'graphql' | |
import polka from 'polka' | |
import { json } from 'body-parser' | |
import send from '@polka/send-type' | |
import { renderPlaygroundPage } from 'graphql-playground-html' | |
import { graphql } from 'graphql' | |
import { Server } from 'http' | |
export function serveGraphql(schema: GraphQLSchema): Server { | |
const { PORT = 4000 } = process.env | |
const app = polka() | |
.use(json()) | |
.get('/', (req, res) => { | |
res.writeHead(200, { 'Content-Type': 'text/html; charset=utf-8' }) | |
res.end( | |
renderPlaygroundPage({ | |
version: '1.7.20', | |
}), | |
) | |
}) | |
.post('/', async (req, res) => { | |
const { variables, query } = req.body | |
const data = await graphql({ | |
schema, | |
source: query, | |
variableValues: variables, | |
}) | |
send(res, 200, data) | |
}) | |
.listen(PORT, err => { | |
if (err) throw err | |
console.log(`> Ready on http://localhost:${PORT}/`) | |
}) | |
return app.server | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment