带有突变图酷/ graphql的重复项目

Posted

技术标签:

【中文标题】带有突变图酷/ graphql的重复项目【英文标题】:duplicated items with mutation graph cool / 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

【问题讨论】:

【参考方案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
  

【讨论】:

以上是关于带有突变图酷/ graphql的重复项目的主要内容,如果未能解决你的问题,请参考以下文章

带有嵌套突变的 Graphql?

带有嵌套突变的 Graphql?

如何在突变后更新阿波罗缓存(使用过滤器查询)

为啥 GraphQl 说我忘记为突变发送参数 [重复]

带有嵌套类型的 Graphql 突变?

如何将单个枚举用于 django 模型和 graphql 突变参数?