This project is my solution for:
- a migration from Apollo Client 2 to Apollo Client 3 for the CRWN Clothing app; a fictional online clothing store.
- To see the repo we're migrating from visit: CRWN Clothing - React & Apollo Client 2
- For the main CRWN Clothing project that uses React & Redux-Sagas visit: CRWN Clothing - React & Redux
- GraphQL Server for this application: crwn-clothing-prisma
The main Apollo Client 3 changes of concern for this app are summarised in the table below.
Apollo Client 2 | Apollo Client 3 | |
---|---|---|
Initialising Local State Values | client.writeData() method |
Choice between:
|
Mutating Local State |
Local Resolvers
|
|
Executing Queries |
Query component
|
useQuery() Hook
|
Executing Mutations |
Mutation component
|
useMutation() Hook
Alternatively, we can use:
|
- The CRWN Clothing - React & Apollo Client 2 project used apollo-cache-persist to persist the application's state
- apollo3-cache-persist was released for data persistence with Apollo Client 3 cache implementations
The collections data (i.e. categories for clothing items) is queried from the GraphQL Server and cached locally in Apollo Client's cache. Caching this data ensures that load times are reduced when users refresh or revisit the app; as data is pulled from the cache instead of executing queries for the same data again.
With the migration to Apollo Client 3, the CRWN Clothing app uses Reactive Variables to handle local state-management. These variables are not cached by apollo and as a result any cart data would disappear on refresh/re-renders. To persist the data, we have to leverage Local Storage.
In this repo at filepath /src/utils/local-storage.utils.js, you can find a few helper functions that I created in order to persist the values of the necessary Reactive Variables.