带有变异图cool / graphql的重复项目

时间:2018-01-15 21:18:49

标签: javascript graphql graphcool

我正在使用graphcool / graphql,并希望将Player添加到Team。我的架构如下所示:

type Team @model {
  id: ID! @isUnique
  name: String
  players: [Player!]! @relation(name: "TeamPlayers")
  fixtures: [Fixture!]! @relation(name: "TeamFixtures")
  results: [Result!]! @relation(name: "TeamResults")
}

type Player @model {
    id: ID! @isUnique
    name: String!
    gamesPlayed: Int!
    goalsScored: Int!
    yellowCards: Int!
    redCards: Int!
    photo: String!
    team: Team! @relation(name: "TeamPlayers")
}

我正在做以下突变:

mutation {
      createPlayer(
      name: "Peter Flanagan",
      gamesPlayed: 1,
      yellowCards: 0,
      redCards: 1,
      goalsScored: 4,
      photo: "http://cdn2-www.craveonline.com/assets/uploads/2017/02/bret1.png",
      team: {
        name: "Man United"
      }
    ) {
        id
      }
}

这可以按预期工作,但是如果我进行另一个突变,如下面的那个,第二个团队,也称为Man United,新id

mutation {
      createPlayer(
      name: "Eric Cantona",
      gamesPlayed: 1,
      yellowCards: 0,
      redCards: 1,
      goalsScored: 4,
      photo: "http://cdn2-www.craveonline.com/assets/uploads/2017/02/bret1.png",
      team: {
        name: "Man United"
      }
    ) {
        id
      }
}

有人可以建议我如何避免此问题并将Player添加到同一个Team

1 个答案:

答案 0 :(得分:2)

通过这种方式,您始终可以通过创建新的Team来创建新的Player。如果你想将球员与球队联系起来。首先创建team并获取teamId。使用teamId,您可以创建与团队相关联的突变:

mutation {
  createPlayer(
  name: "Peter Flanagan",
  gamesPlayed: 1,
  yellowCards: 0,
  redCards: 1,
  goalsScored: 4,
  photo: "http://cdn2-www.craveonline.com/assets/uploads/2017/02/bret1.png",
  teamId: "TEAM_ID"
) {
    id
  }
}