1

I have this schema:

type Game {
  id: ID! @id
  status: Boolean @default(value: false)
  time: DateTime!
  location: String!
  stadium: String!
  teams: [Team!]! @relation(name: "BothTeams")
}
type Team {
  id: ID! @id
  name: String!
  abbrName: String!
  teamLogo: String!
  score: Int @default(value: 0)
  games: [Game!]! @relation(name: "BothTeams")
}

the Game type is gonna return typically two teams and each team is gonna have a score field.

so if i want to update a game later, specifically the score field,

i would have to change the score of every game with that changed team.

So, is there a way to change the score of a specific game, without mutating the original score.

Yassine Bridi
  • 836
  • 2
  • 10
  • 20

1 Answers1

0

Conceptually, a score is not really a property of a particular team. You want to represent it as a separate entity that's associated to both a team and a game. One way to do that:

type Game {
  id: ID! @id
  status: Boolean @default(value: false)
  time: DateTime!
  location: String!
  stadium: String!
  teams: [Team!]! @relation(name: "BothTeams")
  scores: [Score!]! @relation(name: "ScoresByGame")
}
type Team {
  id: ID! @id
  name: String!
  abbrName: String!
  teamLogo: String!
  games: [Game!]! @relation(name: "BothTeams")
}
type Score {
  id: ID! @id
  value: Int @default(value: 0)
  team: Team @relation(name: "ScoreTeam")
}

Or instead of scores, you could have a homeScore field and an awayScore field. Either way, you'll have a Score node that you can mutate without impacting other games. Any other properties of a Team that might vary with each game should be treated similarly (for example, if you wanted to include what players played that particular game).

Daniel Rearden
  • 80,636
  • 11
  • 185
  • 183
  • That would be a better way to approach it, thanks Daniel for the help. – Yassine Bridi Jun 04 '19 at 01:36
  • `Game.status` also should be extracted while building 'append only' system. 'Versioned' score can contain time (offset to game start) and player (change 'author') - build game timeline (last one => game result). Score should be ralated to Game (reverse to ScoresByGame) - f.e. for list of games the player has changed the result. – xadm Jun 04 '19 at 04:51