Integration with Hapi

Hapi allows you to build powerful, scalable applications, with minimal overhead and full out-of-the-box functionality.

Hive Gateway can be integrated easily as a route to the existing Hapi application with a few lines of code.

Example

import http from 'node:http'
import { Readable } from 'node:stream'
import { createGatewayRuntime } from '@graphql-hive/gateway-runtime'
import Hapi from '@hapi/hapi'
import { schema } from './my-graphql-schema'
 
interface ServerContext {
  req: Hapi.Request
  h: Hapi.ResponseToolkit
}
 
const hiveGateway = createGatewayRuntime<ServerContext>(/* Your configuration */)
 
const server = Hapi.server({ port: 4000 })
 
server.route({
  method: '*',
  path: hiveGateway.graphqlEndpoint,
  options: {
    payload: {
      // let hiveGateway handle the parsing
      output: 'stream'
    }
  },
  handler: async (req, h) => {
    const { status, headers, body } = await hiveGateway.handleNodeRequestAndResponse(
      req.raw.req,
      req.raw.res,
      {
        req,
        h
      }
    )
 
    const res = h.response(
      Readable.from(body, {
        // hapi needs the stream not to be in object mode
        objectMode: false
      })
    )
 
    for (const [key, val] of headers) {
      res.header(key, val)
    }
 
    return res.code(status)
  }
})
 
server.start()

Hive Gateway should now be available at http://localhost:4000/graphql.