错误:网络错误:将结果写入存储查询时出错:

MCM*_*tan 9 javascript typescript apollo graphql angular

从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)
Run Code Online (Sandbox Code Playgroud)

这是实现这种情况的代码:

   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)
      );
Run Code Online (Sandbox Code Playgroud)

我评论了一个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
        }
      }
    }
  }
`;
Run Code Online (Sandbox Code Playgroud)

更新:

添加了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
        }
      }
    }
  }
`;
Run Code Online (Sandbox Code Playgroud)

Dan*_*den 12

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

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
        }
      }
    }
  }
`;
Run Code Online (Sandbox Code Playgroud)