Integration with Koa

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.

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.

Hive Gateway 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 Hive Gateway.

Example

import Koa from "koa";
import { createGatewayRuntime } from "@graphql-hive/gateway-runtime";

const app = new Koa();

const gatewayRuntime = createGatewayRuntime<Koa.ParameterizedContext>();

// Bind Hive Gateway to `/graphql` endpoint
app.use(async (ctx) => {
  // Second parameter adds Koa's context into GraphQL Context
  const response = await gatewayRuntime.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/${gatewayRuntime.graphqlEndpoint}`,
  );
});

On this page

View on GitHub