【问题标题】:Why is fragment data fetched but not accessible by Relay mutation为什么 Relay 突变可以获取片段数据但无法访问
【发布时间】:2016-12-13 07:38:42
【问题描述】:

我有突变(code),我想在其中删除一个节点。它依赖于 post rowId(数据库中行的主键)和查看者 ID。当相关组件 (code) 被渲染时。发送以下查询

query Queries {
  viewer {
    id,
    ...F1
  }
}
fragment F0 on Viewer {
  id
}
fragment F1 on Viewer {
  id,
  ...F0
}

query Queries($id_0:ID!) {
  post(id:$id_0) {
    id,
    ...F2
  }
}
fragment F0 on Post {
  id,
  rowId
}
fragment F1 on Post {
  rowId,
  id
}
fragment F2 on Post {
  headline,
  body,
  id,
  ...F0,
  ...F1
}

我收到的回复包括viewer.idpost.rowId。正如你在这里看到的,

{
  "data": {
    "post": {
      "id": "cG9zdDo0",
      "headline": "You hit me with a cricket bat.",
      "body": "Hello.",
      "rowId": 4
    }
  }
}

这里,

{
  "data": {
    "viewer": {
      "id": "viewer"
    }
  }
}

但是,当我想将它们传递给 DeletePostMutation 时,就像 this.props.post.id 一样,它们是 undefined。当我检查this.props.post 时,我得到以下信息

【问题讨论】:

    标签: graphql relayjs


    【解决方案1】:

    错误表明传递给 DeletePostMutation 的道具不是 Relay 获取的数据,并且查看 code 似乎您正在为 post 构造一个新对象查看器,而不是发送通过中继获取的帖子和查看器

    我看到你正在这样做:

      handleDelete(event) {
        this.props.relay.commitUpdate(
          new DeletePostMutation({
            post: { rowId: this.props.post.rowId },
            viewer: { id: this.props.viewer.id },
          })
        )
      }
    

    试试这个:

      handleDelete(event) {
        this.props.relay.commitUpdate(
          new DeletePostMutation({
            post: this.props.post,
            viewer: this.props.viewer,
          })
        )
      }
    

    由于您已经在 Post Relay Container 中编写 DeletePostMutation 的 GraphQL 片段,因此在 DeletePostMutation 中,每个道具都应该具有可访问片段中定义的字段。

    【讨论】:

    • @jose-r-cruz 非常感谢。现在敲我的头。应该考虑一下?
    猜你喜欢
    • 2020-10-15
    • 1970-01-01
    • 2022-10-25
    • 2017-05-05
    • 1970-01-01
    • 2019-11-17
    • 2023-03-20
    • 2013-03-31
    • 2021-05-18
    相关资源
    最近更新 更多