Skip to content

Commit a165f56

Browse files
unicodeveloperpeggyrayzis
authored andcommitted
Add setting up client environment
1 parent 64ea157 commit a165f56

1 file changed

Lines changed: 86 additions & 1 deletion

File tree

docs/source/tutorial/client.md

Lines changed: 86 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,95 @@ title: "5. Connect your API to a client"
33
description: Start here for the Apollo fullstack tutorial
44
---
55

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+
68
<h2 id="dev-environment">Set up your development environment</h2>
79

10+
First, let's install two packages:
11+
12+
```bash
13+
npm install react-apollo graphql graphql-tag --save
14+
```
15+
16+
**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+
818
<h2 id="apollo-client-setup">Create an Apollo Client</h2>
919

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+
const client = new ApolloClient({
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+
1041
<h2 id="apollo-client-setup">Make your first query</h2>
1142

12-
<h2 id="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+
import gql from '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+
<h2 id="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.
77+
78+
Open `src/index.js` and the update the code:
79+
80+
_src/index.js_
81+
82+
```js
83+
import React from 'react';
84+
import ReactDOM from 'react-dom';
85+
import './index.css';
86+
import App from './App';
87+
88+
import { ApolloClient } from 'apollo-client';
89+
import { ApolloProvider } from 'react-apollo';
90+
91+
ReactDOM.render(
92+
<ApolloProvider client={client}>
93+
<App />
94+
</ApolloProvider>, document.getElementById('root'));
95+
```
96+
97+
**Note:** Delete all the service worker code that React provides out of the box. We don't need them for this app.

0 commit comments

Comments
 (0)