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/tutorial/queries.md
+153-2Lines changed: 153 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -9,11 +9,162 @@ You have learned how to fetch data with Apollo Client. In this section, you'll d
9
9
10
10
Fetching data in a simple, predictable and optimistic way is one of the core features of Apollo Client. `react-apollo`, a view layer integration for Apollo Client, exports a `Query` React component that allows you pass a GraphQL query string wrapped with the `gql` tag to a `query` prop.
11
11
12
-
The `Query` component uses the render prop pattern
13
-
12
+
The `Query` component uses the render prop pattern to fetch and load data from queries into our React UI. The render prop pattern provides the ability to add a function as a child to our `Query` component that will notify React about what you want to render. The `Query` component exposes the `error`, `loading` and `data` state properties that you can use to determine the type of UI to render depending on the state of the query.
14
13
15
14
<h2id="fetch-data">Fetch data with Query</h2>
16
15
16
+
Let's create a query and fetch data with the `Query` component.
17
+
18
+
Navigate to `src/components` directory and create a `launch-list.js` file. Copy the code below and add to it.
**Note:** We used `react-emotion` for styling. Make sure the `react-emotion` package is installed.
96
+
97
+
To fetch data using the `Query` component, a query string needs to be passed to the `query` prop. The `LIST_LAUNCHES` query was passed to the `query` prop of the `Query` component to fetch a list of all the launches.
98
+
99
+
The `Query` component uses a render prop pattern, so we passed a function as a child to the component that contains what React will render to the screen. The `Query` component provides us with a `loading`, `error`, and `data` property that keeps the user informed about the status of the query operation on the screen. If it's in a loading state, the user sees **Loading...** on the screen. If there's an error, the user sees **ERROR** on the screen.
100
+
101
+
If the data was returned successfully, then the launches are retrieved from the `data` property and displayed on the screen via the `LaunchTile` component.
102
+
103
+
Now, there are a lot of launches. If all the launches are fetched at once and displayed, the result will be a long undesirable list. Therefore, let's build a pagination feature that accomodates the loading of a few items at once and display a `Load More` button for loading more items on the screen.
104
+
17
105
<h2id="pagination">Build a paginated list</h2>
18
106
107
+
There are different approaches to building a paginated list. You'll build a pagination feature using the `cursor-based` approach. The cursor keeps track of where in the data set the next items should be fetched from.
108
+
109
+
In the code below, we use a `fetchMore` query to continuously load new launches, which will be prepended to the list. The cursor to be used in the fetchMore query is provided in the initial server response, and is updated whenever more data is fetched.
110
+
111
+
Copy the code below and add to the `LaunchList` class.
The easiest way to go about pagination with Apollo is with the `fetchMore` function which is provided as a property by the `Query` component. By default, 20 launches are returned at once. Why 20? The paginate helper function in the `src/utils.js` file accepts a page size of 20 if no argument was passed to specify the number of launches to return.
163
+
164
+
The next step is to check if the `hasMore` property on the launches returned is true. If true, then present a `<LoadMoreButton>` for the user to click.
165
+
166
+
The `<LoadMoreButton>`'s `onClick` function invokes the `fetchMore` function. By default, `fetchMore` calls the original query but with a new set of input, which is the `data.launches.cursor` value passed to the `after` key as a new variable.
167
+
168
+
Once the new data is returned from the server, the `updateQuery` function is used to merge it with the existing data, which will cause a re-render of your UI component with an expanded list.
0 commit comments