Skip to content

Commit 0912138

Browse files
unicodeveloperpeggyrayzis
authored andcommitted
Add codesandbox demo
1 parent 805a48a commit 0912138

File tree

1 file changed

+28
-10
lines changed

1 file changed

+28
-10
lines changed

docs/source/tutorial/client.md

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ const client = new ApolloClient({
3232
uri: "http://localhost:4000/graphql"
3333
});
3434
```
35-
**Note:** The `src` directory mentioned here is not the server directory. It's a new directory that houses all the client code.
35+
**Note:** The `src` directory mentioned here is not the server directory. It's a new directory that contains the client code.
3636

3737
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.
3838

@@ -42,36 +42,54 @@ Now, let's fetch data with Apollo Client.
4242

4343
Our client is ready to start fetching data. Let's send a query with vanilla JavaScript.
4444

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`.
45+
With a `client.query()` call, we can query our graph's API. Copy the code below and add it to `src/index.js`.
4646

4747
_src/index.js_
4848

4949
```js
50-
import gql from 'graphql-tag';
5150
...
51+
import { InMemoryCache } from "apollo-cache-inmemory";
52+
import { HttpLink } from "apollo-link-http";
53+
import gql from "graphql-tag";
54+
55+
const cache = new InMemoryCache();
56+
const client = new ApolloClient({
57+
cache,
58+
link: new HttpLink({
59+
uri: "https://fullstack-tutorial-server-xsftsvrjdj.now.sh/graphql"
60+
})
61+
});
62+
5263
client
5364
.query({
5465
query: gql`
5566
{
56-
launch(launchID: 56) {
67+
launch(id: 56) {
5768
year
69+
mission {
70+
name
71+
}
5872
}
5973
}
6074
`
6175
})
6276
.then(result => console.log(result));
63-
...
77+
6478
```
6579

66-
The result should be an object with a `data` property. The launch returned is attached to the `data` property.
80+
**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`.
6781

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.
82+
The result 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)
6983

70-
Let's connect our client to React.
84+
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.
85+
86+
Now, let's connect our client to React.
7187

7288
<h2 id="react-apollo">Connect your client to React</h2>
7389

74-
React is the choice of framework for our UI frontend in this tutorial. Install React using `create-react-app` via `npx`:
90+
React is the choice of framework for our UI frontend in this tutorial.
91+
92+
Install React using `create-react-app` via `npx`:
7593

7694
```bash
7795
npx create-react-app frontend
@@ -81,7 +99,7 @@ Now, let's connect Apollo Client to our React app.
8199

82100
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.
83101

84-
Open `src/index.js` and the update the code:
102+
Open `src/index.js` and the update the code to be:
85103

86104
_src/index.js_
87105

0 commit comments

Comments
 (0)