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.
COMPARISON
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.
To see the fancy table, click here.
Key Values
Apollo:
* Incrementally adoptable
* Simple to get started with
* Inspectable and understandable
* Built for interactive apps
* Community Driven
Relay:
* 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
Apollo: __typename + id (client-side, automatic)
Relay: graphql-relay globalId (server-side, requires GraphQL schema configuration)
Query Language
Apollo: graphql-tag
Relay: Relay.QL
Query Batching
Apollo: built-in (one-line setup + config)
Relay: react-relay-network-layer
Subscriptions
Fragments
Apollo: yes
React: yes
Optimistic UI Updates
Pagination
Apollo: Arbitrary pagination schema (server-side), fetchMore (client-side)
React: curser-based (server-side, requires GraphQL connections configuration)
Custom Network Interfaces
Server-Side Rendering
Apollo: yes
Relay: isomorphic-relay
Performance (Caching and Query Optimization)
Routing
Apollo: react-router
Relay: react-router-relay
Testing
Apollo: Jest (assuming you’re using React)
Relay: Jest
Developer Tools
Apollo: Redux DevTools (chrome extension)
Relay: React DevTools (chrome extension)
Compatibility
Apollo: React, React Native, Angular 2, Redux, Meteor
Relay: React, React Native
Learning Curve (Familiarity)
Apollo: similar to react-redux
Relay: new material
Mutation-Writing Complexity
Apollo:
* Simple GraphQL mutation w/resolve
* Send literal mutation through Apollo client
* Update Apollo client store
Relay:
* GraphQL mutation w/mutationWithClientMutationID
* Match variable names between GraphQL and Relay
* Set up fat query & configs for Relay
Documentation
Apollo: fantastic (with pictures!)
Relay: …spotty
Open Source Stats*
Apollo: https://github.com/apollostack/apollo-client
* Watchers: 87
* Stars: 815
* Forks: 61
* Pending PRs: 9
* Commits in last month: 176
Relay: https://github.com/facebook/relay
* Watchers: 339
* Stars: 6,940
* Forks: 576
* Open Issues: 123
* Pending PRs: 10
* Commits in the last month: 10
CODA
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.
Make your choice: Get started with Apollo or Get started with Relay
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
*as of time of writing (September 1, 2016)