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-best-practices.mdx
+30-35Lines changed: 30 additions & 35 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -2,11 +2,9 @@
2
2
title: Querying Best Practices
3
3
---
4
4
5
-
The Graph provides a decentralized way to query data from blockchains.
5
+
The Graph provides a decentralized way to query data from blockchains via GraphQL APIs, making it easier to query data with the GraphQL language.
6
6
7
-
The Graph network's data is exposed through a GraphQL API, making it easier to query data with the GraphQL language.
8
-
9
-
This page will guide you through the essential GraphQL language rules and GraphQL queries best practices.
7
+
Learn the essential GraphQL language rules and GraphQL querying best practices.
10
8
11
9
---
12
10
@@ -71,7 +69,7 @@ GraphQL is a language and set of conventions that transport over HTTP.
71
69
72
70
It means that you can query a GraphQL API using standard `fetch` (natively or via `@whatwg-node/fetch` or `isomorphic-fetch`).
73
71
74
-
However, as stated in ["Querying from an Application"](/querying/querying-from-an-application), we recommend you to use our `graph-client`that supports unique features such as:
72
+
However, as stated in ["Querying from an Application"](/querying/querying-from-an-application), it's recommend to use `graph-client`which supports unique features such as:
75
73
76
74
- Cross-chain Subgraph Handling: Querying from multiple subgraphs in a single query
@@ -191,17 +187,16 @@ const result = await execute(query, {
191
187
})
192
188
```
193
189
194
-
Note: The opposite directive is `@skip(if: ...)`.
190
+
> Note: The opposite directive is `@skip(if: ...)`.
195
191
196
192
### Ask for what you want
197
193
198
194
GraphQL became famous for its "Ask for what you want" tagline.
199
195
200
196
For this reason, there is no way, in GraphQL, to get all available fields without having to list them individually.
201
197
202
-
When querying GraphQL APIs, always think of querying only the fields that will be actually used.
203
-
204
-
A common cause of over-fetching is collections of entities. By default, queries will fetch 100 entities in a collection, which is usually much more than what will actually be used, e.g., for display to the user. Queries should therefore almost always set first explicitly, and make sure they only fetch as many entities as they actually need. This applies not just to top-level collections in a query, but even more so to nested collections of entities.
198
+
- When querying GraphQL APIs, always think of querying only the fields that will be actually used.
199
+
- Make sure queries only fetch as many entities as you actually need. By default, queries will fetch 100 entities in a collection, which is usually much more than what will actually be used, e.g., for display to the user. This applies not just to top-level collections in a query, but even more so to nested collections of entities.
205
200
206
201
For example, in the following query:
207
202
@@ -337,8 +332,8 @@ query {
337
332
338
333
Such repeated fields (`id`, `active`, `status`) bring many issues:
339
334
340
-
-harder to read for more extensive queries
341
-
-when using tools that generate TypeScript types based on queries (_more on that in the last section_), `newDelegate` and `oldDelegate` will result in two distinct inline interfaces.
335
+
-More extensive queries become harder to read.
336
+
-When using tools that generate TypeScript types based on queries (_more on that in the last section_), `newDelegate` and `oldDelegate` will result in two distinct inline interfaces.
342
337
343
338
A refactored version of the query would be the following:
344
339
@@ -364,13 +359,13 @@ fragment DelegateItem on Transcoder {
364
359
}
365
360
```
366
361
367
-
Using GraphQL `fragment` will improve readability (especially at scale) but also will result in better TypeScript types generation.
362
+
Using GraphQL `fragment` will improve readability (especially at scale) and result in better TypeScript types generation.
368
363
369
364
When using the types generation tool, the above query will generate a proper `DelegateItemFragment` type (_see last "Tools" section_).
370
365
371
366
### GraphQL Fragment do's and don'ts
372
367
373
-
**Fragment base must be a type**
368
+
### Fragment base must be a type
374
369
375
370
A Fragment cannot be based on a non-applicable type, in short, **on type not having fields**:
376
371
@@ -382,7 +377,7 @@ fragment MyFragment on BigInt {
382
377
383
378
`BigInt` is a **scalar** (native "plain" type) that cannot be used as a fragment's base.
384
379
385
-
**How to spread a Fragment**
380
+
#### How to spread a Fragment
386
381
387
382
Fragments are defined on specific types and should be used accordingly in queries.
388
383
@@ -411,16 +406,16 @@ fragment VoteItem on Vote {
411
406
412
407
It is not possible to spread a fragment of type `Vote` here.
413
408
414
-
**Define Fragment as an atomic business unit of data**
409
+
#### Define Fragment as an atomic business unit of data
415
410
416
-
GraphQL Fragment must be defined based on their usage.
411
+
GraphQL `Fragment`s must be defined based on their usage.
417
412
418
413
For most use-case, defining one fragment per type (in the case of repeated fields usage or type generation) is sufficient.
419
414
420
-
Here is a rule of thumb for using Fragment:
415
+
Here is a rule of thumb for using fragments:
421
416
422
-
-when fields of the same type are repeated in a query, group them in a Fragment
423
-
-when similar but not the same fields are repeated, create multiple fragments, ex:
417
+
-When fields of the same type are repeated in a query, group them in a `Fragment`.
418
+
-When similar but different fields are repeated, create multiple fragments, for instance:
424
419
425
420
```graphql
426
421
# base fragment (mostly used in listing)
@@ -443,7 +438,7 @@ fragment VoteWithPoll on Vote {
443
438
444
439
---
445
440
446
-
## The essential tools
441
+
## The Essential Tools
447
442
448
443
### GraphQL web-based explorers
449
444
@@ -473,21 +468,21 @@ This will allow you to **catch errors without even testing queries** on the play
473
468
474
469
The [GraphQL VSCode extension](https://marketplace.visualstudio.com/items?itemName=GraphQL.vscode-graphql) is an excellent addition to your development workflow to get:
475
470
476
-
-syntax highlighting
477
-
-autocomplete suggestions
478
-
-validation against schema
479
-
-snippets
480
-
-go to definition for fragments and input types
471
+
-Syntax highlighting
472
+
-Autocomplete suggestions
473
+
-Validation against schema
474
+
-Snippets
475
+
-Go to definition for fragments and input types
481
476
482
477
If you are using `graphql-eslint`, the [ESLint VSCode extension](https://marketplace.visualstudio.com/items?itemName=dbaeumer.vscode-eslint) is a must-have to visualize errors and warnings inlined in your code correctly.
483
478
484
479
**WebStorm/Intellij and GraphQL**
485
480
486
481
The [JS GraphQL plugin](https://plugins.jetbrains.com/plugin/8097-graphql/) will significantly improve your experience while working with GraphQL by providing:
487
482
488
-
-syntax highlighting
489
-
-autocomplete suggestions
490
-
-validation against schema
491
-
-snippets
483
+
-Syntax highlighting
484
+
-Autocomplete suggestions
485
+
-Validation against schema
486
+
-Snippets
492
487
493
-
More information on this [WebStorm article](https://blog.jetbrains.com/webstorm/2019/04/featured-plugin-js-graphql/)that showcases all the plugin's main features.
488
+
For more information on this topic, check out the [WebStorm article](https://blog.jetbrains.com/webstorm/2019/04/featured-plugin-js-graphql/)which showcases all the plugin's main features.
0 commit comments