In recent months we’ve been making the transition from using REST APIs to using GraphQL, which has enhanced our development workflow in a variety of ways. If you haven’t heard of GraphQL, it’s an open-source data query language that allows you to request multiple types of data, all using a single endpoint. In addition to transitioning to GraphQL, we also use Apollo to help interface with and build our GraphQL APIs.
Now that we’ve been using both Apollo and GraphQL for several months during the build process of some of our new products, we decided to host a meetup at our LA office to talk about our transition and what we’ve learned. Here are some of the highlights.
We began using GraphQL about 9 months ago. Starting roughly 4 months ago, all our new services are written with GraphQL, where possible. So far, we’ve found that GraphQL gives us a big productivity boost over our previous REST+Redux model:
Our stack on the server side consists of:
The most interesting thing here is Apollo Engine, Apollo’s transparent proxy that sits in front of your GraphQL server. It executes queries, sending deep APM metrics to engine.apollographql.com for service monitoring and traffic analysis. We’re optimistic that we’ll soon be able to deploy Apollo Engine as part of our production GraphQL infrastructure.
Apollo Client is a platform that integrates with almost all modern front-end frameworks, including React. It was designed specifically to be used with GraphQL, making it easy to fetch data from your API.
For most of our previous projects, we used Redux to manage our data store, which could get complicated really fast. It was really easy to lose track of where data was going and to which component—and having to create actions, reducers, constants, etc., for everything was a pain. It led to a large file tree, with Redux files containing hundreds of lines of code.
Thankfully, with Apollo Client, we’ve reduced that significantly. Instead of having to pass data to our components via Reducers and merging data into the state, in Apollo Client you simply write your query, hook it up to your component, and the data is merged into the Apollo Client store automatically. The same goes for mutations—just hook up to your component and use it! Simple.
Apollo Client is also extremely simple to set up, requiring only an ApolloProvider wrapper component around your existing application. This makes it easy to incrementally adopt Apollo Client, without having to completely overhaul your front-end.
Although we’ve transitioned a few of our REST APIs to GraphQL, there are some situations where we have to consume both a REST API and our GraphQL API. In these cases, we turn to a the Apollo-link-rest package, which allows you to wrap your GraphQL queries and mutations in Apollo Client with a @rest directive, and consume the response the same way you would your GraphQL API data.
Using this package to transition between REST and GraphQL is extremely helpful, but not necessarily a viable long-term solution. Apollo Client was built around the idea that you only need one API to get the data you need, maybe two. Using Apollo-link-rest for more than one REST API isn’t possible on a single Apollo Client, meaning multiple clients have to be instantiated. In that case, Apollo-link-state can also be used with state management tools like Redux to merge state existing outside of the Apollo store.
Both packages are excellent transition tools for consuming both GraphQL APIs and REST APIs in the same application.
We’ve been making excellent inroads with GraphQL and Apollo, but we’ve still got more to learn. During our meetup, we heard about some newer Apollo and GraphQL features that we’re interested in learning more about and potentially integrating into our stack.
This is a newer, built-in feature of Apollo that crawls your client source and maps generated IDs to your GraphQL queries and mutations. This is great because as an application scales, the text data alone for all queries and mutations can increase bandwidth usage. Using persisted queries, however, only references to those queries are actually sent from the client to the server.
This is also great for allow-listing specific queries, and preventing unintended queries from ever being executed.
Schema decorators allow you to add features to your API using reusable modules, without having to add more code to your resolvers or any custom logic. This can be great for things like adding metadata to your schema, or cache control. Schema decorators are essentially interfaces that return functions that apply to your schema, which makes them easy to implement in a variety of languages.
This feature is still in development, but it’s one we’re definitely keeping an eye on! You can read more about schema decorators here.
One of the most common things developers have said about Apollo and GraphQL is that they’ve made building applications and APIs way more efficient and fun—and we agree! Both have helped us prototype and build features at Replicated much faster, and we’re excited to see what new features GraphQL and Apollo introduce over time.
In the meantime, here are some lessons we’ve learned about our latest implementations:
As we continue to work with GraphQL and Apollo, we’ll continue to share more of our build process. If you have any tips you’d like to share, please reach out to us at (somewhere) and teach us something!