facebook / facebook/relay

Newly created edge has cursor 'null'

Open
#4,026 0 comments 1 reaction 0 assignees View on GitHub
Dominant language
Rust
Stars
19k
Forks
1.9k
PR merge metrics
No merged PRs in 30d

Description

## Reporting issues with Relay
I am trying to change the order of the `edge` in `ConnectionHandler`.
When I create a new edge with `createEdge`, then the cursor is `null` so I can't find the cursor position on newly created edge.

How do you think we can obtain such behavior? I've captured the record to help understand.

https://user-images.githubusercontent.com/27461460/180636975-f1e771ad-9793-4a51-bf0f-4f7ef9156310.mov

### Sourcecode for calling query
```tsx
commitChangeCollectionsOrder({
variables: {
collectionIds,
},
optimisticUpdater: (store) => {
changeCollectionsOrderUpdater(
store,
collectionIds,
from,
to,
cursors,
);
},
updater: (store) => {
changeCollectionsOrderUpdater(
store,
collectionIds,
from,
to,
cursors,
);
},
});
```

### Sourcecode for the function
```tsx
export function changeCollectionsOrderUpdater(
store: RecordSourceSelectorProxy,
videoIds: string[],
fromIndex: number,
toIndex: number,
cursors: string[] | undefined,
): void {
if (cursors) {
const root = store.getRoot();
const collectionIdTo = videoIds[toIndex];
const collectionToProxy = store.get(collectionIdTo);

const collectionsConnection = ConnectionHandler.getConnection(
root,
'Collection_collections',
{searchText: ''},
);

if (collectionsConnection && collectionToProxy) {
const newCollectionEdge = ConnectionHandler.createEdge(
store,
collectionsConnection,
collectionToProxy,
'Collection',
);

console.log(`fromIndex: ${fromIndex} => toIndex: ${toIndex}`);
console.log('prev cursors', cursors);
console.log('cursor', cursors[toIndex]);

ConnectionHandler.deleteNode(
collectionsConnection,
collectionToProxy.getDataID(),
);

collectionToProxy?.setValue('cursor', nanoid());

if (fromIndex < toIndex) {
ConnectionHandler.insertEdgeAfter(
collectionsConnection,
newCollectionEdge,
cursors[toIndex],
);
} else {
ConnectionHandler.insertEdgeBefore(
collectionsConnection,
newCollectionEdge,
cursors[toIndex],
);
}

console.log('after cursors', cursors);
}
}
}
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.