question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Calculated/computed Fields resolver calling order

See original GitHub issue

I’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:closed
  • Created 5 years ago
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
jkimbocommented, Apr 29, 2018

@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:

class Query(graphene.ObjectType):
	heating = graphene.Field(Heating)

	def resolve_heating(_, info):
		water_heater = get_water_heater_load()
		space_heater = get_space_heater_load()
		total = water_heater + space_heater
		return {
			'water_heater': water_heater,
			'space_heater': space_heater,
			'total_load': total,
		}
0reactions
jkimbocommented, Jul 3, 2020

@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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Field middleware - Hot Chocolate - ChilliCream
Field middleware is composable, so you can specify multiple middleware and they will be executed in order. The field resolver is always the ......
Read more >
Examples — django-computedfields 0.2.1 documentation
The auto resolver will take care, that the computed fields are calculated in the correct order ( MRO ). In the example above...
Read more >
Advanced topics on federated entities - Apollo GraphQL Docs
You can define fields of an entity that are computed based on the values of other entity fields that are resolved by a...
Read more >
"Derived" field resolver for GraphQL using Hot Chocolate and ...
Although I don't use ResolveWith myself often, I'm pretty sure you'd have to annotate the Employee using the ParentAttribute :
Read more >
Adding calculated fields - Amazon QuickSight
Aggregate functions (you can only add these to an analysis). Fields that contain data. Other calculated fields. You can add calculated fields to...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found