-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Subgraph Composition: Entity Ops Detection in Handlers
- Loading branch information
1 parent
868060b
commit cf4951c
Showing
14 changed files
with
229 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,13 @@ | ||
|
||
type Block @entity { | ||
id: ID! | ||
number: BigInt! | ||
hash: Bytes! | ||
testMessage: String | ||
} | ||
|
||
type Block2 @entity { | ||
id: ID! | ||
number: BigInt! | ||
hash: Bytes! | ||
testMessage: String | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,4 +2,5 @@ type MirrorBlock @entity { | |
id: String! | ||
number: BigInt! | ||
hash: Bytes! | ||
testMessage: String | ||
} |
37 changes: 34 additions & 3 deletions
37
tests/integration-tests/subgraph-data-sources/src/mapping.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,46 @@ | ||
import { Entity, log } from '@graphprotocol/graph-ts'; | ||
import { Entity, log, store } from '@graphprotocol/graph-ts'; | ||
import { MirrorBlock } from '../generated/schema'; | ||
|
||
export function handleEntity(blockEntity: Entity): void { | ||
export class EntityTrigger { | ||
constructor( | ||
public entityOp: u32, | ||
public entityType: string, | ||
public entity: Entity, | ||
public vid: i64, | ||
) {} | ||
} | ||
|
||
export function handleEntity(trigger: EntityTrigger): void { | ||
let blockEntity = trigger.entity; | ||
let blockNumber = blockEntity.getBigInt('number'); | ||
let blockHash = blockEntity.getBytes('hash'); | ||
let testMessage = blockEntity.get('testMessage'); | ||
let id = blockEntity.getString('id'); | ||
|
||
log.info('Block number: {}', [blockNumber.toString()]); | ||
|
||
let block = new MirrorBlock(id); | ||
if (trigger.entityOp == 2) { | ||
log.info('Removing block entity with id: {}', [id]); | ||
store.remove('MirrorBlock', id); | ||
return; | ||
} | ||
|
||
let block = loadOrCreateMirrorBlock(id); | ||
block.number = blockNumber; | ||
block.hash = blockHash; | ||
if (testMessage) { | ||
block.testMessage = testMessage.toString(); | ||
} | ||
|
||
block.save(); | ||
} | ||
|
||
export function loadOrCreateMirrorBlock(id: string): MirrorBlock { | ||
let block = MirrorBlock.load(id); | ||
if (!block) { | ||
log.info('Creating new block entity with id: {}', [id]); | ||
block = new MirrorBlock(id); | ||
} | ||
|
||
return block; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,35 @@ | ||
import { Entity, log } from '@graphprotocol/graph-ts'; | ||
|
||
export function handleBlock(content: Entity): void { | ||
let stringContent = content.getString('val'); | ||
export const SubgraphEntityOpCreate: u32 = 0; | ||
export const SubgraphEntityOpModify: u32 = 1; | ||
export const SubgraphEntityOpDelete: u32 = 2; | ||
|
||
export class EntityTrigger { | ||
constructor( | ||
public entityOp: u32, | ||
public entityType: string, | ||
public entity: Entity, | ||
public vid: i64, | ||
) {} | ||
} | ||
|
||
export function handleBlock(content: EntityTrigger): void { | ||
let stringContent = content.entity.getString('val'); | ||
log.info('Content: {}', [stringContent]); | ||
log.info('EntityOp: {}', [content.entityOp.toString()]); | ||
|
||
switch (content.entityOp) { | ||
case SubgraphEntityOpCreate: { | ||
log.info('Entity created: {}', [content.entityType]); | ||
break | ||
} | ||
case SubgraphEntityOpModify: { | ||
log.info('Entity modified: {}', [content.entityType]); | ||
break; | ||
} | ||
case SubgraphEntityOpDelete: { | ||
log.info('Entity deleted: {}', [content.entityType]); | ||
break; | ||
} | ||
} | ||
} |
Oops, something went wrong.