0

I have an entity (Customer) that needs to pull data from multiple sources. The schema looks roughly like this:

{
   id: string
   name: string
   address: string
   contact: string
   status: string
}

The id, name and address come from an EF datacontext. The contact and status fields come from a single REST endpoint, and looks like this:

GET /url/customer?id=1234
{
  id: '1234'
  contact: 'joe@bloggington.com'
  status: 'ACTIVE'
}

If I put both contact and status into a single field/object (i.e. ContactStatus), then it would be a simple case of creating an extension for Customer. But these fields are not related, and should be regarded as different top-level fields.

Is there a way to ensure that the REST endpoint is called only once, when fetching all values? Essentially resolving both fields when fetching one or the other maybe?

Hot Chocolate v12.15.0, net6.0

mortware
  • 1,910
  • 1
  • 20
  • 35

1 Answers1

0

Yes you can use the batching api to do this

Create a DataLoader that loads the data from the rest endpoint. This way you can also optimize the fetches from the rest endpoint (if the endpoint supports somthing like /url/customer?ids=1234,2345,5930)

e.g. class YourDataloader extends BatchDataLoader<int, AdditionalCustomerData>

Then you can just do

[ExtendObjectType<Customer>]
public class CustomerExtensions 
{

   public Task<string> GetContactAsync(
      [Parent]Customer customer, 
      YourDataloader dataloader) 
   {
     var result = await dataloader.LoadAsync(customer.Id);
     return result.Contact;
   }

    public Task<Status> GetStatusAsync(
      [Parent]Customer customer, 
      YourDataloader dataloader) 
   {
     var result = await dataloader.LoadAsync(customer.Id);
     return result.Status;
   }
}
Pascal Senn
  • 1,712
  • 11
  • 20