import * as express from 'express'
import * as bodyParser from 'body-parser'
import { graphqlExpress } from 'apollo-server-express'
import { makeExecutableSchema } from 'graphql-tools'
import Stack from 'graphql-stack'
import { authMiddleware, metricsMiddleware } from './middlewares'
const typeDefs = `
type Query {
hello(name: String): String
}
`
const resolvers = {
Query: {
hello: (root, args: { name }, context) => `Hello ${name ? name : 'world'}!`,
},
}
const schema = makeExecutableSchema({ typeDefs, resolvers })
const stack = new Stack(schema)
stack.use(metricsMiddleware())
stack.use({
Query: {
hello: ({ args, resolve }) => {
const argsWithDefault = { name: 'Bob', ...args }
const result = await resolve({ args: argsWithDefault })
return result.replace(/Trump/g, 'beep')
},
},
})
stack.use({
Query: {
'*': authMiddleware,
},
})
const app = express()
app.use('/graphql', bodyParser.json(), graphqlExpress({ schema: stack.getSchema() }))
app.use('/playground', expressPlayground({ endpoint: '/graphql' }))
app.listen(3000, () => console.log('Server running. Open http://localhost:3000/playground to run queries.'))