Choosing a GraphQL Client: Apollo vs. Relay
If you just want to see the comparison table, keep scrolling down. You won’t hurt anyone’s feelings. Probably.
You’re likely here because you’ve heard about GraphQL, the groundbreaking new way to fetch data for your applications. Maybe you’ve figured out how to set up a basic GraphQL server. (If not, check out these great articles.) Now, you’ve got to pick a client that takes advantage of your shiny new GraphQL server. You’ve probably heard of Relay and tried a tutorial. Maybe you found it confusing or difficult and didn’t realize you have other options. Well, Apollo and Relay are currently* two of the leading open-source GraphQL clients for NodeJS apps.
If only there was a quick and easy side-by-side comparison so you could make an easily informed decision on which client is right for you. Well, congratulations, you found one!
Why choose a GraphQL client?
Of course, before making such an important decision, we should discuss why we even need a GraphQL-specific client. Why can’t you just stick with whatever you’ve comfortably been using? Redux, for one, happens to work fine with GraphQL. If you’d like to spend more time simply figuring out how GraphQL works within a more familiar context like Redux, I highly recommend James Childs-Maidment’s fantastic article on Getting started with Redux and GraphQL.
However, Redux isn’t designed to maximize the benefits of GraphQL. If your goal in learning GraphQL is to really take advantage of its amazing benefits, it makes sense to also invest in a client-side technology that aims to get the most out of your GraphQL server. To this end,
Apollo and Relay both implement query and data caching in some form to further optimize requests to the GraphQL server.
This means doing lots of pre/post-processing to make “smart” requests, which Redux isn’t built to do for you. Apollo and Relay also do lots of other nice things. Instead of reading an entire compare/contrast essay about said things, here’s a neat table below.
Apollo and Relay share many features, but they’re clearly not identical. So, here’s a breakdown of how they’re different. In order to keep this brief, I’ve included links to what I believe are good resources for more information.
|Object Identification for Caching||__typename + id |
(server-side, requires GraphQL schema configuration)
|Query Batching||built-in (one-line setup + config)||
|Optimistic UI Updates||yes||yes|
|Pagination||Arbitrary pagination schema (server-side), fetchMore (client-side)||cursor-based (server-side, requires GraphQL connections configuration)|
|Custom Network Interfaces||yes||yes|
|Performance (Caching and Query Optimization||yes||yes|
|Testing||Jest (assuming you're using React)||Jest|
|Developer Tools||Redux DevTools (chrome extension)||React DevTools (chrome extension)|
|Compatibility||React, React Native, Angular 2, Redux, Meteor||React, React Native|
|Learning Curve (Familiarity)||similar to ||new material|
|Documentation||fantastic (with pictures!)||...spotty|
|Open Source Stats (as of 9/1/2016)||https://github.com/apollostack/apollo-client||https://github.com/facebook/relay
Ultimately, it comes down to you and the specifics of your project. Apollo offers a friendly developer experience and works great for learning and adopting GraphQL into an existing project, especially if it doesn’t use React. Relay is being built with mobile performance and scalability–at the Facebook level–in mind.
As GraphQL continues to mature, both technologies have great communities dedicated to creating a better internet, so you can’t really go wrong with either one.
My team decided to go with Relay. Why? The Relay team at Facebook is hard at work on some major improvements, and the future of Relay looks pretty bright. Also, because Relay and GraphQL are both developed at Facebook, we expect any updates for GraphQL will be supported faster by Relay 2 than Apollo.
If you were hoping for more words to read, check out some of the many links throughout this post. They’re dedicated to more specific topics about GraphQL, Apollo, and Relay. Also, keep an eye out for a follow-up article exploring key changes between Relay and Relay 2!
Posted by Rachel Lee, Software Engineer
About the Author
Rachel studied Computer Science and Cognitive Science at UC Berkeley and is currently working for Codazen as a software engineer.
She holds festive apple pie baking parties and has a big cheeky dog that snores and likes to nap on people’s feet.