-
Notifications
You must be signed in to change notification settings - Fork 46
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
Best practice for exposing a RESTful collection through Falcor #215
Comments
On our side, to avoid adding some cache logic, we have added the object attributes to the Here is a sample in your case:
For the length route, we have a separate route in our API, so there is no need for optimization on this one on our side. Would be happy to have some feedback of other Falcor devs too. |
Thanks @ludovicthomas, that's really interesting! I think I prefer your approach - just so I'm clear, is your // Assuming we asked for allTodos[0]['name']
[
// $ref to todosById
{ path: ['allTodos', 0], value: $ref(['todosById', 100]) },
// values _under the todosById path_
{ path: ['todosById', 100, 'name'], value: $atom('get milk') }
] |
Sorry for the late reply. Yes that's exactly that, in fact we have helpers that we use on both routes Something like this for route
and something like this for the route
With the
|
I work with a large number of APIs exposing collections of items. Typically these accept an offset/limit pair, and return a complete list of items (rather than a list of links to each item), as well as the total number of items available. For example:
/todos?offset=10&limit=10
might return:Additionally
/todos/101
would return the same data for a single item:{ "id": 101, "name": "get bread", "completed": true }
.Exposing this API through Falcor routes should, if I'm following the docs correctly, look something like:
The problem is that a typical usage of these routes:
Would trigger (in this example) 5 API calls when just 1 would suffice -
/todos&offset=10&limit=10
has all the necessary data to fulfill this entire query.I'm currently using 2 different approaches to deal with this:
To share data between the 'list' and 'detail' routes (i.e.
allTodos
andtodosById
) I'm using a mutable cache object on the router. InallTodos
I store 'extra' data for use later:To fulfill the
length
value without making a separate request, I think I can just return that as an extra (un-requested) path value from theallTodos[{ranges:indices}]
route:And as long as
allTodos[{ranges:indices}]
appears beforeallTodos.length
in the route list, then theallTodos.length
route should not be invoked (but requestingallTodos.length
in isolation would still invoke the route and work properly).I'm looking for guidance or best practices on dealing with this scenario. Are my 2 workarounds reasonable or likely to cause problems later? Does anyone have a better way of handling this situation? Am I correct in my choice of Falcor routes to expose a collection of items?
The text was updated successfully, but these errors were encountered: