Skip to content

Commit 22aae62

Browse files
committed
Reworked setting up client page
1 parent 6a6b57e commit 22aae62

File tree

1 file changed

+45
-55
lines changed

1 file changed

+45
-55
lines changed

docs/source/tutorial/client.md

Lines changed: 45 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,52 @@
11
---
22
title: "5. Connect your API to a client"
3-
description: Connect your graph API to a React frontend
3+
description: Hook up your graph to Apollo Client
44
---
55

6-
The next step after building your graph API is to fetch the data from the API and present on a client. In this section, you'll learn how to build a React UI that consumes our graph API's data with Apollo.
6+
The next half of this tutorial exclusively focuses on connecting a graph API to a frontend with Apollo Client. **Apollo Client** is a complete data management solution for any client. It's view-layer agnostic, which means it can integrate with React, Vue, Angular, or even vanilla JS. Thanks to its intelligent cache, Apollo Client offers a single source of truth for all of the local and remote data in your application.
7+
8+
While Apollo Client works with any view layer, it's most commonly used with React. In this section, you'll learn how to connect the graph API you just built in the previous half of this tutorial to a React app. Even if you're more comfortable with Vue or Angular, you should still be able to follow many of the examples since the concepts are the same. Along the way, you'll also learn how to build essential features like authentication and pagination, as well as tips for optimizing your workflow.
79

810
<h2 id="dev-environment">Set up your development environment</h2>
911

10-
First, let's install two packages:
12+
For this half of the tutorial, we will be working in the `client/` folder of the project. You should have the project already from the server portioned, but if you don't, make sure to clone [the tutorial](https://github.com/apollographql/fullstack-tutorial/). From the root of the project, run:
1113

1214
```bash
13-
npm install react-apollo graphql graphql-tag --save
15+
cd client && npm install
1416
```
1517

16-
**react-apollo** is a view layer React integration for Apollo Client.
18+
Now, our dependencies are installed. Here are the packages we will be using to build out our frontend:
19+
20+
- `apollo-client@next`: A complete data management solution with an intelligent cache. In this tutorial, we will be using the Apollo Client 3.0 preview since it includes local state management capabilities and sets your cache up for you.
21+
- `react-apollo`: The view layer integration for React that exports components such as `Query` and `Mutation`
22+
- `graphql-tag`: The tag function `gql` that we use to wrap our query strings in order to parse them into an AST
23+
24+
<h3 id="vscode">Configure Apollo VSCode</h3>
25+
26+
While Apollo VSCode is not required to successfully complete the tutorial, setting it up unlocks a lot of helpful features such as autocomplete for operations, jump to fragment definitions, and more.
27+
28+
First, make a copy of the `.env.example` file located in `client/` and call it `.env`. Add your Engine API key that you already created in step #4 to the file:
29+
30+
```
31+
ENGINE_API_KEY=service:your-key-here
32+
```
1733

18-
**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.
34+
Our key is now stored under the environment variable `ENGINE_API_KEY`. Apollo VSCode uses this API key to pull down your schema from the registry.
35+
36+
Next, create an Apollo config file called `apollo.config.js`. This config file is how you configure both the Apollo VSCode extension and CLI. Paste the snippet below into the file:
37+
38+
```js
39+
```
40+
41+
Great, we're all set up! Let's dive into building our first client.
1942

2043
<h2 id="apollo-client-setup">Create an Apollo Client</h2>
2144

22-
Now that we have installed the necessary packages, let's create an Apollo Client.
45+
Now that we have installed the necessary packages, let's create an `ApolloClient` instance.
2346

24-
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.
47+
Navigate to `src/index.js` so we can create our client. The `uri` that we pass in is the graph endpoint from the service you deployed in step 4.
2548

26-
Navigate to `src/index.js` so we can create our client. Copy the code below into the file.
49+
If you didn't complete the server portion, you can use the `uri` from the code below. Otherwise, use your own deployment's URL, which may be different than the one below. Navigate to `src/index.js` and copy the code below:
2750

2851
_src/index.js_
2952

@@ -34,40 +57,26 @@ const client = new ApolloClient({
3457
uri: "http://localhost:4000/graphql"
3558
});
3659
```
37-
**Note:** The `src` directory mentioned here is not the server directory. It's a new directory that contains the client code.
38-
39-
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.
4060

41-
Now, let's fetch data with Apollo Client.
61+
In just a couple lines of code, our client is ready to fetch data! Let's try making a query in the next section.
4262

4363
<h2 id="apollo-client-setup">Make your first query</h2>
4464

45-
Our client is ready to start fetching data. Let's send a query with vanilla JavaScript.
65+
Before we show you how to use the React integration for Apollo, let's send a query with vanilla JavaScript.
4666

4767
With a `client.query()` call, we can query our graph's API. Copy the code below and add it to `src/index.js`.
4868

4969
_src/index.js_
5070

51-
```js
52-
...
53-
import { InMemoryCache } from "apollo-cache-inmemory";
54-
import { HttpLink } from "apollo-link-http";
71+
```js line=1
5572
import gql from "graphql-tag";
5673

57-
const cache = new InMemoryCache();
58-
const client = new ApolloClient({
59-
cache,
60-
link: new HttpLink({
61-
uri: "https://fullstack-tutorial-server-xsftsvrjdj.now.sh/graphql"
62-
})
63-
});
64-
6574
client
6675
.query({
6776
query: gql`
68-
{
77+
query GetLaunch {
6978
launch(id: 56) {
70-
year
79+
id
7180
mission {
7281
name
7382
}
@@ -76,44 +85,25 @@ client
7685
`
7786
})
7887
.then(result => console.log(result));
79-
8088
```
8189

82-
**Note:** The `apollo-cache-inmemory` and `apollo-link-http` packages are vital to developing with Apollo Client so make sure they are installed. Run `npm install apollo-cache-inmemory apollo-link-http --save`. The `apollo-link-http` package handles network requests for fetching remote data, while the `apollo-cache-inmemory` package handes caching.
83-
84-
The `gql` tag is a JavaScript template literal tag that enables syntax highlighting for our schema, and also parses the query into an abstract sytax tree. The tree is sent to the client via the `client.query()` call.
85-
86-
The result of the query should be an object with a `data` property. The launch returned is attached to the `data` property. You can check out the [codesandbox demo of the code above.](https://codesandbox.io/s/8xmn5j6n88)
90+
Open up your console and you should see an object with a `data` property containing the result of our query. You'll also see some other properties, like `loading` and `networkStatus`. This is because Apollo Client tracks the loading state of your query for you.
8791

88-
Apollo Client is designed to enable fetching of graph data by any JavaScript frontend. No frameworks needed. However, there are view layer integrations for different frameworks that makes it easier to bind queries to UI.
92+
Apollo Client is designed to fetch graph data from any JavaScript frontend. No frameworks needed. However, there are view layer integrations for different frameworks that makes it easier to bind queries to UI.
8993

90-
Now, let's connect our client to React.
94+
Go ahead and delete the `client.query()` call you just made. Now, we'll connect our client to React.
9195

9296
<h2 id="react-apollo">Connect your client to React</h2>
9397

94-
React is the choice of framework for our UI frontend in this tutorial.
98+
Connecting Apollo Client to our React app with `react-apollo` allows us to easily bind GraphQL operations to our UI.
9599

96-
Install React using `create-react-app` via `npx`:
100+
To connect Apollo Client to React, we will wrap our app in the `ApolloProvider` component exported from the `react-apollo` package and pass our client to the `client` prop. 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.
97101

98-
```bash
99-
npx create-react-app frontend
100-
```
101-
102-
Now, let's connect Apollo Client to our React app.
103-
104-
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.
105-
106-
Open `src/index.js` and the update the code to be:
102+
Open `src/index.js` and add the following lines of code:
107103

108104
_src/index.js_
109105

110-
```js
111-
import React from 'react';
112-
import ReactDOM from 'react-dom';
113-
import './index.css';
114-
import App from './App';
115-
116-
import { ApolloClient } from 'apollo-client';
106+
```js lines=1,4,6
117107
import { ApolloProvider } from 'react-apollo';
118108

119109
ReactDOM.render(
@@ -122,4 +112,4 @@ ReactDOM.render(
122112
</ApolloProvider>, document.getElementById('root'));
123113
```
124114

125-
**Note:** Delete all the service worker code that React provides out of the box. We don't need them for this app.
115+
Now, we're ready to start building our first `Query` components in the next section.

0 commit comments

Comments
 (0)