Skip to content

Commit ee80d5a

Browse files
idalithbbenface
andauthored
Creating a Subgraph (#786)
* Update website/pages/en/developing/creating-a-subgraph.mdx Co-authored-by: Benoît Rouleau <[email protected]>
1 parent 58cb1a6 commit ee80d5a

File tree

1 file changed

+24
-18
lines changed

1 file changed

+24
-18
lines changed

website/pages/en/developing/creating-a-subgraph.mdx

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,7 @@ In this configuration:
314314
- `topic2` is configured to filter `Transfer` events where `0xAddressB` and `0xAddressC` is the receiver.
315315
- The subgraph will index transactions that occur in either direction between multiple addresses allowing for comprehensive monitoring of interactions involving all addresses.
316316

317-
## Declared eth_call
317+
### Declared eth_call
318318

319319
> **Requires**: [SpecVersion](#specversion-releases) >= `1.2.0`. Currently, `eth_calls` can only be declared for event handlers.
320320

@@ -326,13 +326,13 @@ This feature does the following:
326326
- Allows faster data fetching, resulting in quicker query responses and a better user experience.
327327
- Reduces wait times for applications that need to aggregate data from multiple Ethereum calls, making the data retrieval process more efficient.
328328

329-
### Key Concepts
329+
#### Key Concepts
330330

331331
- Declarative `eth_calls`: Ethereum calls that are defined to be executed in parallel rather than sequentially.
332332
- Parallel Execution: Instead of waiting for one call to finish before starting the next, multiple calls can be initiated simultaneously.
333333
- 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).
334334

335-
### Scenario without Declarative `eth_calls`
335+
#### Scenario without Declarative `eth_calls`
336336

337337
Imagine you have a subgraph that needs to make three Ethereum calls to fetch data about a user's transactions, balance, and token holdings.
338338

@@ -344,7 +344,7 @@ Traditionally, these calls might be made sequentially:
344344

345345
Total time taken = 3 + 2 + 4 = 9 seconds
346346

347-
### Scenario with Declarative `eth_calls`
347+
#### Scenario with Declarative `eth_calls`
348348

349349
With this feature, you can declare these calls to be executed in parallel:
350350

@@ -356,13 +356,13 @@ Since these calls are executed in parallel, the total time taken is equal to the
356356

357357
Total time taken = max (3, 2, 4) = 4 seconds
358358

359-
### How it Works
359+
#### How it Works
360360

361361
1. Declarative Definition: In the subgraph manifest, you declare the Ethereum calls in a way that indicates they can be executed in parallel.
362362
2. Parallel Execution Engine: The Graph Node's execution engine recognizes these declarations and runs the calls simultaneously.
363363
3. Result Aggregation: Once all calls are complete, the results are aggregated and used by the subgraph for further processing.
364364

365-
### Example Configuration in Subgraph Manifest
365+
#### Example Configuration in Subgraph Manifest
366366

367367
Declared `eth_calls` can access the `event.address` of the underlying event as well as all the `event.params`.
368368

@@ -410,22 +410,28 @@ calls:
410410
The ABI file(s) must match your contract(s). There are a few ways to obtain ABI files:
411411

412412
- 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.
414414
- 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.
415415

416-
## The GraphQL Schema
416+
### The GraphQL Schema
417417

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.
419419

420-
## Defining Entities
420+
### Defining Entities
421421

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.
423423

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.
425431

426-
### Good Example
432+
#### Good Example
427433

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.
429435

430436
```graphql
431437
type Gravatar @entity(immutable: true) {
@@ -437,9 +443,9 @@ type Gravatar @entity(immutable: true) {
437443
}
438444
```
439445

440-
### Bad Example
446+
#### Bad Example
441447

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.
443449

444450
```graphql
445451
type GravatarAccepted @entity {
@@ -457,7 +463,7 @@ type GravatarDeclined @entity {
457463
}
458464
```
459465

460-
### Optional and Required Fields
466+
#### Optional and Required Fields
461467

462468
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:
463469

@@ -473,7 +479,7 @@ For some entity types the `id` is constructed from the id's of two other entitie
473479

474480
#### GraphQL Supported Scalars
475481

476-
We support the following scalars in our GraphQL API:
482+
The following scalars are supported in the GraphQL API:
477483

478484
| Type | Description |
479485
| --- | --- |

0 commit comments

Comments
 (0)