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/developing/creating-a-subgraph.mdx
+24-18Lines changed: 24 additions & 18 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -314,7 +314,7 @@ In this configuration:
314
314
- `topic2`is configured to filter `Transfer` events where `0xAddressB` and `0xAddressC` is the receiver.
315
315
- The subgraph will index transactions that occur in either direction between multiple addresses allowing for comprehensive monitoring of interactions involving all addresses.
316
316
317
-
## Declared eth_call
317
+
### Declared eth_call
318
318
319
319
> **Requires**: [SpecVersion](#specversion-releases) >= `1.2.0`. Currently, `eth_calls` can only be declared for event handlers.
320
320
@@ -326,13 +326,13 @@ This feature does the following:
326
326
- Allows faster data fetching, resulting in quicker query responses and a better user experience.
327
327
- Reduces wait times for applications that need to aggregate data from multiple Ethereum calls, making the data retrieval process more efficient.
328
328
329
-
### Key Concepts
329
+
#### Key Concepts
330
330
331
331
- Declarative `eth_calls`: Ethereum calls that are defined to be executed in parallel rather than sequentially.
332
332
- Parallel Execution: Instead of waiting for one call to finish before starting the next, multiple calls can be initiated simultaneously.
333
333
- Time Efficiency: The total time taken for all the calls changes from the sum of the individual call times (sequential) to the time taken by the longest call (parallel).
334
334
335
-
### Scenario without Declarative `eth_calls`
335
+
#### Scenario without Declarative `eth_calls`
336
336
337
337
Imagine you have a subgraph that needs to make three Ethereum calls to fetch data about a user's transactions, balance, and token holdings.
338
338
@@ -344,7 +344,7 @@ Traditionally, these calls might be made sequentially:
344
344
345
345
Total time taken = 3 + 2 + 4 = 9 seconds
346
346
347
-
### Scenario with Declarative `eth_calls`
347
+
#### Scenario with Declarative `eth_calls`
348
348
349
349
With this feature, you can declare these calls to be executed in parallel:
350
350
@@ -356,13 +356,13 @@ Since these calls are executed in parallel, the total time taken is equal to the
356
356
357
357
Total time taken = max (3, 2, 4) = 4 seconds
358
358
359
-
### How it Works
359
+
#### How it Works
360
360
361
361
1. Declarative Definition: In the subgraph manifest, you declare the Ethereum calls in a way that indicates they can be executed in parallel.
362
362
2. Parallel Execution Engine: The Graph Node's execution engine recognizes these declarations and runs the calls simultaneously.
363
363
3. Result Aggregation: Once all calls are complete, the results are aggregated and used by the subgraph for further processing.
364
364
365
-
### Example Configuration in Subgraph Manifest
365
+
#### Example Configuration in Subgraph Manifest
366
366
367
367
Declared `eth_calls` can access the `event.address` of the underlying event as well as all the `event.params`.
368
368
@@ -410,22 +410,28 @@ calls:
410
410
The ABI file(s) must match your contract(s). There are a few ways to obtain ABI files:
411
411
412
412
- If you are building your own project, you will likely have access to your most current ABIs.
413
-
- If you are building a subgraph for a public project, you can download that project to your computer and get the ABI by using [`truffle compile`](https://truffleframework.com/docs/truffle/overview) or using solc to compile.
413
+
- If you are building a subgraph for a public project, you can download that project to your computer and get the ABI by using [`npx hardhat compile`](https://hardhat.org/hardhat-runner/docs/guides/compile-contracts#compiling-your-contracts) or using `solc` to compile.
414
414
- You can also find the ABI on [Etherscan](https://etherscan.io/), but this isn't always reliable, as the ABI that is uploaded there may be out of date. Make sure you have the right ABI, otherwise running your subgraph will fail.
415
415
416
-
## The GraphQL Schema
416
+
### The GraphQL Schema
417
417
418
-
The schema for your subgraph is in the file `schema.graphql`. GraphQL schemas are defined using the GraphQL interface definition language. If you've never written a GraphQL schema, it is recommended that you check out this primer on the GraphQL type system. Reference documentation for GraphQL schemas can be found in the [GraphQL API](/querying/graphql-api) section.
418
+
The schema for your subgraph is in the file `schema.graphql`. GraphQL schemas are defined using the GraphQL interface definition language. If you've never written a GraphQL schema, it is recommended that you check out this primer on the GraphQL type system. Reference documentation for GraphQL schemas can be found in the [GraphQL API](/querying/graphql-api/) section.
419
419
420
-
## Defining Entities
420
+
### Defining Entities
421
421
422
-
Before defining entities, it is important to take a step back and think about how your data is structured and linked. All queries will be made against the data model defined in the subgraph schema and the entities indexed by the subgraph. Because of this, it is good to define the subgraph schema in a way that matches the needs of your dapp. It may be useful to imagine entities as "objects containing data", rather than as events or functions.
422
+
Before defining entities, it is important to take a step back and think about how your data is structured and linked.
423
423
424
-
With The Graph, you simply define entity types in `schema.graphql`, and Graph Node will generate top level fields for querying single instances and collections of that entity type. Each type that should be an entity is required to be annotated with an `@entity` directive. By default, entities are mutable, meaning that mappings can load existing entities, modify them and store a new version of that entity. Mutability comes at a price, and for entity types for which it is known that they will never be modified, for example, because they simply contain data extracted verbatim from the chain, it is recommended to mark them as immutable with `@entity(immutable: true)`. Mappings can make changes to immutable entities as long as those changes happen in the same block in which the entity was created. Immutable entities are much faster to write and to query, and should therefore be used whenever possible.
424
+
- All queries will be made against the data model defined in the subgraph schema and the entities indexed by the subgraph. As a result, it is good to define the subgraph schema in a way that matches the needs of your dapp.
425
+
- It may be useful to imagine entities as "objects containing data", rather than as events or functions.
426
+
- You define entity types in `schema.graphql`, and Graph Node will generate top-level fields for querying single instances and collections of that entity type.
427
+
- Each type that should be an entity is required to be annotated with an `@entity` directive.
428
+
- By default, entities are mutable, meaning that mappings can load existing entities, modify them and store a new version of that entity.
429
+
- Mutability comes at a price, so for entity types that will never be modified, such as those containing data extracted verbatim from the chain, it is recommended to mark them as immutable with `@entity(immutable: true)`.
430
+
- If changes happen in the same block in which the entity was created, then mappings can make changes to immutable entities. Immutable entities are much faster to write and to query so they should be used whenever possible.
425
431
426
-
### Good Example
432
+
#### Good Example
427
433
428
-
The `Gravatar` entity below is structured around a Gravatar object and is a good example of how an entity could be defined.
434
+
The following `Gravatar` entity is structured around a Gravatar object and is a good example of how an entity could be defined.
429
435
430
436
```graphql
431
437
type Gravatar @entity(immutable: true) {
@@ -437,9 +443,9 @@ type Gravatar @entity(immutable: true) {
437
443
}
438
444
```
439
445
440
-
### Bad Example
446
+
#### Bad Example
441
447
442
-
The example `GravatarAccepted` and `GravatarDeclined` entities below are based around events. It is not recommended to map events or function calls to entities 1:1.
448
+
The following example `GravatarAccepted` and `GravatarDeclined` entities are based around events. It is not recommended to map events or function calls to entities 1:1.
443
449
444
450
```graphql
445
451
type GravatarAccepted @entity {
@@ -457,7 +463,7 @@ type GravatarDeclined @entity {
457
463
}
458
464
```
459
465
460
-
### Optional and Required Fields
466
+
#### Optional and Required Fields
461
467
462
468
Entity fields can be defined as required or optional. Required fields are indicated by the `!` in the schema. If a required field is not set in the mapping, you will receive this error when querying the field:
463
469
@@ -473,7 +479,7 @@ For some entity types the `id` is constructed from the id's of two other entitie
473
479
474
480
#### GraphQL Supported Scalars
475
481
476
-
We support the following scalars in our GraphQL API:
482
+
The following scalars are supported in the GraphQL API:
0 commit comments