简体   繁体   中英

How to mutate the state of an array in graphql?

I am trying to add an array inside a GraphQl schema. Currently, it looks like this:

type Play {
  winRatio: [Float]
}

The purpose of the win ratio is to hold the history of all win ratios. For example, after 3 games, the winRatio array should look like this:

winRatio: [1, .5, .66]

Currently, when I mutate the winRatio array, no history is saved; the state updates to the latest value. This is how that looks:

await API.graphql({
  query: updatePlay,
  variables: {
    input: {
      winRatio: [winRatio, (await wins) / (await totalPlays)], // update the player's win ratio
    },
  },
})

However, when I query for the winRatio data in graphql, I get an array with ONLY the latest value:

winRatio:[.66]

I feel like I need the spread operator, but I am not sure why my current solution is not solving my problem.

是的,您是正确的,因为您需要点差运算符。

winRatio: [...winRatio, (await wins) / (await totalPlays)]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM