-
Notifications
You must be signed in to change notification settings - Fork 1.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Request] Ability to enumerate objects by type from store for missing field handlers #4912
Comments
I don't think there's any way for Relay to support this without either maintaining an internal index of cache records by id, or doing a full scan of all records checking their typename. I don't think it makes sense for Relay to take on the runtime/memory overhead of maintaining such an index for a smaller use case like this. I suspect our best bet here is to enable you to implement the full table scan yourself by providing access to the store. I don't think we pass the missing field handler access to the store today. Would it be possible for you to construct your missing field handler such that it closes over your environment/store such that you could manually traverse over all records in this case? Aside: One avenue that might be worth exploring would be finding a way for Relay to explicitly support some of the directives defined in the Composite Schema spec. For example But that's a larger project. |
Thanks a ton for the context here, I think I'm able to get a proof of concept working based on this. In particular, defining the field handler in a closure with the store helps, and from there I realized I could actually subclass the RecordSource to maintain the mapping by typename (traversing the entire store could still lead to too much over-scanning). I could probably go further to actually index by my "name" field, but even enumerating by typename should be fine for my use-case. Here's a quick little example record source which seems to provide the tracking fairly cheaply:
|
Missing field handlers 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 returnsargValues.id
, allowing the second unit/page to partially render instantlyHowever, 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 enumeratestore.getRoot().getLinkedRecord('viewer').getLinkedRecords('tasks')
, this only works when the initial dropdown fetched exactlyviewer.tasks
. In practice, it may have fetchedviewer.tasks(first: 15)
, orviewer.tasks(completed: false)
(orviewer.completed_tasks
). Regardless of the fields involved, the relay store will contain a list ofTask
objects keyed by their IDs. If there was a functionstore.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.
The text was updated successfully, but these errors were encountered: