Apollo Server

Spec-compliant and production ready JavaScript GraphQL server that lets you...

README

@apollo/server


This @apollo/serverpackage is new with Apollo Server 4. Previous major versions of Apollo Server used a set of package names starting with apollo-server, such as apollo-server, apollo-server-express, apollo-server-core, etc.


A TypeScript/JavaScript GraphQL server


Apollo Server is an open-source , spec-compliant GraphQL serverthat's compatible with any GraphQL client, including Apollo Client . It's the best way to build a production-ready, self-documenting GraphQL API that can use data from any source.

You can use Apollo Server as:

A stand-alone GraphQL server
The GraphQL server for a subgraph in a federated supergraph
The gateway for a federated supergraph

Apollo Server provides a simple API for integrating with any Node.js web framework or serverless environment. The @apollo/serverpackage itself ships with a minimally-configurable, standalone web server which handles CORS and body parsing out of the box. Integrations with other environments are community-maintained.

Apollo Server provides:

Straightforward setup, so your client developers can start fetching data quickly
Incremental adoption, enabling you to add features as they're needed
Universal compatibilitywith any data source, any build tool, and any GraphQL client
Production readiness, enabling you to confidently run your graph in production

Documentation


Full documentation for Apollo Server is available on our documentation site . This README shows the basics of getting a server running (both standalone and with Express), but most features are only documented on our docs site.

Getting started: standalone server


You can also check out the getting started guide in the Apollo Server docs for more details, including examples in both TypeScript and JavaScript.


Apollo Server's standalone server lets you get a GraphQL server up and running quickly without needing to set up an HTTP server yourself. It allows all the same configuration of GraphQL logic as the Express integration, but does not provide the ability to make fine-grained tweaks to the HTTP-specific behavior of your server.

First, install Apollo Server and the JavaScript implementation of the core GraphQL algorithms:

  1. ``` null
  2. npm install @apollo/server graphql

  3. ```

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

  1. ``` js
  2. import { ApolloServer } from '@apollo/server';
  3. import { startStandaloneServer } from '@apollo/server/standalone';

  4. // The GraphQL schema
  5. const typeDefs = `#graphql
  6.   type Query {
  7.     hello: String
  8.   }
  9. `;

  10. // A map of functions which return data for the schema.
  11. const resolvers = {
  12.   Query: {
  13.     hello: () => 'world',
  14.   },
  15. };

  16. const server = new ApolloServer({
  17.   typeDefs,
  18.   resolvers,
  19. });

  20. const { url } = await startStandaloneServer(server);
  21. console.log(` Server ready at ${url}`);
  22. ```

Now run your server with:

  1. ``` null
  2. node server.mjs

  3. ```

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 operationquery { hello }!

Getting started: Express middleware


Apollo Server's built-in Express middleware lets you run your GraphQL server as part of an app built with Express , the most popular web framework for Node.

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

  1. ``` null
  2. npm install @apollo/server graphql express cors body-parser

  3. ```

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

  1. ``` js
  2. import { ApolloServer } from '@apollo/server';
  3. import { expressMiddleware } from '@apollo/server/express4';
  4. import { ApolloServerPluginDrainHttpServer } from '@apollo/server/plugin/drainHttpServer'
  5. import express from 'express';
  6. import http from 'http';
  7. import cors from 'cors';
  8. import bodyParser from 'body-parser';

  9. // The GraphQL schema
  10. const typeDefs = `#graphql
  11.   type Query {
  12.     hello: String
  13.   }
  14. `;

  15. // A map of functions which return data for the schema.
  16. const resolvers = {
  17.   Query: {
  18.     hello: () => 'world',
  19.   },
  20. };

  21. const app = express();
  22. const httpServer = http.createServer(app);

  23. // Set up Apollo Server
  24. const server = new ApolloServer({
  25.   typeDefs,
  26.   resolvers,
  27.   plugins: [ApolloServerPluginDrainHttpServer({ httpServer })],
  28. });
  29. await server.start();

  30. app.use(
  31.   cors(),
  32.   bodyParser.json(),
  33.   expressMiddleware(server),
  34. );

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

Now run your server with:

  1. ``` null
  2. node server.mjs

  3. ```

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 operationquery { hello }!