Skip to content

Commit 627e08a

Browse files
Querying from app (#807)
* updating * additional edits * updates * edits --------- Co-authored-by: Michael Macaulay <[email protected]>
1 parent a77c914 commit 627e08a

File tree

1 file changed

+59
-36
lines changed

1 file changed

+59
-36
lines changed

website/pages/en/querying/querying-from-an-application.mdx

Lines changed: 59 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,30 @@
22
title: Querying from an Application
33
---
44

5-
Once a subgraph is deployed to Subgraph Studio or to Graph Explorer, you will be given the endpoint for your GraphQL API that should look something like this:
5+
Learn how to query The Graph from your application.
66

7-
**Subgraph Studio (testing endpoint)**
7+
## Getting GraphQL Endpoint
88

9-
```sh
10-
Queries (HTTP)
9+
Once a subgraph is deployed to [Subgraph Studio](https://thegraph.com/studio/) or [Graph Explorer](https://thegraph.com/explorer), you will be given the endpoint for your GraphQL API that should look something like this:
10+
11+
### Subgraph Studio
12+
13+
```
1114
https://api.studio.thegraph.com/query/<ID>/<SUBGRAPH_NAME>/<VERSION>
1215
```
1316

14-
**Graph Explorer**
17+
### Graph Explorer
1518

16-
```sh
17-
Queries (HTTP)
19+
```
1820
https://gateway.thegraph.com/api/<API_KEY>/subgraphs/id/<SUBGRAPH_ID>
1921
```
2022

21-
Using the GraphQL endpoint, you can use various GraphQL Client libraries to query the subgraph and populate your app with the data indexed by the subgraph.
23+
With your GraphQL endpoint, you can use various GraphQL Client libraries to query the subgraph and populate your app with data indexed by the subgraph.
2224

23-
Here are a couple of the more popular GraphQL clients in the ecosystem and how to use them:
25+
## Using Popular GraphQL Clients
2426

25-
## GraphQL clients
27+
### Graph Client
2628

27-
### Graph client
2829

2930
The Graph is providing its own GraphQL client, `graph-client` that supports unique features such as:
3031

@@ -33,18 +34,24 @@ The Graph is providing its own GraphQL client, `graph-client` that supports uniq
3334
- [Automatic Pagination](https://github.com/graphprotocol/graph-client/blob/main/packages/auto-pagination/README.md)
3435
- Fully typed result
3536

36-
Also integrated with popular GraphQL clients such as Apollo and URQL and compatible with all environments (React, Angular, Node.js, React Native), using `graph-client` will give you the best experience for interacting with The Graph.
37+
> Note: `graph-client` is integrated with other popular GraphQL clients such as Apollo and URQL, which are compatible with environments such as React, Angular, Node.js, and React Native. As a result, using `graph-client` will provide you with an enhanced experience for working with The Graph.
38+
39+
### Fetch Data with Graph Client
3740

38-
Let's look at how to fetch data from a subgraph with `graphql-client`.
41+
Let's look at how to fetch data from a subgraph with `graph-client`:
3942

40-
To get started, make sure to install The Graph Client CLI in your project:
43+
#### Step 1
44+
45+
Install The Graph Client CLI in your project:
4146

4247
```sh
4348
yarn add -D @graphprotocol/client-cli
4449
# or, with NPM:
4550
npm install --save-dev @graphprotocol/client-cli
4651
```
4752

53+
#### Step 2
54+
4855
Define your query in a `.graphql` file (or inlined in your `.js` or `.ts` file):
4956

5057
```graphql
@@ -72,7 +79,9 @@ query ExampleQuery {
7279
}
7380
```
7481

75-
Then, create a configuration file (called `.graphclientrc.yml`) and point to your GraphQL endpoints provided by The Graph, for example:
82+
#### Step 3
83+
84+
Create a configuration file (called `.graphclientrc.yml`) and point to your GraphQL endpoints provided by The Graph, for example:
7685

7786
```yaml
7887
# .graphclientrc.yml
@@ -90,13 +99,17 @@ documents:
9099
- ./src/example-query.graphql
91100
```
92101
93-
Running the following The Graph Client CLI command will generate typed and ready to use JavaScript code:
102+
#### Step 4
103+
104+
Run the following The Graph Client CLI command to generate typed and ready to use JavaScript code:
94105
95106
```sh
96107
graphclient build
97108
```
98109

99-
Finally, update your `.ts` file to use the generated typed GraphQL documents:
110+
#### Step 5
111+
112+
Update your `.ts` file to use the generated typed GraphQL documents:
100113

101114
```tsx
102115
import React, { useEffect } from 'react'
@@ -134,33 +147,35 @@ function App() {
134147
export default App
135148
```
136149

137-
**⚠️ Important notice**
150+
> **Important Note:** `graph-client` is perfectly integrated with other GraphQL clients such as Apollo client, URQL, or React Query; you can [find examples in the official repository](https://github.com/graphprotocol/graph-client/tree/main/examples). However, if you choose to go with another client, keep in mind that **you won't be able to use Cross-chain Subgraph Handling or Automatic Pagination, which are core features for querying The Graph**.
138151
139-
`graph-client` is perfectly integrated with other GraphQL clients such as Apollo client, URQL, or React Query; you will [find examples in the official repository](https://github.com/graphprotocol/graph-client/tree/main/examples).
152+
### Apollo Client
140153

141-
However, if you choose to go with another client, keep in mind that **you won't be able to get to use Cross-chain Subgraph Handling or Automatic Pagination, which are core features for querying The Graph**.
154+
[Apollo client](https://www.apollographql.com/docs/) is a common GraphQL client on front-end ecosystems. It's available for React, Angular, Vue, Ember, iOS, and Android.
142155

143-
### Apollo client
156+
Although it's the heaviest client, it has many features to build advanced UI on top of GraphQL:
144157

145-
[Apollo client](https://www.apollographql.com/docs/) is the ubiquitous GraphQL client on the front-end ecosystem.
158+
- Advanced error handling
159+
- Pagination
160+
- Data prefetching
161+
- Optimistic UI
162+
- Local state management
146163

147-
Available for React, Angular, Vue, Ember, iOS, and Android, Apollo Client, although the heaviest client, brings many features to build advanced UI on top of GraphQL:
164+
### Fetch Data with Apollo Client
148165

149-
- advanced error handling
150-
- pagination
151-
- data prefetching
152-
- optimistic UI
153-
- local state management
166+
Let's look at how to fetch data from a subgraph with Apollo client:
154167

155-
Let's look at how to fetch data from a subgraph with Apollo client in a web project.
168+
#### Step 1
156169

157-
First, install `@apollo/client` and `graphql`:
170+
Install `@apollo/client` and `graphql`:
158171

159172
```sh
160173
npm install @apollo/client graphql
161174
```
162175

163-
Then you can query the API with the following code:
176+
#### Step 2
177+
178+
Query the API with the following code:
164179

165180
```javascript
166181
import { ApolloClient, InMemoryCache, gql } from '@apollo/client'
@@ -193,6 +208,8 @@ client
193208
})
194209
```
195210

211+
#### Step 3
212+
196213
To use variables, you can pass in a `variables` argument to the query:
197214

198215
```javascript
@@ -224,24 +241,30 @@ client
224241
})
225242
```
226243

227-
### URQL
244+
### URQL Overview
228245

229-
Another option is [URQL](https://formidable.com/open-source/urql/) which is available within Node.js, React/Preact, Vue, and Svelte environments, with more advanced features:
246+
[URQL](https://formidable.com/open-source/urql/) is available within Node.js, React/Preact, Vue, and Svelte environments, with some more advanced features:
230247

231248
- Flexible cache system
232249
- Extensible design (easing adding new capabilities on top of it)
233250
- Lightweight bundle (~5x lighter than Apollo Client)
234251
- Support for file uploads and offline mode
235252

236-
Let's look at how to fetch data from a subgraph with URQL in a web project.
253+
### Fetch data with URQL
254+
255+
Let's look at how to fetch data from a subgraph with URQL:
237256

238-
First, install `urql` and `graphql`:
257+
#### Step 1
258+
259+
Install `urql` and `graphql`:
239260

240261
```sh
241262
npm install urql graphql
242263
```
243264

244-
Then you can query the API with the following code:
265+
#### Step 2
266+
267+
Query the API with the following code:
245268

246269
```javascript
247270
import { createClient } from 'urql'

0 commit comments

Comments
 (0)