1

I'm using Apollo React Native client working with a query for which my request body has become too large to use (it's being rejected by our CDN for a request-too-large rule). So, I'm hoping to split/chunk this request into smaller requests and particularly curious if it's possible to do parallelized.

I think this is better illustrated with an example, so we can imagine I'm building a WhatsApp challenger -- WhoseApp -- for which we want users to be able to see who of their contacts have a WhoseApp account upon signup.

For our implementation, we'll take all of the phone numbers stored on our user's device and send them to our GraphQL query GetPhoneNumberAccountStatus which accepts an array of phone numbers and which returns an Account for each number associated to an account (and nothing for those that are not).

If we send the contacts as one request, we'll have a request body that looks something like this:

[
 "+15558675309",
 "+15558675308",
 "+15558675307"
 "+15558675306"
 ...
 // 500+ numbers for some users 
]

What's the correct way to split this request into multiple?

I'm curious of both:

  1. What's the 'optimal' way to approach this using a sequential approach (e.g., send one group, wait for response, send next group), or
  2. Is there a way to do this parallelized (e.g., send all groups at beginning and then receive responses as they arrive)?

I initially figured it might be possible to use useLazyQuery and send tranches of ~50 numbers at a time, firing each group and then awaiting the responses but this GitHub thread for the library makes it clear that that's not the correct approach.

1 Answers1

0

I think it's readable

const promises = [];
const chunkSize = 50;

for (let i = 0; i <= contacts.length; i += chunkSize) {
    const promise = apollo.query({...dataHere});
    promises.push(promise);
}

await Promise.all(promises);

Ahmed I. Elsayed
  • 2,013
  • 2
  • 17
  • 30