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/data/queries.mdx
+1-1
Original file line number
Diff line number
Diff line change
@@ -24,7 +24,7 @@ The `useQuery` [React hook](https://react.dev/reference/react) is the primary AP
24
24
25
25
Let's look at an example. First, we'll create a GraphQL query named `GET_DOGS`. Remember to wrap query strings in the `gql` function to parse them into query documents:
Next we'll initialize `ApolloClient`, passing its constructor a configuration object with the `uri` and `cache` fields:
44
44
45
-
```js title="index.js"
45
+
```jsx title="main.jsx"
46
46
constclient=newApolloClient({
47
47
uri:'https://flyby-router-demo.herokuapp.com/',
48
48
cache:newInMemoryCache(),
@@ -54,9 +54,9 @@ const client = new ApolloClient({
54
54
55
55
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.
56
56
57
-
In the same `index.js` file, call `client.query()` with the query string (wrapped in the `gql` template literal) shown below:
57
+
In the same `main.jsx` file, call `client.query()` with the query string (wrapped in the `gql` template literal) shown below:
58
58
59
-
```js title="index.js"
59
+
```jsx title="main.jsx"
60
60
// const client = ...
61
61
62
62
client
@@ -85,9 +85,9 @@ Let's look at how that works!
85
85
86
86
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.
87
87
88
-
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.
88
+
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.
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.
114
114
115
-
Switching over to our `App.js` file, we'll start by replacing our existing file contents with the code snippet below:
115
+
Switching over to our `App.jsx` file, we'll start by replacing our existing file contents with the code snippet below:
116
116
117
-
```js title="App.js"
117
+
```jsx title="App.jsx"
118
118
// Import everything needed to use the `useQuery` hook
119
119
import { useQuery, gql } from'@apollo/client';
120
120
@@ -129,7 +129,7 @@ export default function App() {
129
129
130
130
We can define the query we want to execute by wrapping it in the `gql` template literal:
131
131
132
-
```js title="App.js"
132
+
```jsx title="App.jsx"
133
133
constGET_LOCATIONS=gql`
134
134
queryGetLocations {
135
135
locations {
@@ -144,7 +144,7 @@ const GET_LOCATIONS = gql`
144
144
145
145
Next, let's define a component named `DisplayLocations` that executes our `GET_LOCATIONS` query with the `useQuery` hook:
146
146
147
-
```js title="App.js" {2}
147
+
```jsx title="App.jsx" {2}
148
148
functionDisplayLocations() {
149
149
const { loading, error, data } =useQuery(GET_LOCATIONS);
150
150
@@ -171,7 +171,7 @@ Whenever this component renders, the `useQuery` hook automatically executes our
171
171
172
172
Finally, we'll add `DisplayLocations` to our existing component tree:
0 commit comments