Calculated/computed Fields resolver calling order
See original GitHub issueI’m working through providing a graphql source to an energy monitoring application. Total load for a premises is essentially the sum of all of the measured loads on each circuit.
The following is a trivial piece of the work where there are two measured load fields and one total_load field
class Heating(graphene.ObjectType):
total_load = graphene.Float()
water_heater = graphene.Field(lambda: SensorReading,
resolver=get_measurement,
args={'id': graphene.String('Water Heater')})
space_heater = graphene.Field(lambda: SensorReading,
resolver=get_measurement,
args={'id': graphene.String('Space Heater')})
I have tried to create a simple resolver to compute the total_load from water_heater and space_heater values but this total_load resolver always gets called first so both water_heater and space_heater are None, meaning I can’t do the calculation.
Both water_heater and space_heater use a DataLoader, part of the get_measurement resolver. This is important as the time series database (InfluxDB) has a tendancy to return quite large amounts of data the I need to resolve from a cache and which need bulk loading.
I have thought about using a promise or middleware to get the total_load but wanted to get a steer as to the right way to resolve this type of timing related issue and more generally how to approach calculated/computed values using both GraphQL and Graphene as much seaching hasn’t given me much luck so far.
Issue Analytics
- State:
- Created 5 years ago
- Comments:6 (4 by maintainers)
Top GitHub Comments
@j-robin-hunter have you tried moving the data fetching to the level above the
Heating
type so that you can do it at the same time? For example:@rarenatoe in that case I would recommend abstracting the day calculation into a seperate function that you either cache or compute it at the level above the object type that needs the data.