52

我正在使用 Apollo Client 制作一个应用程序来使用 Graphql 查询我的服务器。我有一个 python 服务器,我在其上执行我的 graphql 查询,该查询从数据库中获取数据,然后将其返回给客户端。

我为客户端创建了一个自定义的 NetworkInterface,它可以帮助我发出自定义的服务器请求(默认情况下,ApolloClient 对我们指定的 URL 进行 POST 调用)。网络接口只需要有一个 query() 方法,我们在其中返回 form 结果的承诺Promise<ExecutionResult>

我能够进行服务器调用并获取请求的数据,但仍然出现以下错误。

Error: Network error: Error writing result to store for query 
{
   query something{
      row{
         data
      }
   }
}
Cannot read property 'row' of undefined
    at new ApolloError (ApolloError.js:32)
    at ObservableQuery.currentResult (ObservableQuery.js:76)
    at GraphQL.dataForChild (react-apollo.browser.umd.js:410)
    at GraphQL.render (react-apollo.browser.umd.js:448)
    at ReactCompositeComponent.js:796
    at measureLifeCyclePerf (ReactCompositeComponent.js:75)
    at ReactCompositeComponentWrapper._renderValidatedComponentWithoutOwnerOrContext (ReactCompositeComponent.js:795)
    at ReactCompositeComponentWrapper._renderValidatedComponent (ReactCompositeComponent.js:822)
    at ReactCompositeComponentWrapper._updateRenderedComponent (ReactCompositeComponent.js:746)
    at ReactCompositeComponentWrapper._performComponentUpdate (ReactCompositeComponent.js:724)
    at ReactCompositeComponentWrapper.updateComponent (ReactCompositeComponent.js:645)
    at ReactCompositeComponentWrapper.performUpdateIfNecessary (ReactCompositeComponent.js:561)
    at Object.performUpdateIfNecessary (ReactReconciler.js:157)
    at runBatchedUpdates (ReactUpdates.js:150)
    at ReactReconcileTransaction.perform (Transaction.js:140)
    at ReactUpdatesFlushTransaction.perform (Transaction.js:140)
    at ReactUpdatesFlushTransaction.perform (ReactUpdates.js:89)
    at Object.flushBatchedUpdates (ReactUpdates.js:172)
    at ReactDefaultBatchingStrategyTransaction.closeAll (Transaction.js:206)
    at ReactDefaultBatchingStrategyTransaction.perform (Transaction.js:153)
    at Object.batchedUpdates (ReactDefaultBatchingStrategy.js:62)
    at Object.enqueueUpdate (ReactUpdates.js:200)

如果可能,我想知道错误的可能原因和解决方案。

4

4 回答 4

80

我有一个类似的错误。我通过在查询中添加 id 来解决这个问题。例如,我当前的查询是

query  {
  service:me {
    productServices {
      id
      title
    }
  }
}

我的新查询是

query  {
  service:me {
    id // <-------
    productServices {
      id
      title
    }
  }
}
于 2019-04-19T04:06:26.680 回答
34

我们需要包含id,否则会导致上述错误。

{
   query something {
      id
      row {
         id
         data
      }
   }
}
于 2019-08-07T11:41:25.193 回答
30

I've finally found out what is causing this issue after battling with it in various parts of our app for months. What helped to shed some light on it was switching from apollo-cache-inmemory to apollo-cache-hermes.

I experimented with Hermes hoping to mitigate this ussue, but unfortunately it fails to update the cache the same as apollo-cache-inmemory. What is curious though is that hermes shows a very nice user friendly message, unlike apollo-cache-inmemory. This lead me to a revelation that cache really hits this problem when it's trying to store an object type that is already in the cache with an ID, but the new object type is lacking it. So apollo-cache-inmemory should work fine if you are meticulously consistent when querying your fields. If you omit id field everywhere for a certain object type it will happily work. If you use id field everywhere it will work correctly. Once you mix queries with and without id that's when cache blows up with this horrible error message.

This is not a bug-it's working as intended, it's even documented here: https://www.apollographql.com/docs/react/caching/cache-configuration/#default-identifiers

2020 update: Apollo has since removed this "feature" from the cache, so this error should not be thrown anymore in apollo-client 3 and newer.

于 2019-05-31T21:49:01.357 回答
3

我有一个类似的问题。

也许您的应用程序正在尝试使用错误的商店地址将(网络响应数据)写入商店?

我的问题的解决方案

player在将 a 添加到 a后,我正在更新商店team

// Apollo option object for `mutation AddPlayer`
update: (store, response) => {
  const addr = { query: gql(QUERY_TEAM), variables: { _id } };
  const data = store.readQuery(addr);
  stored.teams.players.push(response.data.player));
  store.writeQuery({...addr, data});
}

我开始在上面遇到类似的错误(我在 Apollo 2.0.2 上)

深入商店后,我意识到我的QUERY_TEAM请求是用一个meta默认为null. 商店“地址”似乎使用*字符串化addr来标识记录。所以我改变了我上面的代码来模仿包含null:

// Apollo option object for `mutation AddPlayer`
update: (store, response) => {
  const addr = { query: gql(QUERY_TEAM), variables: { _id, meta: null } };
  const data = store.readQuery(addr);
  data.teams.players.push(response.data.player));
  store.writeQuery({...addr, data});
}

这解决了我的问题。

* 默认undefined而不是null可能会避免这个讨厌的错误(未验证

更多信息

我的问题可能只是切线相关,所以如果这没有帮助,我有两个建议:

首先,添加这 3 行以node_modules/apollo-cache-inmemory/lib/writeToStore.js在“记录”为空时提醒您。然后进行调查_a以了解出了什么问题。

exports.writeResultToStore = writeResultToStore;
function writeSelectionSetToStore(_a) {

    var result = _a.result, dataId = _a.dataId, selectionSet = _a.selectionSet, context = _a.context;
    var variables = context.variables, store = context.store, fragmentMap = context.fragmentMap;

    +if (typeof result === 'undefined') {
    +    debugger;
    +}

其次,确保所有查询、突变和手动存储更新都与您期望的变量一起保存

于 2018-01-21T12:29:20.663 回答