Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error: Network error: Error writing result to store for query:

Getting this error from 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 is the code that makes this happen:

   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)
      );

There is a SwitchMap that I commented if that is removed, the error does not happen. I can't understand what is going on.

query:

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
        }
      }
    }
  }
`;

Update:

Added ID to query, still, same issue

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
        }
      }
    }
  }
`;
like image 433
MCMatan Avatar asked Mar 05 '19 17:03

MCMatan


1 Answers

According to that error, you need to add an id field (or _id field, whichever exists) to the selection set for the vendor field. Sounds like you already have another query that returns objects of the type Restaurant, that query included the id and was normalized properly. Apollo won't be able to combine the individual Restaurant objects from both queries unless an id is included.

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
        }
      }
    }
  }
`;
like image 66
Daniel Rearden Avatar answered Nov 05 '22 17:11

Daniel Rearden