Announcing Apollo Server 2
Evans Hauser
As we watch teams adopt Apollo, one lesson we’ve learned is how important it is for product engineering teams that use GraphQL to be able to work on their schema as a natural part of their development process. So we think it’s vital that there be a full-featured GraphQL layer in JavaScript with all the latest GraphQL innovations and techniques, all wired up with the machinery and tooling that makes it a complete solution.
So today, after long and productive collaboration with hundreds of Apollo developers and teams in production, we’re excited to announce a new major release of the Apollo GraphQL Server that addresses the biggest needs of product engineering teams and sets them up for long-term success.
Based on our experience working with teams building scalable, performant GraphQL APIs, we focused on three areas for Apollo Server 2:
- Improving the developer experience: making it easy and straightforward for product engineers to stand up a GraphQL API, with minimal boilerplate and configuration.
- Baking in the best development patterns: including common patterns for error handling, testing and mocking, subscriptions, file uploads, as well as implementations of automatic persisted queries, CDN integration, and schema and query metrics that are critical in production.
- Layering GraphQL over existing APIs and services: defining Data sources, a new abstraction that manages connections to REST endpoints and implements standard patterns for cross-request partial query caching.
Apollo Server 2 is an easy transition for teams using Apollo Server 1.x or express-graphql
. After a complete beta and RC cycle, many companies are already running Apollo Server 2 in production. Follow the short migration guide to find out how.
Read on for a closer look at what’s inside.
Improving the developer experience
Apollo Server 2 contains everything you need to build a GraphQL layer, all wired up for you. To get started, install the apollo-server
package:
npm install apollo-server
Using Apollo Server 2 is as simple as defining your GraphQL schema and passing resolvers to the constructor. This structured approach lets you and your team focus on the core application and schema design without needing to worry about the mechanics of a GraphQL API.
const { ApolloServer, gql } = require('apollo-server');// Construct a schema, using GraphQL schema language const typeDefs = gql` type Query { hello: String } `;// Provide resolver functions for your schema fields const resolvers = { Query: { hello: () => 'Hello world!' }, };const server = new ApolloServer({ typeDefs, resolvers });server.listen().then(({ url }) => { console.log(`🚀 Server ready at ${url}`) });
To see this in action, try out a live example here.
The idea to bundle all the features together into one constructor echoes Prisma’s approach with graphql-yoga
. We really liked how it followed our goal of making GraphQL API development more approachable, so we decided that a similar API should be the entry point for Apollo Server 2. Thank you again to the Prisma team for the inspiration and for always pushing the GraphQL community forward.
Production best practices
In addition to revamping the getting started experience, Apollo Server now includes the most common and important patterns we’ve seen in use.
- Automatic persisted queries — Apollo Server supports automatic persisted queries, which lets clients send a calculated hash instead of a full query string over the network, falling back to the full query in cases where the server doesn’t recognize the hash. This technique reduces query latency and bandwidth requirements — especially for clients that have large or complex queries — and doesn’t require any build-time configuration on either client or server.
- CDN integration — Apollo Server implements Apollo Cache Control, a specification for computing cache control headers from field level schema directives. Paired with a CDN, this technique enables full query caching and can dramatically reduce TTI in many settings. For more on how CDN integration works, read our post about improving full stack performance.
- Integrated metrics reporting — Apollo Server includes a pure JavaScript implementation of our metrics reporting agent. We’ve eliminated the need for a separate in-band binary proxy and made it simpler than ever to enable Engine, our cloud service that helps teams manage schema changes, understand what parts of their schema are used by each client, and diagnose performance problems down to the field and resolver level. The new metrics collection system also works out of the box in modern serverless and edge environments, such AWS Lambda and Cloudflare Workers, that are an excellent match for GraphQL.
- Standard error handling—Building on GraphQL’s flexible approach to errors, Apollo Server 2 includes standard full-stack patterns for categorizing errors that, in conjunction with Apollo Client, makes it easy to handle re-authentication and build error-aware UI components and interactions. Read more here.
Data sources — Integrating existing backends
Data sources provide a structured approach for fetching the data for your schema. They facilitate the best practice of keeping resolvers simple and delegating to a data model, replacing most uses of Dataloader with a purpose built solution, including deduplication and error handling.
Today’s Apollo Server 2 release comes with a REST data source that enables an important pattern, partial query caching. A REST data source can cache long lived data in an in-memory cache, Memcached, or Redis and stitch short lived personal data for low latency full responses. We hope to continue to expand this pattern with more data sources for backends like GRPC, Thrift, and databases.
Read more about data sources in our introductory post.
Get started now
To get started with Apollo Server 2, follow the introductory guide. Upgrades from Apollo Server 1.x or express-graphql
are straightforward and documented in our migration guide. We’re here to help and excited to work with you in the future. Reach out to us on the Apollo Slack or GitHub. For contributors and beta users who helped make Apollo Server 2 possible, thank you!
I hope you’ll also join us at the 3rd annual GraphQL Summit on Nov 7–8 in San Francisco. With over 800 attendees expected, it’s the largest GraphQL developer event in the world. To share your GraphQL API experience and best practices, submit a proposal to speak. Early bird tickets are selling fast, so register today to reserve your spot and hear about what’s next for Apollo Server 2!
Special thank you to: Prosper Otemuyiwa, James Baxley, Matías Olivera, Clarence Ngoh, Jake Dawkins, Alessio Dionisi, Matías Olivera, Martijn Walraven, Yichang Liu, Jesse Rosenberger, C. T. Lin, Gauthier Rodaro, Radu Achim, Danilo Bürger, Vince Picone, Sasha Jolich, uosl, Hugh Willson, Adam Zionts, Nicola Molinari, Tejas Kumar, Ben Iofel, Fabien BERNARD, Daniel Ferreira, Amit Portnoy, Daniele Zurico, Dave Wasmer, Luke Barton, Yihong, David Glasser, Aditya Pratap Singh, Giorgio Grasso, Walter Barbagallo, Andy Brenneke, Vlad Shcherbin, Nicolás Fantone, Sashko Stubailo, Eve Porcello, Alex Banks, Justin Hall, Abhi Aiyer, Steve Rice, and Matt Debergalis.