Lets suppose a entity Contact:
Contact(id: String, name: String, email: String: phone: String)
I would like to make a GraphQL query by id to get "email, name" only.
that would be like:
{"query": "{contact(id: 123){name, email}}"}
This will return name and email after execution and that is as expected.
But, internally from the resolver method to call Repository, it will select all the fields from the database.
I am looking for a solution (without using custom regex on query string), so that I can extract the fields from the above query and pass them as projection to the database query.
Note: I have extracted using the below code (using astFields in sangria context):
val fields = SangriaUtil.extractFields(ctx.astFields)
def extractFields(fields: Vector[sangria.ast.Field]): Seq[String] = {
fields.foldLeft(Seq.empty[String]){(ac, t) => ac ++ t.selections.foldLeft(Seq.empty[String]){(c,v) => c :+ {v match {
case f: Field => f.name
case f: FragmentSpread => f.name
}}}}
}
I want to have something more cleaner, Is there something supported by the library out of the box?
Thanks in advance