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: website/pages/en/querying/querying-from-an-application.mdx
+59-36Lines changed: 59 additions & 36 deletions
Original file line number
Diff line number
Diff line change
@@ -2,29 +2,30 @@
2
2
title: Querying from an Application
3
3
---
4
4
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.
6
6
7
-
**Subgraph Studio (testing endpoint)**
7
+
## Getting GraphQL Endpoint
8
8
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:
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.
22
24
23
-
Here are a couple of the more popular GraphQL clients in the ecosystem and how to use them:
25
+
## Using Popular GraphQL Clients
24
26
25
-
##GraphQL clients
27
+
### Graph Client
26
28
27
-
### Graph client
28
29
29
30
The Graph is providing its own GraphQL client, `graph-client` that supports unique features such as:
30
31
@@ -33,18 +34,24 @@ The Graph is providing its own GraphQL client, `graph-client` that supports uniq
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
37
40
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`:
39
42
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:
41
46
42
47
```sh
43
48
yarn add -D @graphprotocol/client-cli
44
49
# or, with NPM:
45
50
npm install --save-dev @graphprotocol/client-cli
46
51
```
47
52
53
+
#### Step 2
54
+
48
55
Define your query in a `.graphql` file (or inlined in your `.js` or `.ts` file):
49
56
50
57
```graphql
@@ -72,7 +79,9 @@ query ExampleQuery {
72
79
}
73
80
```
74
81
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:
76
85
77
86
```yaml
78
87
# .graphclientrc.yml
@@ -90,13 +99,17 @@ documents:
90
99
- ./src/example-query.graphql
91
100
```
92
101
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:
94
105
95
106
```sh
96
107
graphclient build
97
108
```
98
109
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:
100
113
101
114
```tsx
102
115
importReact, { useEffect } from'react'
@@ -134,33 +147,35 @@ function App() {
134
147
exportdefaultApp
135
148
```
136
149
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**.
138
151
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
140
153
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.
142
155
143
-
### Apollo client
156
+
Although it's the heaviest client, it has many features to build advanced UI on top of GraphQL:
144
157
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
146
163
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
148
165
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:
154
167
155
-
Let's look at how to fetch data from a subgraph with Apollo client in a web project.
168
+
#### Step 1
156
169
157
-
First, install`@apollo/client` and `graphql`:
170
+
Install`@apollo/client` and `graphql`:
158
171
159
172
```sh
160
173
npm install @apollo/client graphql
161
174
```
162
175
163
-
Then you can query the API with the following code:
To use variables, you can pass in a `variables` argument to the query:
197
214
198
215
```javascript
@@ -224,24 +241,30 @@ client
224
241
})
225
242
```
226
243
227
-
### URQL
244
+
### URQL Overview
228
245
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:
230
247
231
248
- Flexible cache system
232
249
- Extensible design (easing adding new capabilities on top of it)
233
250
- Lightweight bundle (~5x lighter than Apollo Client)
234
251
- Support for file uploads and offline mode
235
252
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:
237
256
238
-
First, install `urql` and `graphql`:
257
+
#### Step 1
258
+
259
+
Install `urql` and `graphql`:
239
260
240
261
```sh
241
262
npm install urql graphql
242
263
```
243
264
244
-
Then you can query the API with the following code:
0 commit comments