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/client.md
+86-1Lines changed: 86 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -3,10 +3,95 @@ title: "5. Connect your API to a client"
3
3
description: Start here for the Apollo fullstack tutorial
4
4
---
5
5
6
+
The next step after building your graph API is to present and manage the data on a client. In this section, you'll learn how to build a UI that consumes our graph API's data with Apollo.
7
+
6
8
<h2id="dev-environment">Set up your development environment</h2>
**react-apollo** is a view layer React integration for Apollo Client. **Apollo Client** is a JavaScript production-ready client that helps you quickly build a UI that fetches your graph's data and manages state efficiently. It supports esssential features like caching, pagination, and code splitting.
17
+
8
18
<h2id="apollo-client-setup">Create an Apollo Client</h2>
9
19
20
+
Now that we have installed the necessary packages, let's create an Apollo Client.
21
+
22
+
Apollo Client needs to connect to the endpoint of our graph API. If no URL is passed to Apollo Client, it falls back to the `/graphql` endpoint on the same host your app is served from.
23
+
24
+
Navigate to `src/index.js` so we can create our client. Copy the code below into the file.
25
+
26
+
_src/index.js_
27
+
28
+
```js
29
+
import { ApolloClient } from"apollo-client";
30
+
31
+
constclient=newApolloClient({
32
+
uri:"http://localhost:4000/graphql"
33
+
});
34
+
```
35
+
**Note:** The `src` directory mentioned here is not the server directory. It's a new directory that houses all the client code.
36
+
37
+
To connect our graph API to a client, we need to import the `ApolloClient` class from `apollo-client` and pass our API URL to the client via the `uri` property of the client config object.
38
+
39
+
Now, let's fetch data with Apollo Client.
40
+
10
41
<h2id="apollo-client-setup">Make your first query</h2>
11
42
12
-
<h2id="react-apollo">Connect your client to React</h2>
43
+
Our client is ready to start fetching data. Let's send a query with vanilla JavaScript.
44
+
45
+
With a `client.query()` function call, we can query our graph's API. Copy the code below and add it to `src/index.js`.
46
+
47
+
_src/index.js_
48
+
49
+
```js
50
+
importgqlfrom'graphql-tag';
51
+
...
52
+
client
53
+
.query({
54
+
query:gql`
55
+
{
56
+
launch(launchID: 56) {
57
+
year
58
+
}
59
+
}
60
+
`
61
+
})
62
+
.then(result=>console.log(result));
63
+
...
64
+
```
65
+
66
+
The result should be an object with a `data` property. The launch returned is attached to the `data` property.
67
+
68
+
Apollo Client is designed to enable fetching of graph data by anyone in the JavaScript ecosystem. No frameworks needed. However, there are view layer integrations for different frameworks that makes it easier to bind queries to UI.
69
+
70
+
Let's connect our client to React.
71
+
72
+
<h2id="react-apollo">Connect your client to React</h2>
73
+
74
+
React is the choice of framework for our UI frontend in this tutorial.
75
+
76
+
To connect Apollo Client to React, you will need to invoke the `ApolloProvider` component exported from the `react-apollo` package. The `ApolloProvider` component is similar to React’s context provider. It wraps your React app and places the client on the context, which allows you to access it from anywhere in your component tree.
0 commit comments