facebook / facebook/relay

[Request] Ability to enumerate objects by type from store for missing field handlers

Open
#4,912 2 comments 0 reactions 0 assignees View on GitHub
shared with relay team
Dominant language
Rust
Stars
19k
Forks
1.9k
PR merge metrics
No merged PRs in 30d

Description

[Missing field handlers](https://relay.dev/docs/guided-tour/reusing-cached-data/filling-in-missing-data/) provide a great way to normalize data fetched through different field paths.

One such use case supported today is a dropdown/table to select from a list of items (e.g. `viewer.tasks { id name}`), followed by a unit (or second page) which shows some more information about the selected item (e.g. `viewer.task(id) { id name ...TaskDescription }`). This can be supported by a handler which returns `argValues.id`, allowing the second unit/page to [partially render instantly](https://relay.dev/docs/guided-tour/reusing-cached-data/rendering-partially-cached-data/)

However, a similar use-case not supported could be a unit/page which fetches the task by another property, such as `viewer.task(name) { id name ... }`. While it would be possible to enumerate `store.getRoot().getLinkedRecord('viewer').getLinkedRecords('tasks')`, this only works when the initial dropdown fetched exactly `viewer.tasks`. In practice, it may have fetched `viewer.tasks(first: 15)`, or `viewer.tasks(completed: false)` (or `viewer.completed_tasks`). Regardless of the fields involved, the relay store will contain a list of `Task` objects keyed by their IDs. If there was a function `store.getAllByType('Task')`, we could enumerate them (albeit somewhat inefficiently, but for a small number of tasks this is fine; perhaps a more robust solution would allow a listener for any Task being updated to maintain this lookup map?) and find the task with the matching name.

I think it's fairly common that the id is not the only method to identify/lookup items (ex. the URL may often contain a friendlier name), so it'd be valuable to lookup data from the store in other ways for missing field handlers. From what I can tell, there is no function exposed to do this today.

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.