graphqlsubgraph

GraphQL joining subgraphs


Given a subgraph that contains order information including a product id for each product that is in the order, and a separate subgraph that contains a list of images and their appropriate product ids. These are served by a supergraph.

The order subgraph contains a list of products for a given order and will have a list of product ids.

I need to return an order and it's related product images.

This is likely to cause a huge number of calls because for each product in an order it will hit the images subgraph to find the appropriate matching image. So if an order contains 10 products, it will hit the images subgraph 10 times to fetch the 10 matching images.

In an attempt to resolve this we have tailored the images subgraph so you can specify X product ids in a single delimited query: 12,123,1234,12345 etc.

Is there a way to make this performant? I've looked through the docs but am unclear how we can avoid such a large number of queries.

My idea was to pull a given order from the subgraph, and then using javascript pull all the product ids out of that response and then hit the images subgraph using the (above mentioned query) that allows for a query based off X delimited product ids to try and make it just one query. But this feels like an antipattern, as I'd likely need to invoke axios or something to hit the other subgraph.

I'm not sure how this is supposed to work when using subgraph, or if I'm going down the wrong route entirely.

Any help would be much appreciated.

Thank you


Solution

  • The images could be retrieved in single query in which you specify the product ids. Of course, the service for the images must be capable of resolving by product id then and provide a imagesForProductIds endpoint (which returns for each product id the proper image).

    Once you've implemented this, a dataloaders can be used in the GraphQL gateway, transforming the incoming request by bundling the sub-requests (e.g. all sub-requests for images at same point in the request graph) into a single one. The dataloader then sends this single request to imagesForProductIds endpoint. The result (product id to image) can be used in the dataloader to assign elements of the result set back to the single incoming GraphQL queries.