React Apollo:在商店中更新可重用查询的特定实例

时间:2018-06-13 18:27:24

标签: apollo react-apollo apollo-client

我代表反应应用中的层次结构。每次单击节点旁边的(+)时,它都会使用GET_CHILDREN查询以及父唯一ID的变量。

然后,有自己孩子的孩子也会有(+)。当然,当您单击它时,它也会使用GET_CHILDREN,并使用新的父ID。

当我运行新的ADD_CHILD突变时,我遇到了麻烦。它提交的数据很好,但我也试图立即更新商店,或者至少指向要重新获取的GET_CHILDREN的特定实例。

这是我到目前为止所得到的:

_createChild = async () => {
  const { newChildId, newChildName } = this.state;
  const { parentId } = this.props;
  await this.props.createChild({
    variables: {
      parentId,
      newChildId,
      newChildName,
    },
    update: (store, { data: { createChild } }) => {
      const data = store.readQuery({ query: GET_CHILDREN });
      data.frontline.splice(0, 0, createChild);

      store.writeQuery({
        query: GET_CHILDREN,
        data,
      });
    },
  });
  this.setState({ open: false });
};

我最终得到了这个错误:

Error: Can't find field children({}) on object (ROOT_QUERY) {
 "counter": {
   "type": "id",
   "generated": true,
   "id": "$ROOT_QUERY.counter",
   "typename": "Counter"
 }
 "children({\"parentId\":\"4684541\"}): [
   {
     "type": "id",
     "generated": false,
     "id": "ChildType:4",
     "typename": "ChildType"
   },
   {
     "type": "id",
     "generated": false,
     "id": "ChildType:5",
     "typename": "ChildType"
   },
...more of those
  ],
 "children({\"parentId\":\"5846752\"}): [
   {
     "type": "id",
     "generated": false,
     "id": "ChildType:23",
     "typename": "ChildType"
   }
  ],
 "children({\"parentId\":\"8458752\"}): [
   {
     "type": "id",
     "generated": false,
     "id": "ChildType:44",
     "typename": "ChildType"
   }
  ],

0 个答案:

没有答案