Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

@as-integrations/koa

Apollo server integration for koa framework

GraphQL, Apollo, Server, Koa, Javascript

readme

@as-integrations/koa

A TypeScript/JavaScript GraphQL middleware for @apollo/server

Getting started: Koa middleware

Apollo Server enables the ability to add middleware that lets you run your GraphQL server as part of an app built with Koa, one of the most popular web frameworks for Node.

First, install Apollo Server, the JavaScript implementation of the core GraphQL algorithms, Koa, and two common Koa middleware packages:

npm install @as-integrations/koa @apollo/server graphql koa @koa/cors koa-bodyparser

Then, write the following to server.mjs. (By using the .mjs extension, Node lets you use the await keyword at the top level.)

import http from "http";
import Koa from "koa";
import bodyParser from "koa-bodyparser";
import cors from "@koa/cors";
import { ApolloServer } from "@apollo/server";
import { ApolloServerPluginDrainHttpServer } from "@apollo/server/plugin/drainHttpServer";
import { koaMiddleware } from "@as-integrations/koa";

// The GraphQL schema
const typeDefs = `#graphql
  type Query {
    hello: String
  }
`;

// A map of functions which return data for the schema.
const resolvers = {
  Query: {
    hello: () => "world",
  },
};

const app = new Koa();
const httpServer = http.createServer(app.callback());

// Set up Apollo Server
const server = new ApolloServer({
  typeDefs,
  resolvers,
  plugins: [ApolloServerPluginDrainHttpServer({ httpServer })],
});
await server.start();

app.use(cors());
app.use(bodyParser());
app.use(
  koaMiddleware(server, {
    context: async ({ ctx }) => ({ token: ctx.headers.token }),
  })
);

await new Promise((resolve) => httpServer.listen({ port: 4000 }, resolve));
console.log(`🚀 Server ready at http://localhost:4000`);

Now run your server with:

node server.mjs

Open the URL it prints in a web browser. It will show Apollo Sandbox, a web-based tool for running GraphQL operations. Try running the operation query { hello }!

changelog

CHANGELOG

1.1.1

Patch Changes

  • #132 01fa9d1 Thanks @ryota-ka! - Parameterize KoaContextFunctionArgument and KoaMiddlewareOptions so that the ctx parameter in the context function is correctly typed when this library is used along with a context-parameterized Koa app.

1.1.0

Minor Changes

  • #130 c504479 Thanks @ryota-ka! - Parameterize the returned Middleware type so that this library can be used along with a context-parameterized Koa app.

    In order to utilize this feature, provide your Koa app's State and Context types as type arguments to the koaMiddleware function.

    If you use a context function (for Apollo Server), the State and Context types are positions 1 and 2 like so:

    type KoaState = { state: object };
    type KoaContext = { context: object };
    
    koaMiddleware<GraphQLContext, KoaState, KoaContext>(
      new ApolloServer<GraphQLContext>(),
      //...
      {
        context: async ({ ctx }) => ({ graphqlContext: {} }),
      },
    );

    If you don't use a context function, the State and Context types are positions 0 and 1 like so:

    type KoaState = { state: object };
    type KoaContext = { context: object };
    
    koaMiddleware<KoaState, KoaContext>(
      new ApolloServer<GraphQLContext>(),
      //...
    );

1.0.0

Major Changes

  • #117 4567c98 Thanks @trevor-scheer! - Drop support for Node.js 14, require v16+

    The only change required for users to take this update is to upgrade their Node.js version to v16+. No other breaking changes.

0.3.0

Minor Changes

  • #85 bb28b66 Thanks @laverdet! - Implement support for the @defer directive. In order to use this feature, you must be using an appropriate version of graphql. At the time of writing this, @defer is only available in v17 alpha versions of the graphql package, which is currently not officially supported. Due to peer dependencies, you must install graphql like so in order to force v17: npm i graphql@alpha --legacy-peer-deps

0.2.1

Patch Changes

0.2.0

Minor Changes

0.1.0

Minor Changes

  • #19 61106d1 Thanks @matthew-gordon! - Update Apollo Server dependencies, add explicit non-support for incremental delivery for now"

0.0.12

Patch Changes

0.0.11

Patch Changes

0.0.10

Patch Changes

0.0.9

Patch Changes

0.0.8

Patch Changes

0.0.7

Patch Changes

0.0.6

Patch Changes