【问题标题】:Error: Network error: Error writing result to store for query:错误:网络错误:将结果写入存储以供查询时出错:
【发布时间】:2019-07-27 06:22:08
【问题描述】:

Apollo 收到此错误:

core.js:14576 ERROR Error: Network error: Error writing result to store for query:
 {"kind":"Document","definitions":[{"kind":"OperationDefinition","operation":"query","name":{"kind":"Name","value":"AdditionalServices"},"variableDefinitions":[{"kind":"VariableDefinition","variable":{"kind":"Variable","name":{"kind":"Name","value":"vendorID"}},"type":{"kind":"NonNullType","type":{"kind":"NamedType","name":{"kind":"Name","value":"ID"}}},"directives":[]}],"directives":[],"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"vendor"},"arguments":[{"kind":"Argument","name":{"kind":"Name","value":"id"},"value":{"kind":"Variable","name":{"kind":"Name","value":"vendorID"}}}],"directives":[],"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","alias":{"kind":"Name","value":"services"},"name":{"kind":"Name","value":"products"},"arguments":[{"kind":"Argument","name":{"kind":"Name","value":"productTypes"},"value":{"kind":"EnumValue","value":"service"}}],"directives":[],"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"nodes"},"arguments":[],"directives":[],"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"id"},"arguments":[],"directives":[]},{"kind":"Field","name":{"kind":"Name","value":"isActive"},"arguments":[],"directives":[]},{"kind":"Field","name":{"kind":"Name","value":"cartSection"},"arguments":[],"directives":[],"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"id"},"arguments":[],"directives":[]},{"kind":"Field","name":{"kind":"Name","value":"name"},"arguments":[],"directives":[]},{"kind":"Field","name":{"kind":"Name","value":"__typename"}}]}},{"kind":"Field","name":{"kind":"Name","value":"description"},"arguments":[],"directives":[]},{"kind":"Field","name":{"kind":"Name","value":"imageUrl"},"arguments":[],"directives":[]},{"kind":"Field","name":{"kind":"Name","value":"shortDescription"},"arguments":[],"directives":[]},{"kind":"Field","name":{"kind":"Name","value":"name"},"arguments":[],"directives":[]},{"kind":"Field","name":{"kind":"Name","value":"__typename"}}]}},{"kind":"Field","name":{"kind":"Name","value":"__typename"}}]}},{"kind":"Field","name":{"kind":"Name","value":"__typename"}}]}}]}}],"loc":{"start":0,"end":372}}
