reactjsgraphqlrelayjsrelayrelaymodern

Relay Modern: delete record in optimisticUpdater


In my app I have a mutation where I want to use the optimisticUpdater to update the view before the server response. For that I need to remove some records from a list into the relay store, like that:

optimisticUpdater: storeProxy => {
  storeProxy.delete(recordDataID)
}

The problem is that relay don't remove the record, but it transforms the record in null. This can be annoying because I have to filter the list every time I use it in my app.

Some one know how can I remove the record ? thx


Solution

  • You have to remove your records from the list

    optimisticUpdater: (store) => {
        const listOfRecords = store.getRoot().getLinkedRecords('list')
        const newList = listOfRecords.filter(record => record.getDataID() !== recordDataID)
        store.getRoot().setLinkedRecords(newList, 'list')
    }
    

    In this example I assume your list is placed at the root of your graph