It can be quite common to perform the same subquery in multiple queries. It's therefore desirable to be able to abstract this subquery away and be able to include it into different queries for reuse (and reuse of the typeclass instances of the result class - which we may already have if we're mapping into an external type).
I imagine something along the lines of:
trait SubQuery[A] {
def subDocument: String
implicit def jsonDecoder: Decoder[A]
}
And a string interpolator to insert the subDocument in a Query.
An example of usage would be:
object TargetSubQuery extends SubQuery[Target] {
val subDocument = """
id
name
magnitudes {
band
etc
}
"""
}
And then somewhere else, in a Query:
val obsQueryDocument = gql"""
query {
observations {
targets {
$TargetSubQuery
}
}
}
"""
The biggest choke point I see with this is how to get grackle to process obsQueryDocument. We could trick it somehow. Otherwise we would have to process the interpolated string within the scalafix rule, which means evaluating it, which I don't know how to do (maybe we could imitate mdoc?). But I think that other than that, we can get away with generating a reference to an external type (Target in this case, obtained from the SubQuery) and resolving the interpolated string only at runtime.
It can be quite common to perform the same subquery in multiple queries. It's therefore desirable to be able to abstract this subquery away and be able to include it into different queries for reuse (and reuse of the typeclass instances of the result class - which we may already have if we're mapping into an external type).
I imagine something along the lines of:
And a string interpolator to insert the
subDocumentin aQuery.An example of usage would be:
And then somewhere else, in a
Query:The biggest choke point I see with this is how to get
grackleto processobsQueryDocument. We could trick it somehow. Otherwise we would have to process the interpolated string within the scalafix rule, which means evaluating it, which I don't know how to do (maybe we could imitatemdoc?). But I think that other than that, we can get away with generating a reference to an external type (Targetin this case, obtained from theSubQuery) and resolving the interpolated string only at runtime.