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
> Important: Failing to follow these rules will result in an error from The Graph API.
54
54
55
55
1. Each `queryName` must only be used once per operation.
56
-
2. Each `field` must be used only once in a selection (you cannot query `id` twice under `token`)
57
-
3. Complex types require selection of sub-fields.
58
-
- For example, some `fields' or queries (like `tokens`) return complex types which will require a selection of sub-field. Not providing a selection when expected or providing one when not expected will raise an error, such as `id`. To know a field type, please refer to [Graph Explorer](/subgraphs/explorer/).
56
+
2. Each `field` must be used only once in a selection (you cannot query `id` twice under `token`).
57
+
3. Complex types require a selection of sub-fields.
58
+
- For example, some `fields' or queries (like `tokens`) return complex types which will require a selection of sub-fields. Not providing a selection when expected or providing one when not expected will raise an error, such as `id`. To know a field type, please refer to [Graph Explorer](/subgraphs/explorer/).
59
59
4. Any variable assigned to an argument must match its type.
60
60
5. Variables must be uniquely defined and used.
61
61
@@ -104,7 +104,7 @@ For more alternatives, see ["Querying from an Application"](/subgraphs/querying/
104
104
105
105
### 1. Always Write Static Queries
106
106
107
-
A common bad practice is to dynamically build a query strings as follows:
107
+
A common bad practice is to dynamically build a query string as follows:
108
108
109
109
```tsx
110
110
const id =params.id
@@ -122,10 +122,10 @@ query GetToken {
122
122
123
123
While the example above produces a valid GraphQL query, it comes with several issues:
124
124
125
-
- The full query is harder to understand
126
-
- Developers are responsible for safely sanitizing the string interpolation
127
-
- Not sending the values of the variables as part of the request can block server-side caching
128
-
- It prevents tools from statically analyzing the query (ex: Linter, or type generations tools)
125
+
- The full query is harder to understand.
126
+
- Developers are responsible for safely sanitizing the string interpolation.
127
+
- Not sending the values of the variables as part of the request can block server-side caching.
128
+
- It prevents tools from statically analyzing the query (e.g.linters or type generation tools).
129
129
130
130
Instead, it's recommended to **always write queries as static strings**.
131
131
@@ -153,10 +153,10 @@ const result = await execute(query, {
153
153
154
154
Static strings have several **key advantages**:
155
155
156
-
- Queries are easier to read, manage, and debug
157
-
- Variable sanitization is handled by the GraphQL server The GraphQL
158
-
- Variables can be cached at server-level
159
-
- Queries can be statically analyzed by tools (see [GraphQL Essential Tools](/subgraphs/querying/best-practices/#graphql-essential-tools-guides))
156
+
- Queries are easier to read, manage, and debug.
157
+
- Variable sanitization is handled by the GraphQL server The GraphQL.
158
+
- Variables can be cached at the serverlevel.
159
+
- Queries can be statically analyzed by tools (see [GraphQL Essential Tools](/subgraphs/querying/best-practices/#graphql-essential-tools-guides)).
160
160
161
161
### 2. Include Fields Conditionally in Static Queries
162
162
@@ -190,7 +190,7 @@ const result = await execute(query, {
190
190
191
191
### 3. Ask Only For What You Want
192
192
193
-
GraphQL is known for it's "Ask for what you want” tagline, which is why it requires explicitly listing each field you want. There's no built-in way to fetch all available fields automatically.
193
+
GraphQL is known for its "Ask for what you want” tagline, which is why it requires explicitly listing each field you want. There's no built-in way to fetch all available fields automatically.
194
194
195
195
- When querying GraphQL APIs, always think of querying only the fields that will actually be used.
196
196
- 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.
Sending multiple queries in the same GraphQL request **improves the overall performance** by reducing the time spent on the network (saves you a round trip to the API) and will provide a **more concise implementation**.
304
+
Sending multiple queries in the same GraphQL request **improves the overall performance** by reducing the time spent on the network (saves you a round trip to the API) and provides a **more concise implementation**.
305
305
306
306
### 6. Leverage GraphQL Fragments
307
307
@@ -364,8 +364,8 @@ When using the types generation tool, the above query will generate a proper `De
364
364
365
365
### Do's and Don'ts for Fragments
366
366
367
-
1. Fragments cannot be based on a non-applicable types (on type not having fields)
368
-
2.`BigInt` cannot be used as a fragment's base because it's a **scalar** (native "plain" type)
367
+
1. Fragments cannot be based on a non-applicable types (types without fields).
368
+
2.`BigInt` cannot be used as a fragment's base because it's a **scalar** (native "plain" type).
369
369
370
370
Example:
371
371
@@ -406,9 +406,9 @@ fragment VoteItem on Vote {
406
406
407
407
---
408
408
409
-
### How-to Define `Fragment` as an Atomic Business Unit of Data
409
+
### Howto Define `Fragment` as an Atomic Business Unit of Data
410
410
411
-
> For most use-case, defining one fragment per type (in the case of repeated fields usage or type generation) is enough.
411
+
> For most use-cases, defining one fragment per type (in the case of repeated fields usage or type generation) is enough.
412
412
413
413
Here is a rule of thumb for using fragments:
414
414
@@ -476,19 +476,19 @@ GraphQL plugins streamline your workflow by offering real-time feedback while yo
476
476
477
477
1. VS Code
478
478
479
-
Install the [GraphQL VSCode extension](https://marketplace.visualstudio.com/items?itemName=GraphQL.vscode-graphql) to unlock:
479
+
Install the [GraphQL VS Code extension](https://marketplace.visualstudio.com/items?itemName=GraphQL.vscode-graphql) to unlock:
480
480
481
481
- Syntax highlighting
482
482
- Autocomplete suggestions
483
483
- Validation against schema
484
484
- Snippets
485
485
- Go to definition for fragments and input types
486
486
487
-
If you are using `graphql-eslint`, use the [ESLint VSCode extension](https://marketplace.visualstudio.com/items?itemName=dbaeumer.vscode-eslint) to visualize errors and warnings inlined in your code correctly.
487
+
If you are using `graphql-eslint`, use the [ESLint VS Code extension](https://marketplace.visualstudio.com/items?itemName=dbaeumer.vscode-eslint) to visualize errors and warnings inlined in your code correctly.
488
488
489
-
2. WebStorm/Intellij and GraphQL\*\*
489
+
2. WebStorm/Intellij and GraphQL
490
490
491
-
Install the [JS GraphQL plugin](https://plugins.jetbrains.com/plugin/8097-graphql/). It significantly improves your experience while working with GraphQL by providing:
491
+
Install the [JS GraphQL plugin](https://plugins.jetbrains.com/plugin/8097-graphql/). It significantly improves the experience of working with GraphQL by providing:
0 commit comments