Can I get requested fields in resolve ?
I want to use includes, ActiveRecord::QueryMethods selectively.
Found a way:
resolve -> (_, _, ctx) {
ctx.selection.scoped_children.values[0].keys
}
Maybe it's different for your query.
I'm not sure if it is long supported, it used GraphQL::InternalRepresentation::Node#scoped_children.
You can access child selections with
ctx.irep_node.typed_children
It returns Hash<ObjectType => Hash<String => InternalRepresentation::Node> where:
ObjectType is the runtime type of the return values (for fields that return union or interface types, there may be _multiple_ object types as keys)String is the name in the _result_ (so, alias _or_ field name)InternalRepresentation::Node is the representation of the child selection. It has .definition to access the GraphQL::Field and .definition_name to access the name of that field. Sorry the API is a bit ... all of the place. I haven't solidified it much because I haven't _had_ to. If you want to share your use case, I'd be happy to consider some improvements! Hope that helps.
Thank you !
For minimizing the number of queries in my app, I usually use includes (ActiveRecord::QueryMethods) which pre-fetchs included rows associated.
I'll just check requested fields and if the associated rows in another table exists, then add it to includes methods.
But if don't need associated rows, I don't have to include that.
Great!
Is there a better API now or is this still the only way to check it?
no better api yet!
Most helpful comment
Found a way:
Maybe it's different for your query.
I'm not sure if it is long supported, it used
GraphQL::InternalRepresentation::Node#scoped_children.