Most efficient way to 'chunk' queries with too-large requests?

I’ve been reading a bit around pagination, batching, paraellel queries, breaking up large queries into smaller, etc., but I haven’t been able to find any resources on the optimal way to call the same query multiple times with different arguments.

As an example, imagine we are creating a ReactNative WhatsApp competitor, WhoseApp, and would like to show a user if any of their contacts are already using WhoseApp to send messages. In our implementation, we want to send an array of phone numbers to our server which will then return boolean hasWhoseAppAccount and then our user will be able to see who of their friends already have accounts.

The problem is that, for some users, our request body ends up being an array of phone numbers of length 1000+ which ends up causing issues with our CDN.

If we opt to chunk our request into smaller groups, of say 50, what is the most efficient way to do so? I had originally thought to use multiple calls to useLazyQuery, but I saw from this thread that that seems to be a completely incorrect approach.

Thanks for the help :grinning:!

I have a very similar issue for which I am trying to come up with a viable solution. Currently, I have a query that just takes an array (List) of ids (phone numbers in your case). Something like this:

query usersByPhoneNumber($phoneNumbers: [String!]!) {
  usersByPhoneNumber(phoneNumbers: $phoneNumbers) {
    name
    phoneNumber
  }
}

Then I just batch up the queries into groups of phoneNumbers. Easy enough and that’s all great, however. I’m struggling with.

  • I want to execute these queries in parallel as there could be thousands of phoneNumbers.
  • If I already have the desired user data for a phone number in the cache (or it’s already in flight) I don’t want to query for it.
  • If I only have incomplete data in the cache, (user.name but not user.phoneNumber) I want the query for that phoneNumber to proceed.
  • I need to handle failures and possibly issue a refetch.
  • I want to merge all the results from multiple of these queries. Something I’ve already done using a field policy.
  • The hooks useQuery and useLazyQuery do not seem to be appropriate here.

I’ve been considering just using the ApolloClient and watchQuery as well as creating my own useBulkIdQuery hook. But, after looking at the source code for some of the Apollo hooks, that feels a bit intimidating. Right now that is the path I’m currently on; trying to understand the internals of Apollo. I would also appreciate any direction on idea.

1 Like

Right now I’ve had some success doing it sequentially in batches. I’m using useLazyQuery and just maintaining two pointers for a start index and end index and incrementing across my array of phone numbers and firing an onCompleted method that re-runs the query for batch_n when batch_n-1 has completed.

Haven’t fully explored a parallelized solution but found the above pretty performant