Apollo客户端缓存无法识别一对一关系

我有一个带有对象places的graphql模式,该对象与另一个对象placeDetails具有一对一的关系

在为placeDetails插入place的突变之后,我已经更新了我的阿波罗缓存。我正在使用optimisticResponse进行突变,并且部分工作正常。

问题在于,乐观响应会在服务器实际返回突变之前在屏幕上短暂闪烁,这似乎会覆盖我在自定义update函数中对缓存所做的任何手动更改。另一个令人困惑的问题是,阿波罗缓存将新插入的placeDetails对象与正确的places对象正确关联,但是在缓存中未创建相反的关系。我已经检查了缓存并看到了这样的

placeDetails:ChIJq0HUUq6ipBIRWM6qGqALmok:
  places: {"__ref":"places:a60803b7-378d-4c6c-a41a-ada68f64a020"}

但是,如果我查看缓存中的places:a60803b7-378d-4c6c-a41a-ada68f64a020条目,则placeDetails的值为空。

更新功能和乐观的反应

optimisticResponse: {
  __typename: "Mutation",placeDetails: {
    otherId: place.otherId,// From component calling the mutation
    name: name,// From component calling the mutation
    __typename: "placeDetails",places: {
      id: place.id,// From component calling the mutation
      __typename: "places"
    }
  }
},update: (proxy,{ data }) => {
  const id = `places:${place.id}`;
  const fragment = gql`
    id
    placeDetails {
      otherId
      name
      __typename
    }
    __typename
  `;
   const place: Place = proxy.readFragment<Place>({
      fragment,id
   })!;
   const updatedPlace: Place = {
       ...place,placeDetails: data.placeDetails
   };
   proxy.writeFragment({
     id,fragment,data: updatedPlace
   });
  }
}

简化突变

mutation InsertPlaceDetails(
    $otherId: String!
    $name: String!
  ) {
    insert_placeDetails(
      objects: [
        {
          otherId: $otherId
          name: $name
        }
      ]
    ) {
      returning {
        otherId
        name
        __typename
        places {
          id
          __typename
        }
      }
    }
  }

我将示例简化了一些。 otherIdplaceDetails上的一列,是places上一列的FK。我已经通过dataIdFromObject配置选项将缓存正确配置为使用正确的ID。

任何帮助将不胜感激!如有必要,我还可以提供更多详细信息

HUNDUNYISI 回答:Apollo客户端缓存无法识别一对一关系

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/3162895.html

大家都在问