UNPKG

itch-graphql

Version:

GraphQL server/types and resolvers for the itch.io API

39 lines (33 loc) 1.05 kB
import { microGraphiql, microGraphql } from 'apollo-server-micro' import micro, { send } from 'micro' import { get, post, router } from 'microrouter' import { makeExecutableSchema } from 'graphql-tools' import { typeDefs, resolvers } from './types' const schema = makeExecutableSchema({ typeDefs, resolvers }) const graphqlHandler = microGraphql((req, res) => { return { schema, context: { itchToken: process.env.ITCH_TOKEN ? process.env.ITCH_TOKEN : req.headers.ItchToken } } }) const graphiqlHandler = microGraphiql({ endpointURL: '/graphql' }) const server = function (port, graphiql = true) { return micro( router( get('/graphql', graphqlHandler), post('/graphql', graphqlHandler), get( '/graphiql', graphiql ? graphiqlHandler : (req, res) => send(res, 405, 'sorry bub') ), (req, res) => send(res, 404, 'whatcha lookin for (oh four)') ) ).listen(port, () => { console.log(`hmu on port ${port}`) }) } export { server, typeDefs, resolvers }