Relay: [Modern - rc.1 and rc.2] RelayRecordSourceMutator#create(): Cannot create a record with id

Created on 21 Apr 2017  路  10Comments  路  Source: facebook/relay

Getting follow error when using PaginationContainer -

Uncaught Error: RelayRecordSourceMutator#create(): Cannot create a record with idclient:Vmlld2VyLS0tezppZD0+InJvb3QifQ==:__Some_connection, this record already exists.

I am using react router 4 to render Query, and it works fine on initial render but when navigating it throws above error. Perhaps, it should merge in case the id exists?

Most helpful comment

I hit the same issue, it seems to be caused by the viewer handler when pagination container tries to create a record with an ID that already exists because it cannot find the existing one (that actually exists).

As a workaround I created a dumb viewer handler and I don't get crash anymore.

function update(store, payload) {
  const record = store.get(payload.dataID);
  if (!record) {
    return;
  }
  const serverViewer = record.getLinkedRecord(payload.fieldKey);
  record.setLinkedRecord(serverViewer, payload.handleKey);

  const root = store.getRoot();
  root.setLinkedRecord(serverViewer, payload.handleKey);
}

function handlerProvider(handle) {
  switch (handle) {
    // Augment (or remove from) this list:
    case 'connection':
      return ConnectionHandler;
    case 'viewer':
      return { update };
    default:
      throw new Error(`handlerProvider: No handler provided for ${handle}`);
  }
}

All 10 comments

I have the some problem here https://github.com/sibelius/ReactNavigationRelay

on rc.3

I hit the same issue, it seems to be caused by the viewer handler when pagination container tries to create a record with an ID that already exists because it cannot find the existing one (that actually exists).

As a workaround I created a dumb viewer handler and I don't get crash anymore.

function update(store, payload) {
  const record = store.get(payload.dataID);
  if (!record) {
    return;
  }
  const serverViewer = record.getLinkedRecord(payload.fieldKey);
  record.setLinkedRecord(serverViewer, payload.handleKey);

  const root = store.getRoot();
  root.setLinkedRecord(serverViewer, payload.handleKey);
}

function handlerProvider(handle) {
  switch (handle) {
    // Augment (or remove from) this list:
    case 'connection':
      return ConnectionHandler;
    case 'viewer':
      return { update };
    default:
      throw new Error(`handlerProvider: No handler provided for ${handle}`);
  }
}

Thanks @janicduplessis!

@josephsavona is this an erro with default ViewerHandler?

I'm having a similar problem with viewer getting incorrectly updated (it's losing data, but I haven't pinned down exactly why), @janicduplessis's solution above fixes the problem

I have the same issue. Where exactly do I use the code? Have tried in the mutation, but can't figure out how to fit it all together. Do you have a whole file example?

@janicduplessis where should I use this code you mention, did you create an additional component instead of paginationContainer?

@IkerArb You need to configure your environment with the handlerProvider. See https://facebook.github.io/relay/docs/relay-environment.html.

@apalm thank you so much! That did solve my issue!

Glad your issue was solved, if anyone wants to send a PR for the documentation it'd be great to add what might've helped someone like you!

Was this page helpful?
0 / 5 - 0 ratings