3. Define your schema
Create your schema by defining entities.
In GhostGraph, you write your entities in the form of Solidity structs like in the below examples. (You'll be able to query and save entities from within your indexer handlers).
In the cases where you want to use mappings or arrays, you can instead create @many
and @belongsTo
relationships by using these decorators.
We've built a custom Solidity preprocessor to parse these decorators so that you can explicitly establish relationships between entities.
In this example, one Pair references many Swaps, so we use the @many
keyword. In the Swap struct, @belongsTo
specifies the Pair that each swap belongs to.
Full Example
struct Pair {
address id;
address token0;
address token1;
uint256 reserve0;
uint256 reserve1;
@many(Swap.pairId) swaps;
uint32 createdAt;
bytes32 createdTxHash;
uint32 lastSwapTime;
bytes32 lastSwapTxHash;
}
struct Swap {
string id;
@belongsTo(Pair.id) pairId;
uint256 amount0In;
uint256 amount1In;
uint256 amount0Out;
uint256 amount1Out;
uint32 time;
bytes32 txHash;
}
FAQs
id
is requiredEvery entity must have an id
property which can be bytes32
, address
or string
@belongsTo
should end with Id
if you want the object association.
For example: the swap struct above has @belongsTo(Pair.id) pairId
. When this is deployed, you will be able to query:
query Swaps {
swaps {
items {
pairId
pair {
id
token0
}
}
}
}
- Does every
@belongsTo
require@many
?
- No, you can have a one-to-one relationship using
@belongsTo
without having an associated@many
.
- Does
@belongsTo
need to end withId
?
- No, if you do not care about object association, you don't need to end with
Id
- Does every
@many
require@belongsTo
?
- Yes, it does. If you try to codegen
@many
without an associated@belongsTo
, our API should throw an error.