【问题标题】:duplicated items with mutation graph cool / graphql带有突变图酷/ graphql的重复项目
【发布时间】:2018-06-24 13:04:17
【问题描述】:

我正在使用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
      }
}

谁能告诉我如何避免这个问题并将Players 添加到同一个Team

【问题讨论】:

    标签: javascript graphql graphcool


    【解决方案1】:

    这样,您始终可以通过创建新的Player 来创建新的Team。如果您想将玩家连接到团队。先创建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
      }
    }
    

    【讨论】:

      猜你喜欢
      • 2023-03-05
      • 2017-07-28
      • 2014-04-24
      • 1970-01-01
      • 1970-01-01
      • 2020-12-09
      • 2018-03-02
      • 1970-01-01
      相关资源
      最近更新 更多