简体   繁体   English

调用钩子时出现意外错误:不可为空的字段缺少值

[英]Unexpected error upon calling hook: Missing value for non-nullable field

I am trying to build a subgraph for Graph Protocol following the example here (the example with the more performant way).我正在尝试按照此处的示例(具有更高性能方式的示例)为 Graph Protocol 构建一个子图。 During compilation of my Graph protocol project using a testing tool matchstick-as ^0.5.0 , I get the following error:在使用测试工具 matchstick-as ^0.5.0编译我的 Graph 协议项目期间,我收到以下错误:

Igniting tests

nft-lottery
--------------------------------------------------
  Enter the NFT Lottery:
thread 'main' panicked at '🆘 Unexpected error upon calling hook: Missing value for non-nullable field 'player' for an entity of type 'PlayerToLottery'.
wasm backtrace:
    0: 0x331a - <unknown>!src/nft-lottery/handleNftLotteryEnter;
    1: 0x3807 - <unknown>!start:tests/nft-lottery.test~anonymous|0~anonymous|0

Could anyone help me with this, please?请问有人可以帮我吗?

Here is my ./schema.graphql这是我的./schema.graphql

type NftLottery @entity {
  id: ID!
  open: BigInt!
  close: BigInt!
  prize: BigInt!
  players: [PlayerToLottery!]! @derivedFrom(field: "lottery")
  requestId: BigInt
  updatedAtTimestamp: BigInt
}

type Player @entity {
  id: ID! # address
  lotteries: [PlayerToLottery!]! @derivedFrom(field: "player")
}

type PlayerToLottery @entity {
  id: ID! # Set playerAddress.toHexString() + lotteryId.toHexString()
  player: Player!
  lottery: NftLottery!
}

# events
type NftLotteryEnter @entity {
  id: ID! # Set lotteryCounter + playerAddress
  lotteryCounter: BigInt!
  player: Bytes!
  numberOfEntrances: [BigInt!]!
  updatedAtTimestamp: BigInt
}

Here is my ./src/mapping.ts这是我的./src/mapping.ts

function getIdFromEventAddressInt(par2: Address, par1: BigInt): string {
  return par1.toHexString() + par2.toHexString()
}

export function handleNftLotteryEnter(event: NftLotteryEnterEvent): void {
  /* if a PlayerToLottery does not exists, create it */
  const playerLotteryId = getIdFromEventAddressInt(event.params.player, event.params.lotteryCounter)
  let playerToLottery = PlayerToLottery.load(playerLotteryId)
  if (!playerToLottery) {
      playerToLottery = new PlayerToLottery(playerLotteryId)
      playerToLottery.save()
    }

  /* if a player does not exist, create them */
  const playerId = event.params.player.toHexString()
  let player = Player.load(playerId)
  if (!player) {
    player = new Player(playerId)
    player.save()
  }

  /* if a lottery does not exist, create it */ 
  const itemId = event.params.lotteryCounter.toHexString()
  let nftLottery = NftLottery.load(itemId)
  if (!nftLottery) {
    nftLottery = new NftLottery(itemId)
    nftLottery.open = event.block.timestamp
    nftLottery.close = BigInt.fromString("000000000000")
    nftLottery.prize = BigInt.fromString(event.params.entranceFee.toString())
    nftLottery.players = new Array<string>()
  }
  // update lottery data
  nftLottery.prize.plus(event.params.entranceFee)
  // update players
  let arrPlayers = nftLottery.players
  arrPlayers.push(event.params.player.toHexString())
  nftLottery.players = arrPlayers

  nftLottery.updatedAtTimestamp = event.block.timestamp
  nftLottery.save()
}

Entity PlayerToLottery needs to have values assigned before saving it:实体PlayerToLottery需要在保存之前分配值:

/* if a PlayerToLottery does not exists, create it */
  const playerLotteryId = getIdFromEventAddressInt(event.params.player, event.params.lotteryCounter)
  let playerToLottery = PlayerToLottery.load(playerLotteryId)
  if (!playerToLottery) {
      playerToLottery = new PlayerToLottery(playerLotteryId)
      playerToLottery.player = event.params.player.toHexString()
      playerToLottery.lottery = event.params.lotteryCounter.toHexString()
      playerToLottery.save()
  }

Credit goes to Maks#3349 at unit testing of Graph discord.归功于 Maks#3349 在图 discord 的单元测试中。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 FakeData“错误:无法为不可为空的字段返回空值” - FakeData "Error: Cannot return null for non-nullable field" 阿波罗服务器+续集错误不可为空的字段 - apollo server + sequelize error non-nullable field GraphQL非空字段嵌套在可空字段中 - GraphQL non-nullable field nested in nullable field 错误:GraphQL 错误:无法为不可为空的字段 Mutation.updateRendezvous 返回 null - Error: GraphQL error: Cannot return null for non-nullable field Mutation.updateRendezvous graphql supertest,错误消息:&#39;不能为不可为空的字段 Mutation 返回 null - graphql supertest , error message: 'Cannot return null for non-nullable field Mutation 架构 graphql 错误给出“无法为不可为空的字段 Organisation.id 返回 null”为什么? - Schema graphql error gives 'Cannot return null for non-nullable field Organisation.id' why? 为什么在执行更改时会收到“无法为不可为空的字段返回 null”错误? - Why am I getting a “Cannot return null for non-nullable field” error when doing a mutation? GraphQL 查询返回错误“不能为不可为空的字段返回空值” - GraphQL query returns error "Cannot return null for non-nullable field" Nestje 订阅获取 NULL 并出现错误:“不能为不可为空的字段订阅返回 null” - Nestje subscription getting NULL and with the error: "Cannot return null for non-nullable field Subscription" 错误:不能为不可为空的字段 Post.title 返回 null - Error: Cannot return null for non-nullable field Post.title
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM