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.
- Incrementally adoptable
- Simple to get started with
- Inspectable and understandable
- Built for interactive apps
- Community Driven
- Declarative: what data, don’t worry about how or when
- Colocations: write data dependencies right in the view
- Mutations: data consistency, optimistic updates, and error handling
Object Identification for Caching
__typename + id (client-side, automatic)
Apollo: built-in (one-line setup + config)
Optimistic UI Updates
Relay: cursor-based (server-side, requires GraphQL connections configuration)
Custom Network Interfaces
Performance (Caching and Query Optimization)
Apollo: Jest (assuming you’re using React)
Relay: React DevTools - (chrome extension)
Apollo: React, React Native, Angular 2, Redux, Meteor
Relay: React, React Native
Learning Curve (Familiarity)
Apollo: similar to react-redux
Relay: new material
- Simple GraphQL mutation w/ resolve
- Send literal mutation through Apollo client
- Update Apollo client store
- GraphQL mutation w/ mutationWithClientMutationID
- Match variable names between GraphQL and Relay
- Set up fat query & configs for Relay
Apollo: fantastic, with pictures!
Open Source Stats*
- Watchers: 87
- Stars: 815
- Forks: 61
- Pending PRs: 9
- Commits in last month: 176
- Watchers: 339
- Stars: 6,940
- Forks: 576
- Open Issues: 123
- Pending PRs: 10
- Commits in the last month: 10
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!