Why the 2.0
The 2.0 version of Apollo provides a more customizable experience with GraphQL. It prioritizes features like custom execution chains (using Apollo Link) and custom stores while providing powerful defaults. It is an overall minor change to the API so you shouldn’t have to change very much code in your current app at all!
Goals
The
2.*
version of Apollo Client builds on the original principles of the project. For reference, those goals are:
- Incrementally adoptable, so that you can drop it into an existing JavaScript app and start using GraphQL for just part of your UI.
- Universally compatible, so that Apollo works with any build setup, any GraphQL server, and any GraphQL schema.
- Simple to get started with, you can start loading data right away and learn about advanced features later.
- Inspectable and understandable, so that you can have great developer tools to understand exactly what is happening in your app.
- Built for interactive apps, so your users can make changes and see them reflected in the UI immediately.
- Small and flexible, so you don’t get stuff you don’t need
- Community driven, Apollo is driven by the community and serves a variety of use cases. Everything is planned and developed in the open.
Based on feedback from a wide variety of users, the 2.*
version doubles down on being incrementally adoptable and flexible by allowing much stronger extension points. Customization of the client (i.e. data store, execution chain, etc) is a primary feature in the revised API. This version also take steps to reduce the overall size of the default client by 200% and provide the foundations for Apollo powering more of the application experience from development to production with client side state management.
The goal of the 2.0
launch is not to provide all of the new features that have been asked to be built in. Instead, the 2.0 makes a few key changes to both management of the code base (lerna / small modules) and the changes necessary to support custom stores and links fully. Apollo Client 2.0 is the jumping off point for user-land driven innovation (custom stores, custom links) and internal refactor (moving query manager into links, breaking apart the store / links into packages, etc)
Installation
One of the largest changes with the new version is the breaking apart of the monolith
apollo-client
package into a few small, but isolated modules. This gives way more flexibility, but does require more packages to install. Long term we think this is the right way to go for applications of all sizes, but we have created an apollo-client-preset
package with defaults that match the 1.0 version.
To get started with the 2.0, you will change your imports from either react-apollo
, or just apollo-client
to use the new packages. A typical upgrade looks like this:
Before
After
By splitting these features apart, it makes it possible to import only what your application needs and it makes it possible for us to indvidually increment versions instead of where a breaking change in one, means a breaking change in all since the packages are re-exported;
The above example can be reduced using apollo-client-preset
like so:
|
|
Basic updates
We know how much hard work and care you have put into your application! The 2.0 upgrade is designed to be as minimal as possible and to not require any application (like UI integration) changes. It is instead focused on how to construct a client as a way to new features and improvements. A simple usage of Apollo Client upgrading to the 2.0 would look like this:
Before
After
Custom configuration
Since everything was baked into the 1.0, custom configuration of the parts, like the network interface or cache, all were done on the constructor. With the 2.0, this is broken up slightly, and uneccessary configurations were removed. The following code snippet shows every possible option with the previous version and how to use it with the 2.0:
Before
After
Note If you were using customResolvers
, the name of that has been changed to be cacheResolvers
to be more descriptive of what it does. customResolvers
will still be supported throughout the 2.0 though to be backwards compatible and ease the upgrade path
Cache extraction
If you have previously used
getInitialState
for SSR, that API has been moved to the cache itself instead of on the client. You can read more about the recipe for server side rendering here. The upgrade path looks like this:
Before
After
Network Middleware and Afterware
If you previously used
use
or useAfter
on the networkInterface from the 1.0 of Apollo Client, you will need to update to use Apollo Links as the new way to handle *wares
in the 2.0. Apollo Link provides a lot more power for *ware
features and more information is available here. A few examples of migrating custom *ware
methods to Apollo Links are shown below:
Middleware
Before
After
Afterware (error handling)
Before
After
Afterware (data manipulation)
Before
After
For more information on using Apollo Links, check out the link docs!;
Replacing Redux
The 2.0 moves away from using Redux as the caching layer in favor of Apollo maintaining its own store through the provided
cache
passed when creating a client. This allows the new version to be more flexible around how data is cached, and opens the storage of data to many new avenues and view integrations. If you were previously using the Redux integration to do something like logging, you can now use an Apollo Link to log whenever a request is sent to the server. For example:
|
|
Ultimately we think the move off Redux will open the door for more powerful cache implementations and further performance gains. If you were using the Redux integration for other uses, please reach out or open an issue so we can help find a solution with the 2.0!
If you were using Redux for other purposes in your app with an older version of Apollo, you may have been relying on the fact that the <ApolloProvider store=store>
component did double duty as a react-redux
<Provider>
. In react-apollo 2.0, <ApolloProvider>
no longer has <Provider>
semantics; you’ll have to use <Provider>
yourself.
Query Reducers
Query reducers have been finally removed in the 2.0, instead we recommend using the more flexible
update
API instead of reducer.