Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Apollo client is giving me an error of 'store already contains an id' - what does that mean?

In a react native project I am creating an object and then redirecting the screen to the newly created object's details page and I'm getting this error:

Possible Unhandled Promise Rejection (id: 0): Network error: Store error: the application attempted to write an object with no provided id but the store already contains an id of XYZ for this object.

Looking in the database I see that the item is properly created in the previous step. Navigating to the same screen and item through a list (not after a create and redirect) seems to work fine. Do I have to wait or somehow set some sort of timing for the apollo store to stay correct?

I'm using the standard apollo client @graphql binding/wrapping

gql:

 query getEvent($eventId: ID!) {
    Event(id:$eventId) {
      id
      headline
      photo
      location
      startTime
      creator {
        username
        photo
      }
    }
  }
`;

And here's a code snippet

@graphql(getEventGql,{
  options: ({route}) => {
    console.log('route params', route.params);
    return {
      variables: {
        eventId: route.params.eventId,
      }
    }
  },
})

@connect((state) => ({ user: state.user }))
export default class EventDetailScreen extends Component {
...
like image 523
MonkeyBonkey Avatar asked Jan 30 '17 19:01

MonkeyBonkey


People also ask

What is Apollo error?

Throwing errorsApollo Server throws errors of most built-in types automatically when applicable. For example, it throws a ValidationError whenever an incoming operation isn't valid against the server's schema. Your resolvers can also throw errors in situations where Apollo Server doesn't do so automatically.

How do you handle errors in Apollo GraphQL?

By default, Apollo Client throws away partial data and populates the error. graphQLErrors array of your useQuery call (or whichever hook you're using). You can instead use these partial results by defining an error policy for your operation. If the response includes GraphQL errors, they are returned on error.

How do I reset my Apollo cache?

Resetting the cache This method is asynchronous, because it also refetches any of your active queries. To reset the cache without refetching active queries, use client. clearStore() instead of client. resetStore() .

What is Apollo Client cache?

Overview. Apollo Client stores the results of your GraphQL queries in a local, normalized, in-memory cache. This enables Apollo Client to respond almost immediately to queries for already-cached data, without even sending a network request.


1 Answers

You have to add id also to the creator field:

query getEvent($eventId: ID!) {
    Event(id:$eventId) {
      id
      headline
      photo
      location
      startTime
      creator {
        id
        username
        photo
      }
    }
  }

In general, make sure to add id to all subselections of your queries.

like image 131
marktani Avatar answered Oct 13 '22 22:10

marktani