Get selected fields from the query

146 views
Skip to first unread message

Ros

unread,
Apr 26, 2018, 9:31:54 AM4/26/18
to sangria-graphql
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

Oleg Ilyenko

unread,
Apr 26, 2018, 4:00:23 PM4/26/18
to sangria-graphql
Hi,

sure there are several features that you can use, but I think "Projection" would fit the best:


Here is a an example of how you can use it (I probably should add a small example in the documentation as well):


You also might find a general section on the schema and query analysis interesting:


Cheers,
Oleg

Ros

unread,
Apr 27, 2018, 12:25:37 AM4/27/18
to sangria-graphql
Thanks for quick reply.

That is exactly what I needed. 
However this works for me:
resolve = Projector(1, (ctx, projections) => {
 ctx
.ctx.allProjections = projections
 ctx
.ctx.getContact(ctx arg ID)
})))


But this doesn't (if I don't provide the level)
resolve = Projector((ctx, projections) => {
 ctx
.ctx.allProjections = projections
 ctx
.ctx.getContact(ctx arg ID)
})))

I am using sangria 1.4.0 with Scala 2.11.12.
Am I doing something wrong?

Oleg Ilyenko

unread,
Apr 27, 2018, 6:37:19 AM4/27/18
to sangria-graphql
What kind of issue are you facing in the second scenario? Is it a compilation error or projected field list is empty or maybe something else, like unexpected exception?

Ros

unread,
Apr 27, 2018, 2:19:01 PM4/27/18
to sangria-graphql

Code was working fine. Only the highlight error of the IDE. It doesn't show up with this:

Projector(1, (ctx, projections)=> ...
 

Only during this:

Projector((ctx, projections) => ...


Message has been deleted

Oleg Ilyenko

unread,
Apr 27, 2018, 3:54:12 PM4/27/18
to sangria-graphql
I see. Sometimes more advanced IDE code analysis features show false positives, like type-aware highlighting in IntelliJ IDEA. But I haven't experienced this issue when I was working with the projections so far. Maybe explicit type arguments might help (for an object type or for a list of fields).
Reply all
Reply to author
Forward
0 new messages