graphql-relay-js icon indicating copy to clipboard operation
graphql-relay-js copied to clipboard

Better support for async connections

Open clayallsopp opened this issue 9 years ago • 3 comments
trafficstars

Hey, a pretty common situation is that getting the list of connected IDs is cheap (i.e. the list of the IDs of your friends), but resolving each ID is expensive.

Specifically for this kind of use case: https://github.com/clayallsopp/graphqlhub/pull/28/commits/07fec4c728cbf55b321bcdcdf3741c53944230d2#diff-6cd9d3d473d2f9b30b961ebfb3d3f048R50 - I have the entire list of IDs in memory, so if the query specifies first: 5 or provides a cursor, it would be great to reduce the set of IDs we're resolving before we resolve them. the current connectionFromPromisedArray doesnt help in this regard, as you have to resolve the entire list and then it trims after-the-fact

I think something like this API would be very helpful in general - I think it's possible? (assuming you have the entire list of IDs in memory and cursors are pure functions of offset and IDs)

let connectionIds = item.kids;
return promisedConnectionFromIdsArray(connectionIds, args, (trimmedIds) => {
  return Promise.all(trimmedIds.map(getItem));
});

clayallsopp avatar Apr 28 '16 19:04 clayallsopp

Can you use connectionFromPromisedArraySlice to avoid resolving the entire entire set?

wincent avatar Apr 28 '16 22:04 wincent

Not really, it doesn't easily work out-of-the-box - I believe you have to write the logic which calculates the correct sliceStart accounting for whatever args are passed, which ends up duplicating logic from within connectionFromArraySlice

I ended up doing this, takes advantage of the fact that cursors are soley based on the offset and not whatever the array contains: https://github.com/clayallsopp/graphqlhub/pull/29/files; would be happy to open a PR to merge it into graphql-relay-js if its a thing that might be useful for others, but totally happen to just close this too

clayallsopp avatar Apr 29 '16 02:04 clayallsopp

@clayallsopp have you figured out a clean way to deal with this?

I too have a similar case where it's easier for me to fetch exactly the data I need instead of fetching all the data and having the library trim the data after the fact. I think this is rather common (in my case, request a slice of the database collection instead of the entire thing)

pranaygp avatar Jan 07 '18 11:01 pranaygp