Integrate GraphQL Yoga with Koa, a new web framework designed by the team behind Express, which aims to be a smaller, more expressive, and more robust foundation for web applications and APIs.
import { createYoga } from 'graphql-yoga'import Koa from 'koa'const app = new Koa()const yoga = createYoga<Koa.ParameterizedContext>()// Bind GraphQL Yoga to `/graphql` endpointapp.use(async ctx => { // Second parameter adds Koa's context into GraphQL Context // If you use any body parsing middleware in your application, // Make sure it is `ctx.request` and not `ctx.req` const response = await yoga.handleNodeRequestAndResponse(ctx.request, ctx.res, ctx) // Set status code ctx.status = response.status // Set headers response.headers.forEach((value, key) => { ctx.append(key, value) }) // Converts ReadableStream to a NodeJS Stream ctx.body = response.body})app.listen(4000, () => { console.log(`Running a GraphQL API server at http://localhost:4000/${yoga.graphqlEndpoint}`)})
You can also check a full example on our GitHub repository
here
This site uses cookies for analytics and improving your experience.