You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/source/guides/state-mgmt.md
+97-4Lines changed: 97 additions & 4 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -3,15 +3,108 @@ title: State management
3
3
description: Managing your local and remote state in a GraphQL world
4
4
---
5
5
6
-
Thousands of developers have told us that Apollo Client excels at managing remote data, which equates to roughly 80% of their data needs. But what about local data (like global flags and device API results) that make up the other 20% of the pie? This is where `apollo-link-state` comes in, our solution for local state management that allows you to use your Apollo cache as the single source of truth for data in your application. Managing all your data with Apollo Client allows you to take advantage of GraphQL as a unified interface to all of your data.
6
+
Not only is state management one of the most important aspects of building the front-end for your application, it's also one of the most challenging. With a REST and Redux workflow, you're writing action creators, reducers, and selectors for each network request. For a production-level app, you're also juggling several middleware packages for features like optimistic updates and manually normalizing your data.
7
+
8
+
With a GraphQL and Apollo workflow, you just write queries and let Apollo Client take care of the rest. Apollo Client normalizes and caches your data out of the box with zero configuration. It also enables you to execute complicated features such as optimistic UI, polling, and pagination with only a few lines of code.
9
+
10
+
If we're using Apollo Client to manage our remote data, then what do we do with local data such as boolean flags and device API information that we'd like to access globally? This is where [`apollo-link-state`](/docs/react/essentials/local-state.html) comes in, our solution for local state management that allows you to use your Apollo cache as the single source of truth for data in your application. We recommend managing all of your local and remote data with Apollo Client so GraphQL becomes a unified interface to all of your application's data.
11
+
12
+
The following sections outline some tips to help you make the most of your transition to managing all of your state with Apollo Client.
7
13
8
14
<h2id="colocate">Colocate queries with components</h2>
9
15
10
-
<h2id="transformation">Move data transformation to the backend</h2>
16
+
When you first start building `Query` components for your GraphQL data, it can be tempting to dump all of your queries in one file similar to how developers using Redux put all of their reducers in a single file. Instead, we recommend that you colocate your GraphQL queries with the `Query` components that are using them. One of the greatest strengths of GraphQL is its declarative approach to data fetching, which you lose when you have to switch back to another file in order to determine what the shape of your data prop looks like:
In this example, we place our `GET_DOG_PHOTO` query next to our `DogPhoto` component and wrap it with the `gql` function. Now in the render prop function for `Query`, we know exactly what properties live on `data` and can use them to render our UI.
43
+
44
+
<h2id="data-transformation">Move data transformation to the backend</h2>
45
+
46
+
Your GraphQL schema should always reflect how you're consuming the data on the front-end. This is why we recommend that [product teams own the design](../fundamentals/tips.html#schema) of their GraphQL schema. Shifting to this mentality is a bit of a departure from REST, where front-end developers consume APIs dictated by the backend team and often have to filter and sort the data into the shape their UI components expect.
47
+
48
+
If you find yourself sorting or filtering the data you receive back from your GraphQL API, it's probably a sign that you need to move that logic to your resolvers instead. Moving filtering and sorting logic to the backend ensures that you can share it across platforms easily instead of duplicating these efforts for every client.
49
+
50
+
**Instead of this:**
51
+
```jsx
52
+
constGET_MOVIES=gql`
53
+
{
54
+
movies {
55
+
id
56
+
title
57
+
popularity
58
+
score
59
+
}
60
+
}
61
+
`;
62
+
63
+
constPopularMovies= () => (
64
+
<Query query={GET_MOVIES}>
65
+
{({ loading, error, data }) => {
66
+
if (loading) returnnull;
67
+
if (error) return`Error!: ${error}`;
68
+
69
+
constpopularMovies=data.movies.sort((a, b) => {
70
+
returnb.popularity-a.popularity;
71
+
});
72
+
73
+
return<Movies movies={popularMovies} />
74
+
}}
75
+
</Query>
76
+
);
77
+
```
78
+
79
+
**Do this:**
80
+
81
+
```jsx
82
+
constGET_MOVIES=gql`
83
+
{
84
+
movies(sort: POPULARITY) {
85
+
id
86
+
title
87
+
popularity
88
+
score
89
+
}
90
+
}
91
+
`;
92
+
93
+
constPopularMovies= () => (
94
+
<Query query={GET_MOVIES}>
95
+
{({ loading, error, data }) => {
96
+
if (loading) returnnull;
97
+
if (error) return`Error!: ${error}`;
98
+
99
+
return<Movies movies={data.movies} />
100
+
}}
101
+
</Query>
102
+
);
103
+
```
11
104
12
-
<h2id="combine">Combine local and remote data</h2>
105
+
<h2id="combine-data">Combine local and remote data</h2>
13
106
14
-
With `apollo-link-state`, you can add client-side only fields to your remote data seamlessly and query them from your components. In this example, we’re querying the client-only field isLiked alongside our server data. Your components are made up of local and remote data, now your queries can be too!
107
+
With `apollo-link-state`, you can add virtual fields to your remote data seamlessly and query them from your components by specifying a `@client` directive. In this example, we’re querying the client-only field isLiked alongside our server data. Your components are made up of local and remote data, now your queries can be too! This is one of the main advantages for using Apollo Client to manage all of your application's data.
0 commit comments