Repository navigation
graph, store, graphql: Add entityCount field to subgraph entities - #602
Conversation
| } | ||
|
|
||
| /// Compute special fields that are not stored such as as `entityCount`. | ||
| fn add_computed_fields( |
There was a problem hiding this comment.
Overall, this is great, but I would do this part differently. Adding computed fields like this will mean that we'll always count entities for all subgraphs, even if entityCount is not part of the query.
What I would do instead is resolve fields like this lazily. Here's what I'd recommend:
- Change the signature of
Resolver::resolve_scalar_valueso that it takes the object type and the field name:fn resolve_scalar_value( &self, object_type: &s::ObjectType, field_name: &q::Name, scalar_type: &s::ScalarType, value: Option<&q::Value>, ) -> q::Value {
- Implement
resolve_scalar_valueinStoreResolver. Something like this should do:fn resolve_scalar_value( &self, object_type: &s::ObjectType, field_name: &q::Name, scalar_type: &s::ScalarType, value: Option<&q::Value>, ) -> q::Value { match field_name.as_str() { "entityCount" => { ... parse subgraph ID, if possible, fall back to value ... ... obtain entity count ... }, _ => value }.and_then(|value| value.coerce(scalar_type)).unwrap_or(q::Value::Null) }
There was a problem hiding this comment.
But I'm not completely convinced of my approach because it makes adding support for GraphQL interfaces harder (which don't have ObjectType). Perhaps let's go with your approach first.
There was a problem hiding this comment.
Overall, this is great, but I would do this part differently. Adding computed fields like this will mean that we'll always count entities for all subgraphs
I hadn't thought about this. I'll follow your recommendation then and keep things as they are for now, we can revisit the general issue of overfetching things later.
Resolves #583 (the url should be tracked in a different issue).
This adds the
entityCountfield. Instead of storing it with the data and incrementing on every entity insert, which would be error-prone, this introduces the concept of a "calculated field", which is a field of an entity that is not in the store but rather calculated on the fly. We can probably also use this for__typename.