1

This question might be closely related to this GitHub issue.

I have a GraphQL server written in Rust using Juniper. This server needs to perform some HTTP requests in order to build and send back the data required by the clients.

Some fields may need up to ~15 HTTP requests, at most (essentially collections). To run theses requests sequentially might take time, and I was considering using Tokio + Futures to run them in parallel. It seems to be technically doable, but so far I have no clue on how to actually implement such a solution, and couldn't find any example using Juniper and Tokio, until I found the GitHub issue mentioned earlier...

Something like the following:

graphql_object(Whatever: MyContext |&self| {
    field parallel_requests(&executor) -> ??? {
        multiple_parallel_http_requests()
    }
}

Where ??? is FieldResult<Vec<AnyResource>>(?). And if so, how?

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366

1 Answers1

0

Juniper is not yet set up to handle parallel resolution of fields, and has no integration with any async frameworks like Tokio. Instead, you can do some of that yourself, under the covers. If you have an iterator of the requests you need to make, you can farm them out to a thread pool by using the Rayon crate, which exposes a function .par_iter() that can automatically parallelize the work.

I too am interested in moving Juniper onto an async model, but that work likely won't begin until after async/await lands in stable.

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
Marcus Griep
  • 8,156
  • 1
  • 23
  • 24