Registry / api / apollo-server-nextjs

apollo-server-nextjs

JSON →
library3.10.3jsnpmunverified

A lightweight Apollo Server integration for Next.js API routes. Current version 3.10.3 supports Node >=12, GraphQL 15/16, and Next.js 12+. Provides a simple `ApolloServerNext` class that plugs into Next.js pages/api handlers, handling CORS, body parsing, and error formatting. Unlike `apollo-server-micro` or standalone solutions, it's optimized for Next.js's serverless environment. Ships TypeScript types.

npm install apollo-server-nextjs
INSTALL
IMPORT
SIG · APOLLO-SERVER-NEXT
A
apollo-server-nextjs
apijavascriptv3.10.3
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

ApolloServerNext
import { ApolloServerNext } from 'apollo-server-nextjs'
import ApolloServerNext from 'apollo-server-nextjs'
Named export only; not default exported.
startServerAndCreateNextHandler
import { startServerAndCreateNextHandler } from 'apollo-server-nextjs'
const { startServerAndCreateNextHandler } = require('apollo-server-nextjs')
ESM-only; CommonJS require will fail.
ApolloServerNextConfig
import type { ApolloServerNextConfig } from 'apollo-server-nextjs'
Type import for config object if using TypeScript.

Creates a minimal GraphQL API endpoint in a Next.js API route using ApolloServerNext and the handler helper.

import { ApolloServerNext, startServerAndCreateNextHandler } from 'apollo-server-nextjs'; import { NextApiRequest, NextApiResponse } from 'next'; const typeDefs = `#graphql type Query { hello: String } `; const resolvers = { Query: { hello: () => 'Hello world!', }, }; const server = new ApolloServerNext({ typeDefs, resolvers, }); export default startServerAndCreateNextHandler(server); // Optional config: export const config = { api: { bodyParser: false } };
Debug
Known issues
breakingVersion 3.x drops support for Next.js <12 and GraphQL <15.
fix
Upgrade to Next.js >=12 and GraphQL >=15.
affects: <3.0.0
gotchaThe handler assumes body parsing is disabled; you must set `bodyParser: false` in Next.js config if using built-in body parsing.
fix
Add `export const config = { api: { bodyParser: false } };` in the API route file.
affects: >=3.0.0
gotchaCORS headers are not set by default; you must configure them via `context` or middleware.
fix
Use a custom `context` function to set CORS headers or wrap handler with a CORS middleware.
affects: all
deprecatedThe old `ApolloServerNextHandler` pattern from v2 is removed.
fix
Use `startServerAndCreateNextHandler(server)` instead.
affects: <3.0.0
gotchaError formatting default includes stack traces in production if not overridden.
fix
Set `formatError` in ApolloServerNext config to avoid exposing stack traces.
affects: >=3.0.0
Errors
Common errors & fixes
TypeError: Cannot destructure property 'req' of 'ctx' as it is undefined.
The `context` function receives `{ req, res }` but destructuring incorrectly.
fix
Use `context: async ({ req, res }) => ({ req, res })`.
ApolloServerNext is not a constructor
Using CommonJS `require` instead of ESM import.
fix
Use `import { ApolloServerNext } from 'apollo-server-nextjs'` and configure your project for ESM.
Response is not a function
Forgetting to return the handler or misusing `res.json()`.
fix
Export `startServerAndCreateNextHandler(server)` directly; do not wrap in another function.
GraphQLError: Query root type must be provided.
Missing `typeDefs` or empty schema.
fix
Ensure `typeDefs` includes a `type Query { ... }` definition.
Upgrade
Version history
3.10.3latest on npm
Audit
Dependencies
graphqlrequiredpeer dependency for GraphQL execution
nextrequiredpeer dependency for Next.js API routes
Agent activity
52 hits · last 30 days
node
42
OpenAI (training)
1
Resources
apollo-server-nextjs — npm install apollo-server-nextjs · libregistry