Skip to content

Commit

Permalink
[apollographql#11305]update get-started doc
Browse files Browse the repository at this point in the history
  • Loading branch information
gento-ogane committed Nov 2, 2023
1 parent fe12ef4 commit 04e89ca
Showing 1 changed file with 13 additions and 13 deletions.
26 changes: 13 additions & 13 deletions docs/source/get-started.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ Hello! 👋 This short tutorial gets you up and running with Apollo Client.

To start this tutorial, do one of the following:

- Create a new React project locally with [Create React App](https://create-react-app.dev/), or
- Create a new React project locally with [Vite](https://vitejs.dev/), or
- Create a new React sandbox on [CodeSandbox](https://codesandbox.io/).

## Step 2: Install dependencies
Expand All @@ -34,15 +34,15 @@ Our example application will use the [FlyBy GraphQL API](https://flyby-router-de

With our dependencies set up, we can now initialize an `ApolloClient` instance.

In `index.js`, let's first import the symbols we need from `@apollo/client`:
In `main.jsx`, let's first import the symbols we need from `@apollo/client`:

```js title="index.js"
```jsx title="main.jsx"
import { ApolloClient, InMemoryCache, ApolloProvider, gql } from '@apollo/client';
```

Next we'll initialize `ApolloClient`, passing its constructor a configuration object with the `uri` and `cache` fields:

```js title="index.js"
```jsx title="main.jsx"
const client = new ApolloClient({
uri: 'https://flyby-router-demo.herokuapp.com/',
cache: new InMemoryCache(),
Expand All @@ -54,9 +54,9 @@ const client = new ApolloClient({

That's it! Our `client` is ready to start fetching data. Now before we start using Apollo Client with React, let's first try sending a query with plain JavaScript.

In the same `index.js` file, call `client.query()` with the query string (wrapped in the `gql` template literal) shown below:
In the same `main.jsx` file, call `client.query()` with the query string (wrapped in the `gql` template literal) shown below:

```js title="index.js"
```jsx title="main.jsx"
// const client = ...

client
Expand Down Expand Up @@ -85,9 +85,9 @@ Let's look at how that works!

You connect Apollo Client to React with the `ApolloProvider` component. Similar to React's [`Context.Provider`](https://react.dev/reference/react/useContext), `ApolloProvider` wraps your React app and places Apollo Client on the context, enabling you to access it from anywhere in your component tree.

In `index.js`, let's wrap our React app with an `ApolloProvider`. We suggest putting the `ApolloProvider` somewhere high in your app, above any component that might need to access GraphQL data.
In `main.jsx`, let's wrap our React app with an `ApolloProvider`. We suggest putting the `ApolloProvider` somewhere high in your app, above any component that might need to access GraphQL data.

```jsx title="index.js" {15-17}
```jsx title="main.jsx" {15-17}
import React from 'react';
import * as ReactDOM from 'react-dom/client';
import { ApolloClient, InMemoryCache, ApolloProvider } from '@apollo/client';
Expand All @@ -112,9 +112,9 @@ root.render(

After your `ApolloProvider` is hooked up, you can start requesting data with `useQuery`. The `useQuery` hook is a [React hook](https://react.dev/reference/react) that shares GraphQL data with your UI.

Switching over to our `App.js` file, we'll start by replacing our existing file contents with the code snippet below:
Switching over to our `App.jsx` file, we'll start by replacing our existing file contents with the code snippet below:

```js title="App.js"
```jsx title="App.jsx"
// Import everything needed to use the `useQuery` hook
import { useQuery, gql } from '@apollo/client';

Expand All @@ -129,7 +129,7 @@ export default function App() {

We can define the query we want to execute by wrapping it in the `gql` template literal:

```js title="App.js"
```jsx title="App.jsx"
const GET_LOCATIONS = gql`
query GetLocations {
locations {
Expand All @@ -144,7 +144,7 @@ const GET_LOCATIONS = gql`

Next, let's define a component named `DisplayLocations` that executes our `GET_LOCATIONS` query with the `useQuery` hook:

```js title="App.js" {2}
```jsx title="App.jsx" {2}
function DisplayLocations() {
const { loading, error, data } = useQuery(GET_LOCATIONS);

Expand All @@ -171,7 +171,7 @@ Whenever this component renders, the `useQuery` hook automatically executes our

Finally, we'll add `DisplayLocations` to our existing component tree:

```jsx title="App.js" {6}
```jsxx title="App.jsx" {6}
export default function App() {
return (
<div>
Expand Down

0 comments on commit 04e89ca

Please sign in to comment.