Skip to content

Reusable fragments #275

@rpiaggio

Description

@rpiaggio

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions