Simple implementation of debounced fetching in GraphQL to allow merging of multiple rest / database requests into one. Although this example uses GraphQL, the debouncedFetch
/ fetchProgramPlaycount
implementations could probably be used in any context to achieve the same result.
This approach was first described by @leebyron at graphql/graphql-js#19 (comment)
For example this allows turning ten requests for playcounts from this GraphQL query into just one:
{
latestPrograms(first: 10) {
name,
playcount
}
}
Instead of having to do ten separate requests for
/playcounts?program_id=program1Id
/playcounts?program_id=program2Id
- ...
to resolve the query, the debounced version does just one:
/playcounts?program_id=program1Id,program2Id,...
Also, during load multiple user requests can be combined into single request to backend api.