Store error: the application attempted to write an object with no provided id but the store already contains an id of Restaurant:200 for this object. The selectionSet that was trying to be written is:
{"kind":"Field","name":{"kind":"Name","value":"vendor"},"arguments":[{"kind":"Argument","name":{"kind":"Name","value":"id"},"value":{"kind":"Variable","name":{"kind":"Name","value":"vendorID"}}}],"directives":[],"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","alias":{"kind":"Name","value":"services"},"name":{"kind":"Name","value":"products"},"arguments":[{"kind":"Argument","name":{"kind":"Name","value":"productTypes"},"value":{"kind":"EnumValue","value":"service"}}],"directives":[],"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"nodes"},"arguments":[],"directives":[],"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"id"},"arguments":[],"directives":[]},{"kind":"Field","name":{"kind":"Name","value":"isActive"},"arguments":[],"directives":[]},{"kind":"Field","name":{"kind":"Name","value":"cartSection"},"arguments":[],"directives":[],"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"id"},"arguments":[],"directives":[]},{"kind":"Field","name":{"kind":"Name","value":"name"},"arguments":[],"directives":[]},{"kind":"Field","name":{"kind":"Name","value":"__typename"}}]}},{"kind":"Field","name":{"kind":"Name","value":"description"},"arguments":[],"directives":[]},{"kind":"Field","name":{"kind":"Name","value":"imageUrl"},"arguments":[],"directives":[]},{"kind":"Field","name":{"kind":"Name","value":"shortDescription"},"arguments":[],"directives":[]},{"kind":"Field","name":{"kind":"Name","value":"name"},"arguments":[],"directives":[]},{"kind":"Field","name":{"kind":"Name","value":"__typename"}}]}},{"kind":"Field","name":{"kind":"Name","value":"__typename"}}]}},{"kind":"Field","name":{"kind":"Name","value":"__typename"}}]}}
    at new ApolloError (ApolloError.js:25)
    at QueryManager.js:276
    at QueryManager.js:638
    at Array.forEach (<anonymous>)
    at QueryManager.js:637
    at Map.forEach (<anonymous>)
    at QueryManager.push../node_modules/apollo-client/core/QueryManager.js.QueryManager.broadcastQueries (QueryManager.js:632)
    at QueryManager.js:226
    at ZoneDelegate.push../node_modules/zone.js/dist/zone.js.ZoneDelegate.invoke (zone.js:391)
    at Object.onInvoke (core.js:16135)

这是实现这一点的代码:

   this.restaurantID$.pipe(
      takeUntil(this._ngOnDestroy)
      )
      .subscribe((restaurantID) => {
        this.additionalServicesQuery$.next(this._apollo
          .watchQuery<AdditionalServices>({
            query: AdditionalServicesQuery,
            variables: { vendorID: restaurantID }
          }));
      });

    const loadAdditionalServicesData = this.additionalServicesQuery$
      .pipe(
        takeUntil(this._ngOnDestroy),
        filter((query) => !!query),
        switchMap((query) => query.valueChanges), // This is the switchMap that makes it happen
        takeUntil(this._ngOnDestroy),
        map((response) => response.data.vendor.services.nodes)
      );

有一个 SwitchMap 我评论了如果它被删除,错误就不会发生。我不明白发生了什么。

查询

export const AdditionalServicesQuery = gql`
  query AdditionalServices(
    $vendorID: ID!
  ) {
    vendor(
      id: $vendorID
    ) {
      services: products (productTypes: service) {
        nodes {
          id
          isActive
          cartSection {
            id
            name
          }
          description
          imageUrl
          shortDescription
          name
        }
      }
    }
  }
`;

更新:

添加了 ID 来查询,还是一样的问题

export const AdditionalServicesQuery = gql`
  query AdditionalServices(
    $vendorID: ID!
  ) {
    vendor(
      id: $vendorID
    ) {
      services: products (productTypes: service) {
        id
        nodes {
          id
          isActive
          cartSection {
            id
            name
          }
          description
          imageUrl
          shortDescription
          name
        }
      }
    }
  }
`;

【问题讨论】:

    标签: javascript angular typescript graphql apollo


    【解决方案1】:

    根据该错误,您需要将id 字段(或_id 字段,以存在者为准)添加到vendor 字段的选择集中。听起来您已经有另一个返回 Restaurant 类型的对象的查询,该查询包含 id 并且已正确规范化。除非包含 id,否则 Apollo 将无法合并两个查询中的单个 Restaurant 对象。

    export const AdditionalServicesQuery = gql`
      query AdditionalServices(
        $vendorID: ID!
      ) {
        vendor(
          id: $vendorID
        ) {
          id # <----------------------------------------- ADD ME
          services: products (productTypes: service) {
            nodes {
              id
              isActive
              cartSection {
                id
                name
              }
              description
              imageUrl
              shortDescription
              name
            }
          }
        }
      }
    `;
    

    【讨论】:

    • 哇。天呐!在服务器将这个 id 迁移进来后,我会检查它是否有效,如果有效则接受这个答案
    • 它不起作用...为新查询编辑了我的问题
    • Hmm.. 该错误表明有另一个查询正在生成 Restuarant:200 的缓存键。您知道正在运行的其他查询会返回带有该键的 Restaurant 对象吗?你在使用自定义的dataIdFromObject 函数吗?
    • 我确实有其他查询返回具有该 ID 的相同对象,但他们指定了一个...而不是使用 dataIdFromObject。我应该吗?
    • 好吧,你是对的,这就是问题所在。餐厅申请中缺少 ID,谢谢。
    猜你喜欢
    • 2017-11-08
    • 2018-10-14
    • 2019-02-28
    • 1970-01-01
    • 2018-08-27
    • 2016-07-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多