Docs / Integrations / Integration with Koa Integration with Koa GraphQL Yoga v4 documentation (superseded by v6): Koa is 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.
v6latest v5 v4 v3 v2
This is the documentation for the old GraphQL Yoga v4
We recommend upgrading to the latest GraphQL Yoga v6.
Migrate to GraphQL Yoga v6 .
Koa is 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.
GraphQL Yoga 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 Yoga.
Installation
npm yarn pnpm bun
npm i koa graphql-yoga graphql yarn add koa graphql-yoga graphql pnpm add koa graphql-yoga graphql bun add koa graphql-yoga graphql
Example
import { createYoga } from 'graphql-yoga'
import Koa from 'koa'
const app = new Koa ()
const yoga = createYoga < Koa . ParameterizedContext >()
// Bind GraphQL Yoga to `/graphql` endpoint
app. use ( async ctx => {
// Second parameter adds Koa's context into GraphQL Context
const response = await yoga. handleNodeRequest (ctx.req, 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
← Fastify NestJS →