July 19, 2018

Announcing Apollo Server 2

Evans Hauser
Software Engineer
@evanshauser
AnnouncementBackend

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.

The GraphQL API at the center of your architecture

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:

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.

<strong>const { ApolloServer, gql } = require('apollo-server');</strong>// 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 <strong>ApolloServer</strong>({ typeDefs, resolvers });server.<strong>listen</strong>().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.
Apollo Server behind a CDN
  • 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.
Request Rate View in Apollo Engine
  • 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-graphqlare 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!

GraphQL Summit 2018 (Nov 7–8, San Francisco)

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 OtemuyiwaJames BaxleyMatías OliveraClarence NgohJake DawkinsAlessio DionisiMatías OliveraMartijn WalravenYichang LiuJesse RosenbergerC. T. LinGauthier RodaroRadu AchimDanilo BürgerVince PiconeSasha JolichuoslHugh WillsonAdam ZiontsNicola MolinariTejas KumarBen IofelFabien BERNARDDaniel FerreiraAmit PortnoyDaniele ZuricoDave WasmerLuke BartonYihongDavid GlasserAditya Pratap SinghGiorgio GrassoWalter BarbagalloAndy BrennekeVlad ShcherbinNicolás FantoneSashko StubailoEve PorcelloAlex BanksJustin HallAbhi AiyerSteve Rice, and Matt Debergalis.

Written by

Evans Hauser

Follow

Stay in our orbit!

Become an Apollo insider and get first access to new features, best practices, and community events. Oh, and no junk mail. Ever.

Make this article better!

Was this post helpful? Have suggestions? Consider so we can improve it for future readers ✨.

Similar posts

November 12, 2020

The Evolution of GraphQL at Scale

by Andy Roberts

Company