0

Let's say I have two GraphQL services, one that provides an inventory of all cars at a dealership, and another that lets me find the history of what a particular car sold for. My top level types would be:

type Vehicle {
    vin: String
}
type SaleHistory {
    vin: String
    salePrice: Decimal
    soldWhen: DateTime
}

I want to stitch those together (using HotChocolate), so that someone can go from a particular car to its sale history:

{
    vehicles: {
        vin,
        saleHistory: {
            salePrice,
            soldWhen
        }
    }
}

Which, if the VIN WAUAC48H55K008231 was sold in 2020 for $10,000 then in 2021 for $15,000, it would return:

{
  "data": {
    "vehicles": [
      {
        "vin": "WAUAC48H55K008231",
        "saleHistory": [
          {
            "soldWhen": "2020-01-01",
            "salePrice": "10000.00"
          },
          {
            "soldWhen": "2021-02-05",
            "salePrice": "15000.00"
          }
        ]
      }
    ]
  }
}

When I've set this up, I was able to extend the Vehicle type, and define a delegate type, which it can find on a path where it passes in the VIN and gets back all of the sales

extend type Vehicle {
    financing: [SaleHistory] @delegate(schema: "saleHistory", path: "vehicleSalesByVin(vin: $fields:vin)")
}

I noticed that when I have multiple vehicles that I want to look up the sale history for, it's sending all of the VINs to the GraphQL endpoint, but it's iterating over each of them, querying my database one at a time, and then aggregating the results together.

What I would like to do is configure the delegate so that it passes in a list of all of the VINs that it knows it's looking for, so I only have one database hit.

I've setup the GraphQL endpoint to accept a list of VINs, but I can't figure out how to declare the delegate to pass in a list of VINs, it seems to only make the association at a 1:1 level. I'm getting internal exceptions if I try something like this:

path: "vehicleSalesByVins(vins: [$fields:vin])"

I've double checked my vehicleSalesByVins, and when I invoke it directly, that is outside of the stitching schema, it does exactly what I want. But I can't figure out how to tell the delegate that it should pass in a list of VINs from the result set.

Thoughts?

0 Answers0