Handle subgraph schema entity array type and relation fields (#49)

* Handle relation entities in subgraph

* Modify eden-watcher entities to handle subgraph schema data types

* Modify schema gql to match eden subgraph gql shape

* Handle array type fields in subgraph schema

* Fix store get api for array type fields in subgraph entities

* Handle array type in eden-watcher and format await used in params
This commit is contained in:
nikugogoi
2021-12-28 16:08:05 +05:30
committed by nabarun
parent 158c3928c9
commit 800ad79baf
25 changed files with 527 additions and 221 deletions
@@ -12,6 +12,70 @@ import {
BigDecimal
} from "@graphprotocol/graph-ts";
export class RelatedEntity extends Entity {
constructor(id: string) {
super();
this.set("id", Value.fromString(id));
this.set("paramBigInt", Value.fromBigInt(BigInt.zero()));
this.set("examples", Value.fromStringArray(new Array(0)));
this.set("bigIntArray", Value.fromBigIntArray(new Array(0)));
}
save(): void {
let id = this.get("id");
assert(id != null, "Cannot save RelatedEntity entity without an ID");
if (id) {
assert(
id.kind == ValueKind.STRING,
"Cannot save RelatedEntity entity with non-string ID. " +
'Considering using .toHex() to convert the "id" to a string.'
);
store.set("RelatedEntity", id.toString(), this);
}
}
static load(id: string): RelatedEntity | null {
return changetype<RelatedEntity | null>(store.get("RelatedEntity", id));
}
get id(): string {
let value = this.get("id");
return value!.toString();
}
set id(value: string) {
this.set("id", Value.fromString(value));
}
get paramBigInt(): BigInt {
let value = this.get("paramBigInt");
return value!.toBigInt();
}
set paramBigInt(value: BigInt) {
this.set("paramBigInt", Value.fromBigInt(value));
}
get examples(): Array<string> {
let value = this.get("examples");
return value!.toStringArray();
}
set examples(value: Array<string>) {
this.set("examples", Value.fromStringArray(value));
}
get bigIntArray(): Array<BigInt> {
let value = this.get("bigIntArray");
return value!.toBigIntArray();
}
set bigIntArray(value: Array<BigInt>) {
this.set("bigIntArray", Value.fromBigIntArray(value));
}
}
export class ExampleEntity extends Entity {
constructor(id: string) {
super();
@@ -24,6 +88,7 @@ export class ExampleEntity extends Entity {
this.set("paramBytes", Value.fromBytes(Bytes.empty()));
this.set("paramEnum", Value.fromString(""));
this.set("paramBigDecimal", Value.fromBigDecimal(BigDecimal.zero()));
this.set("related", Value.fromString(""));
}
save(): void {
@@ -114,4 +179,13 @@ export class ExampleEntity extends Entity {
set paramBigDecimal(value: BigDecimal) {
this.set("paramBigDecimal", Value.fromBigDecimal(value));
}
get related(): string {
let value = this.get("related");
return value!.toString();
}
set related(value: string) {
this.set("related", Value.fromString(value));
}
}
@@ -3,6 +3,13 @@ enum EnumType {
choice2
}
type RelatedEntity @entity {
id: ID!
paramBigInt: BigInt!
examples: [ExampleEntity!]!
bigIntArray: [BigInt!]!
}
type ExampleEntity @entity {
id: ID!
count: BigInt!
@@ -12,4 +19,5 @@ type ExampleEntity @entity {
paramBytes: Bytes!
paramEnum: EnumType!
paramBigDecimal: BigDecimal!
related: RelatedEntity!
}
@@ -4,7 +4,7 @@ import {
Example1,
Test
} from '../generated/Example1/Example1';
import { ExampleEntity } from '../generated/schema';
import { ExampleEntity, RelatedEntity } from '../generated/schema';
export function handleTest (event: Test): void {
log.debug('event.address: {}', [event.address.toHexString()]);
@@ -15,12 +15,12 @@ export function handleTest (event: Test): void {
// Entities can be loaded from the store using a string ID; this ID
// needs to be unique across all entities of the same type
let entity = ExampleEntity.load(event.transaction.from.toHex());
let entity = ExampleEntity.load(event.transaction.hash.toHexString());
// Entities only exist after they have been saved to the store;
// `null` checks allow to create entities on demand
if (!entity) {
entity = new ExampleEntity(event.transaction.from.toHex());
entity = new ExampleEntity(event.transaction.hash.toHexString());
// Entity fields can be set using simple assignments
entity.count = BigInt.fromString('0');
@@ -37,6 +37,25 @@ export function handleTest (event: Test): void {
entity.paramEnum = 'choice1';
entity.paramBigDecimal = BigDecimal.fromString('123');
let relatedEntity = RelatedEntity.load(event.transaction.from.toHex());
if (!relatedEntity) {
relatedEntity = new RelatedEntity(event.transaction.from.toHex());
relatedEntity.paramBigInt = BigInt.fromString('123');
}
const bigIntArray = relatedEntity.bigIntArray;
bigIntArray.push(entity.count);
relatedEntity.bigIntArray = bigIntArray;
const examples = relatedEntity.examples;
examples.push(entity.id);
relatedEntity.examples = examples;
relatedEntity.save();
entity.related = relatedEntity.id;
// Entities can be written to the store with `.save()`
entity.save();