Integration with Koa
💡
This page is currently under construction and expected to change. Please feel free to reach out to us directly in case you are having any troubles.
GraphQL Mesh can be integrated easily as a route to the existing Koa application with a few lines of code.
So you can benefit middlewares written for Koa with GraphQL Mesh.
Example
import Koa from 'koa'
import { createServeRuntime } from '@graphql-mesh/serve-runtime'
const app = new Koa()
const serveRuntime = createServeRuntime<Koa.ParameterizedContext>()
// Bind GraphQL Mesh to `/graphql` endpoint
app.use(async ctx => {
// Second parameter adds Koa's context into GraphQL Context
const response = await serveRuntime.handleNodeRequestAndResponse(ctx.req, ctx.res, ctx)
// Set status code
ctx.status = response.status
// Set headers
response.headers.forEach((value, key) => {
ctx.append(key, value)
})
if (response.body) {
// Set body
ctx.body = response.body
}
})
app.listen(4000, () => {
console.log(
`Running a GraphQL API server at http://localhost:4000/${serveRuntime.graphqlEndpoint}`
)
})