- 
                Notifications
    
You must be signed in to change notification settings  - Fork 1.1k
 
Subgraph Composition: Entity Ops Detection in Handlers #5636
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
          
     Closed
      
      
            incrypto32
  wants to merge
  1
  commit into
  zoran/subgraph-composition-sql-more-entities
from
krishna/subgraph-comp-entity-ops-wasm
  
      
      
   
      
    
  
     Closed
                    Changes from all commits
      Commits
    
    
  File filter
Filter by extension
Conversations
          Failed to load comments.   
        
        
          
      Loading
        
  Jump to
        
          Jump to file
        
      
      
          Failed to load files.   
        
        
          
      Loading
        
  Diff view
Diff view
There are no files selected for viewing
  
    
      This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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.
        
    
  
  Add this suggestion to a batch that can be applied as a single commit.
  This suggestion is invalid because no changes were made to the code.
  Suggestions cannot be applied while the pull request is closed.
  Suggestions cannot be applied while viewing a subset of changes.
  Only one suggestion per line can be applied in a batch.
  Add this suggestion to a batch that can be applied as a single commit.
  Applying suggestions on deleted lines is not supported.
  You must change the existing code in this line in order to create a valid suggestion.
  Outdated suggestions cannot be applied.
  This suggestion has been applied or marked resolved.
  Suggestions cannot be applied from pending reviews.
  Suggestions cannot be applied on multi-line comments.
  Suggestions cannot be applied while the pull request is queued to merge.
  Suggestion cannot be applied right now. Please check back later.
  
    
  
    
Uh oh!
There was an error while loading. Please reload this page.