We have been using relay in production for quite a while, but as we add a new edge to a connection, we always used to simply prepend or append it.
We just tried to order a connection alphabetically and therefore use the third argument in ConnectionHandler.insertEdgeAfter, i.e. cursor. It works for the first mutation, and then the next insertions happen mostly in wrong places.
A little digging in the code showed that the cursors generated with graphql-relay are merely a representation of the item's index in the array, therefore cursors are messed up after the first insertion in the middle (two edges have the same cursor and all the next have wrong cursors).
I only see two solutions :
id instead of edge cursorid)Any insights on this would be much appreciated !
Additional solution / idea I just thought of :
I also found this old comment in #293 which suggests what I implemented in the first place but does not account for subsequent mutations.
I think this should be resolved server side. The real problem, which I guess is that cursors are not stable, is there.
I think you can get the connection, iterate and decide where to put the new edge
@sibelius as a quick workaround I returned the id of the previous edge along with the new one and did what you suggest by copy-pasting insertEdgeAfter and customizing it to insert the newly created edge at the right place and it works fine.
I am still wondering if that is the right thing to do. If it is, then it defeats the purpose of insertEdgeAfter using a cursor. If it is not, then it means I should use ids as cursor ?...
The insertEdgeAfter/Before methods are just helpers for common tasks. Two thoughts:
Roughly:
const connection = getConnection(...);
const yourNewEdge = ...;
const prevEdges = connection.getLinkedRecords(‘edges’);
const nextEdges =[...prevEdges, yourNewEdge];
sortAndReorderEdgesArbitrsrily(nextEdges); // mutate in place, no reason to copy again
connection.setLinkedRecords(nextEdges, ‘edges’);
@josephsavona thanks a lot for the answer ! It is almost what I do, only difference is I let the back end do the sorting and send the previous node id.
Most helpful comment
The insertEdgeAfter/Before methods are just helpers for common tasks. Two thoughts:
Roughly: