graph-tooling icon indicating copy to clipboard operation
graph-tooling copied to clipboard

Could not create WasmInstance from valid module with context: unknown import: `index::store.loadRelated` has not been defined'

Open hexzerodev opened this issue 2 years ago • 12 comments

Which packages are impacted by your issue?

@graphprotocol/graph-cli

Describe the issue

Trying to run unit tests in my subgraph but getting an error:

Could not create WasmInstance from valid module with context: unknown import: `index::store.loadRelated` has not been defined'

Here the versions im using

"dependencies": {
    "@graphprotocol/graph-cli": "0.51.1",
    "@graphprotocol/graph-ts": "0.31.0"
  },
  "devDependencies": { "matchstick-as": "0.5.0" }

Here's a minimal reproducible example https://github.com/hexzerodev/subgraph-load-related-error

The error pops up when running graph test

Reproduction

https://github.com/hexzerodev/subgraph-load-related-error

Steps to Reproduce the Bug or Issue

1. npm install
2. graph test

Expected behavior

Test to be compiled properly

Screenshots or Videos

No response

Platform

- OS: Mac
- NodeJS: 19.8.1
- `@graphprotocol/*` version(s): v0.51.1

Subgraph Manifest

specVersion: 0.0.5
description: 'example of a subgraph'
repository: https://github.com/graphprotocol/graph-tooling.git
schema:
  file: ./schema.graphql
dataSources:
  - kind: ethereum/contract
    name: ExampleSubgraph
    network: mainnet
    source:
      address: '0x22843e74c59580b3eaf6c233fa67d8b7c561a835'
      abi: ExampleContract
    mapping:
      kind: ethereum/events
      apiVersion: 0.0.7
      language: wasm/assemblyscript
      file: ./src/mapping.ts
      entities:
        - ExampleEntity
        - OtherEntity
      abis:
        - name: ExampleContract
          file: ./abis/ExampleContract.json
      eventHandlers:
        - event: ExampleEvent(indexed string)
          handler: handleExampleEvent

Subgraph GraphQL Schema

type ExampleEntity @entity {
  id: ID!

  optionalBoolean: Boolean
  requiredBoolean: Boolean!
  optionalBooleanList: [Boolean!]
  requiredBooleanList: [Boolean!]!

  optionalString: String
  requiredString: String!
  optionalStringList: [String!]
  requiredStringList: [String!]!

  optionalBytes: Bytes
  requiredBytes: Bytes!
  optionalBytesList: [Bytes!]
  requiredBytesList: [Bytes!]!

  optionalInt: Int
  requiredInt: Int!
  optionalIntList: [Int!]
  requiredIntList: [Int!]!

  optionalBigInt: BigInt
  requiredBigInt: BigInt!
  optionalBigIntList: [BigInt!]
  requiredBigIntList: [BigInt!]!

  optionalReference: OtherEntity
  requiredReference: OtherEntity!
  optionalReferenceList: [OtherEntity!]
  requiredReferenceList: [OtherEntity!]!
}

type OtherEntity @entity {
  id: ID!
}

type User @entity {
  id: ID!
  objects: [Object!]! @derivedFrom(field: "owner")
}

type Object @entity {
  id: ID!
  owner: User
}

Additional context

No response

hexzerodev avatar Jun 17 '23 08:06 hexzerodev

@dimitrovmaksim @georg-getz do we need to update Matchstick for the new loadRelated functions?

azf20 avatar Jun 19 '23 08:06 azf20

Hey @azf20, sorry for the late reply. Yeah all the new functions would need to be added/mocked in Matchstick.

dimitrovmaksim avatar Jun 28 '23 10:06 dimitrovmaksim

I'm facing the same issue, how do I fix it?

Update: I think I understand the issue, it seems it is new functionality in the subgraph that has to be implemented in matchstick-as.

EdsonAlcala avatar Aug 03 '23 14:08 EdsonAlcala

I'm running into the same issue.

Has anyone figured out how to write unit tests for this kind of relationship? Where a User has an array of Objects that gets added, removed, changed in response to new events?

This is what the documentation currently says https://thegraph.com/docs/en/developing/assemblyscript-api/#loading-entities-from-the-store

Looking up derived entities⁠#Link to this section As of graph-node v0.31.0, @graphprotocol/graph-ts v0.31.0 and @graphprotocol/graph-cli v0.51.0 the loadRelated method is available.

This enables loading derived entity fields from within an event handler. For example, given the following schema:

type Token @entity {
  id: ID!
  holder: Holder!
  color: String
}

type Holder @entity {
  id: ID!
  tokens: [Token!]! @derivedFrom(field: "holder")
}

The following code will load the Token entity that the Holder entity was derived from:


let holder = Holder.load('test-id')
// Load the Token entity that the Holder entity was derived from
let token = holder.tokens.load()

This method fails with matchstick-as with this error


 Compile subgraphERROR TS2339: Property 'loadRelated' does not exist on type '~lib/@graphprotocol/graph-ts/index/store'.

     let value = store.loadRelated(this._entity, this._id, this._field);

parkerburchett avatar Aug 06 '23 18:08 parkerburchett

Also running into this problem. Is anyone working on it? If not, I might try to implement it as it's blocking some stuff I am trying to build.

Preston-Harrison avatar Aug 08 '23 00:08 Preston-Harrison

hey @Preston-Harrison I think @saihaj was looking at Matchstick, but this isn't currently being worked on, would be great if you might be able to help?

azf20 avatar Aug 09 '23 11:08 azf20

Hi @Preston-Harrison, I agree with @azf20. I also think that they are not currently working on it. I need your help too)

kevandee avatar Aug 23 '23 10:08 kevandee

Hello,

still getting that issue with following dependencies:

    "dependencies": {
        "@graphprotocol/graph-cli": "0.68.5",
        "@graphprotocol/graph-ts": "0.33.0"
    },
    "devDependencies": {
        "matchstick-as": "0.6.0"
    }

this is blocking if I keep our smart contract event unchanged. But if I change them, I do only cause previous The Graph version (graph-cli:0.46.1 ; graph-ts: 0.29.1) are not handling N-M relationships correctly within subgraph logic... A fix on that issue would be consequently much appreciated.

Best,

mffrench avatar Mar 15 '24 10:03 mffrench

loadRelated should be supported in matchstick 0.6.0 and matchstick-as 0.6.0

dimitrovmaksim avatar Mar 15 '24 11:03 dimitrovmaksim

Well just tested with the dependencies I told you - upgraded matchstick-as from 0.5.0 to 0.6.0 for that purpose...

I will retest just to be sure anyway.

Is there a way to display the matchstick version used while testing ?

Thank you

mffrench avatar Mar 15 '24 12:03 mffrench

You can see the version with graph test -- --version And you can force a specific version with graph test -v 0.6.0 But unless the version is specified it should always use the latest version, which currently is 0.6.0

dimitrovmaksim avatar Mar 15 '24 12:03 dimitrovmaksim

OK, so I confirm matchstick 0.6.0 is solving that 🆘 Could not create WasmInstance from valid module with context: unknown import: index::store.loadRelated has not been defined error. Thank you @dimitrovmaksim .

mffrench avatar Mar 15 '24 13:03 mffrench