How it works
When a subgraph handler does something likeentity.save()
, an update is written to an intermediate db which powers the subgraph API. This update is interpreted by a real-time watcher and set to your webhook handler, with an UPDATE
, INSERT
, or DELETE
operation.
Interpreting entity updates
If you’re tracking an immutable entity (as in one that does not get updated), then this section is not applicable. Subgraphs store all versions of entities, each with ablock_range
column which shows it’s valid for each block range. This allows you to distinguish between an entity changing vs a change being rolled-back due to blockchain reorgs.
Entity updates and removals
Updates (when an existing entity’s.save()
is called) in a subgraph entity system is denoted as a new version row being created, with a corresponding update on the last version’s row.
There is an entity with the id: 1
created at block 1. A webhook will fire:
Tracking the latest state
If your goal is to track the latest state of an entity for the most recent block, when you see anyCREATE
or UPDATE
webhook, you can do an upsert
in your database for the id
. The id
always tracks a unique entity. The vid
in the payload denotes the version of the id
, where the highest vid
is the latest version.
Handling Updates and Race Conditions
It is important to note that there is no guarantee of ordering between the insert and update operation webhooks, as they are part of the same atomic operation when a subgraph mapping handler runs. An effective strategy involves utilizing the “deleted_at” and “updated_at” flags in the local representation to manage any potential race conditions.Reference
Create a new webhook
To create a new webhook for a subgraph entity:--secret "some-secret"
to have control over the secret you can use to identify valid traffic from goldsky.