Apollo writeFragment 不更新数据

Apollo writeFragment not updating data

在 react-apollo 2.0.1 中,我有一个 graphql 类型,如下所示:

type PagedThing {
  data: [Thing]
  total: Int
}

当做下面的writeFragment

  client.writeFragment({
    id,
    fragment: gql`
      fragment my_thing on Thing {
        status
      }
    `,
    data: {
      status
    }
  })

缓存未更新,UI 上未显示新数据。还有什么需要做的吗?

PS:为了安全起见,我使用了fragment matching

编辑 1:

我收到以下错误:

Cannot match fragment because __typename property is missing: {"status":"online"}

所以我把代码改成了:

client.writeFragment({
    id,
    fragment: gql`
      fragment my_thing on Thing {
        status
      }
    `,
    data: {
      __typename: 'Thing',
      status
    }
  })

并没有抛出错误,但更新仍然没有发生

事实证明我不仅需要添加 __typename 因为 ID 需要是默认解析的 ID(解释 here

所以我需要执行以下操作才能使其正常工作:

client.writeFragment({
  id: `Thing:${id}`,
  fragment: gql`
    fragment my_thing on Thing {
      status
    }
  `,
  data: {
    __typename: 'Thing',
    status
  }
})

似乎已扩展框架以提供一种名为 'identify' 的方法来避免此问题(以防他们稍后更改实现)

cache.modify({
  id: cache.identify(myPost),
             ^
  fields(fieldValue, details) {
    return details.INVALIDATE;
  },
});

https://www.apollographql.com/docs/react/caching/cache-interaction/#example-invalidating-fields-within-a-cached-object