Koa GraphQL Middleware
raw JSON →koa-graphql is a middleware designed for Koa.js applications, enabling the quick and efficient setup of a GraphQL HTTP server. It serves as a direct port of the well-established `express-graphql` package, bringing its comprehensive features for handling GraphQL queries, mutations, and subscriptions to the Koa ecosystem. The current stable version, 0.12.0, was released in November 2021. While the project doesn't have a rapid release cadence, it provides a robust and stable foundation for integrating GraphQL into Koa-based APIs. Its key differentiators include seamless integration with Koa's middleware paradigm, out-of-the-box GraphiQL IDE support (including subscription capabilities), and extensive options for custom schema execution and error formatting. It supports both `graphql` v14 and v15 as peer dependencies, ensuring compatibility with a range of GraphQL projects and is maintained by the GraphQL community.
Common errors
error TypeError: (0 , koa_graphql_1.default) is not a function ↓
import { graphqlHTTP } from 'koa-graphql'; for ESM or const { graphqlHTTP } = require('koa-graphql'); for CommonJS. error Error: Cannot find module 'graphql' from '.../node_modules/koa-graphql' ↓
graphql package: npm install graphql (or yarn add graphql). Ensure the installed version is compatible with koa-graphql (e.g., ^14.7.0 || ^15.3.0). error TypeError: app.use is not a function or app.use is not a GeneratorFunction ↓
koa-convert and wrap the graphqlHTTP middleware: app.use(mount('/graphql', convert.back(graphqlHTTP(...)))); error GraphiQL: Subscription client not found or not initialized. Check console for error messages. ↓
subscriptions-transport-ws or graphql-ws), an http server listening, and a SubscriptionServer instance configured as shown in the package's documentation for subscription support. Warnings
breaking The `graphqlHTTP` function changed from a default export to a named export. Code using `import graphqlHTTP from 'koa-graphql'` or `const graphqlHTTP = require('koa-graphql')` will break. ↓
breaking Flowtype support was entirely removed, and the project converted all remaining files to TypeScript. This affects users relying on Flow for type checking. ↓
gotcha `koa-graphql` has `graphql` as a peer dependency. Your project must explicitly install a compatible version of `graphql`. ↓
gotcha For Koa 1 applications, `koa-graphql` middleware needs to be converted using `koa-convert` due to API changes between Koa 1 and Koa 2+. ↓
gotcha Enabling GraphQL subscriptions in GraphiQL requires additional setup, including creating an `http` server and a `SubscriptionServer` instance with `subscriptions-transport-ws` or `graphql-ws`. ↓
Install
npm install koa-graphql yarn add koa-graphql pnpm add koa-graphql Imports
- graphqlHTTP wrong
import graphqlHTTP from 'koa-graphql';correctimport { graphqlHTTP } from 'koa-graphql'; - graphqlHTTP wrong
const graphqlHTTP = require('koa-graphql');correctconst { graphqlHTTP } = require('koa-graphql'); - GraphQLSchema
import { GraphQLSchema } from 'graphql';
Quickstart
import Koa from 'koa';
import mount from 'koa-mount';
import { graphqlHTTP } from 'koa-graphql';
import { buildSchema } from 'graphql';
// Define your GraphQL Schema using GraphQL Schema Language
const schema = buildSchema(`
type Query {
hello: String
echo(message: String!): String
}
type Mutation {
add(a: Int!, b: Int!): Int
}
`);
// Define a root resolver to handle queries and mutations
const rootValue = {
hello: () => 'Hello from koa-graphql!',
echo: ({ message }: { message: string }) => message,
add: ({ a, b }: { a: number; b: number }) => a + b,
};
const app = new Koa();
// Mount the GraphQL middleware
app.use(
mount(
'/graphql',
graphqlHTTP({
schema: schema,
rootValue: rootValue, // Provide the root resolver
graphiql: true, // Enable the GraphiQL IDE for easy testing
}),
),
);
const PORT = process.env.PORT || 4000;
app.listen(PORT, () => {
console.log(`GraphQL server running on http://localhost:${PORT}/graphql`);
console.log(`Access GraphiQL IDE at http://localhost:${PORT}/graphql`);
});