Apollo Server
Apollo Server is a community-maintained open-source GraphQL server that works with many Node.js HTTP server frameworks or can run standalone. Version 3.13.0 is the final stable release of this package, which has since been superseded by the `@apollo/server` package (v4+). Active development and new features are now released under the `@apollo/server` package, which follows a rapid release cadence.
Common errors
-
TypeError: ApolloServer is not a constructor
cause Attempting to use `require('apollo-server')` to import a version or package (e.g., `@apollo/server` v4+) that is ESM-only, or a misconfigured `package.json`.fixEnsure you are importing the correct package for your module system. If using `apollo-server` v3, ensure you're using compatible CJS/ESM. If migrating to `@apollo/server` v4+, update your project to use ESM `import` statements. -
Error: Cannot find module 'graphql'
cause The `graphql` package is a peer dependency and must be explicitly installed alongside `apollo-server`.fixRun `npm install graphql` to add the necessary peer dependency. -
Syntax Error: Expected Name, found <EOF>
cause The `typeDefs` string passed to `ApolloServer` contains an incomplete or malformed GraphQL schema definition.fixCarefully review your `gql` template literal for any syntax errors in your schema definition (e.g., missing curly braces, incorrect type names, etc.).
Warnings
- breaking The `apollo-server` package (v3) has been superseded by the `@apollo/server` package (v4+). Future development and active maintenance are focused on the new package, which introduces breaking changes and a new API structure.
- gotcha The `graphql` package is a peer dependency. Ensure you have a compatible version installed, as `apollo-server` v3.13.0 requires `^15.3.0 || ^16.0.0`.
- gotcha When migrating to the successor `@apollo/server` (v4+), note that it is ESM-only. Code using CommonJS `require()` will need to be updated to `import` statements and your project configured for ESM.
Install
-
npm install apollo-server -
yarn add apollo-server -
pnpm add apollo-server
Imports
- ApolloServer
const { ApolloServer } = require('apollo-server');import { ApolloServer } from 'apollo-server'; - gql
const { gql } = require('apollo-server');import { gql } from 'apollo-server';
Quickstart
import { ApolloServer, gql } from 'apollo-server';
// The GraphQL schema
const typeDefs = gql`
type Query {
"A simple type for getting started!"
hello: String
}
`;
// A map of functions which return data for the schema.
const resolvers = {
Query: {
hello: () => 'world',
},
};
const server = new ApolloServer({
typeDefs,
resolvers,
});
server.listen().then(({ url }) => {
console.log(`🚀 Server ready at ${url}`);
});