【问题标题】:Error: Network error: Error writing result to store for query (Apollo Client)错误:网络错误:将结果写入存储以供查询时出错(Apollo 客户端)
【发布时间】:2017-11-08 07:00:51
【问题描述】:

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

我已经为客户端创建了一个自定义的 NetworkInterface,它可以帮助我发出自定义的服务器请求(默认情况下,ApolloClient 对我们指定的 URL 进行 POST 调用)。网络接口只需要一个 query() 方法,我们在其中返回 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)

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

【问题讨论】:

  • { query something{ row{ data } } } 应该是什么?
  • 指定组件所需数据的是graphql查询
  • 无论何时使用“fetchPolicy={"cache-and-network"}”,都必须为每个 graphql 对象包含“id”。 {查询东西{id行{“id”数据}}}

标签: javascript graphql react-apollo apollo-client


【解决方案1】:

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

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

我的新查询是

query  {
  service:me {
    id // <-------
    productServices {
      id
      title
    }
  }
}

【讨论】:

    【解决方案2】:

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

    {
       query something {
          id
          row {
             id
             data
          }
       }
    }
    

    【讨论】:

      【解决方案3】:

      在我们的应用程序的各个部分与此问题斗争了几个月后,我终于找到了导致此问题的原因。从 apollo-cache-inmemory 切换到 apollo-cache-hermes 有助于对此有所了解。

      我尝试了 Hermes 希望缓解这个问题,但不幸的是它无法像apollo-cache-inmemory 一样更新缓存。奇怪的是,与apollo-cache-inmemory 不同,hermes 显示了非常好的用户友好信息。这让我发现,当缓存尝试存储已经在缓存中的具有 ID 的对象类型时,它确实遇到了这个问题,但是新的对象类型缺少它。因此,如果您在查询字段时一丝不苟地保持一致,apollo-cache-inmemory 应该可以正常工作。如果您在任何地方为某个对象类型省略 id 字段,它将很高兴地工作。如果您在任何地方使用id 字段,它将正常工作。一旦你混合了带和不带 id 的查询,缓存就会因这个可怕的错误消息而爆炸。

      这不是错误 - 它按预期工作,甚至在此处记录:https://www.apollographql.com/docs/react/caching/cache-configuration/#default-identifiers

      2020 年更新:Apollo has since removed this "feature" from the cache,因此在 apollo-client 3 及更高版本中不应再抛出此错误。

      【讨论】:

      • 感谢您解释问题的根本原因。将id 添加到查询中以使其与其他查询一致我已经为我解决了这个问题。
      • 天才爱马仕完成了这项工作github.com/convoyinc/apollo-cache-hermes
      【解决方案4】:

      我遇到了类似的问题。

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

      解决我的问题

      在将player 添加到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 默认为nullstore "address" 似乎使用 *stringified 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;
          +}
      

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

      【讨论】:

        猜你喜欢
        • 2019-07-27
        • 2018-10-14
        • 2019-10-18
        • 2019-03-17
        • 1970-01-01
        • 2018-03-18
        • 2019-12-03
        • 1970-01-01
        • 2021-06-16
        相关资源
        最近更新 更多