简体   繁体   中英

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

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

If you decorate your query with @connection , then you can use the ConnectionHandler to easily remove records:

const query = graphql`query WidgetListQuery {
    widgets(first: 10) @connection(key: "WidgetList_widgets") {
        ...
    }
}`
optimisticUpdater(store) {
    const widgets = ConnectionHandler.getConnection(store.getRoot(), 'WidgetList_widgets');
    ConnectionHandler.deleteNode(widgets, deleted_widget.id)
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM