44

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 {
...
marktani
  • 6,520
  • 5
  • 33
  • 59
MonkeyBonkey
  • 40,521
  • 63
  • 217
  • 426

1 Answers1

78

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.

marktani
  • 6,520
  • 5
  • 33
  • 59
  • 6
    Had this error where there was no need for an id in a nested property but needed the id at the root level, so this answer was helpful. I would appreciate an explanation of why though. – ABCD.ca Apr 27 '17 at 18:08
  • 3
    @ABCD.ca I am not 100% sure, but it seems that some result caching is going on, so you have to provide the unique identifier for apollo client to retrieve it. Makes some sense, but I have not had this issue prior to the release of 1.0.0, so I have temporarily reverted to 0.10.1 – ViggoV May 30 '17 at 12:12
  • Also be careful include `__typename` or avoid it if you're testing with MockProvider. But in some cases you have to include `__typename` allowing it in the MockProvider – Javier Gutierrez Jun 07 '19 at 17:36