Skip to content

Latest commit

 

History

History
26 lines (17 loc) · 1008 Bytes

README.md

File metadata and controls

26 lines (17 loc) · 1008 Bytes

🐨 Async Middleware 🐨

Introduction

This add-on wraps an asynchronously-initialised middleware to allow it to be synchronously attached to a Koa application. This is useful if you want to keep the initialisation of your Koa application synchronous for simplicity and interoperability with tooling like Koa Cluster, but you need to perform asynchronous work like GraphQL schema introspection in order to build one of the middleware in your chain. You can optionally set a TTL so the inner middleware can be reinitialised when the cache has expired.

Usage

import { AsyncMiddleware } from 'seek-koala';

const initGraphMiddleware: = async () => {
  const schema = await introspectSchema();

  return new GraphServer(schema).getMiddleware();
};

const graphMiddleware = AsyncMiddleware.lazyLoad(initGraphMiddleware, 120000);

app.use(graphMiddleware);