为什么分片数据被抓取但 Relay 突变无法访问

Why is fragment data fetched but not accessible by Relay mutation

我有突变 (code) in which I want to delete a node. It has a dependency on the post rowId — which is the primary key of the row in the database — and the viewer id. When the pertaining component (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"
    }
  }
}

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

错误表明传给 DeletePostMutation 的 props 不是 Relay 获取的数据,并且查看 code 似乎您正在为 [=21 构造一个新对象=]post 和 viewer 而不是发送中继获取的 post 和 viewer .

我看到你在这样做:

  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 中继容器 中编写 DeletePostMutation 的 GraphQL 片段然后在 DeletePostMutation 中,每个 prop 都应该具有在可访问的片段中定义的字段。