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

MonkeyBonkey picture MonkeyBonkey · Jan 30, 2017 · Viewed 9.2k times · Source

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 {
...

Answer

marktani picture marktani · Feb 1, 2017

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.