In my previous post I talked about how easy it is to implement a dynamically generated GraphQL Schema using
graphql-js as you can simply ignores types and do your job. I then made some comments about Sangria, perhaps the most stable and powerful GraphQL lib for Scala. Follow me in this exploration until I find a solution or a good soul can share it with us.
If you refer to the sangria-akka-http-example project, you’ll see the hand made schema. Something like:
ObjectType uses a
Droid (a case class) as its internal representation. In order to circunvent such way to define GraphQL Object Types, I tried many things using shapeless.
To be fair, I’m far from being proficient in shapeless. And the only solution I found so far was to use
Records. This is a half solution because even using it, I had to set up the Record by hand. Look:
There is a powerful macro here responsible for generating the type of our book. But I still couldn’t find a way to generate the
Book type at runtime. Bear the
resolve here is hard coded instead of bringing something from the database, for example. Enough for our purpose though.
We were able to replace a case class by a shapeless
Record and I think in few more tries we can replace everything by
HLists. In fact if you create a instance of
Book, you’ll see the type as a HList with tagged type parameters and -
not sure - some singleton types for the field labels.
In the end, the problem is not only replacing case classes for something more dynamic, but the Sangria
ObjectType are quite annoying to instantiate if you start moving things around and for example don’t use
fields function from
sangria.schema package object.
While the solution in NodeJs or clojure would be simply replace the thing by
Map[String, Any] in scala is not only bizarre but would led to a confusing code full of type casts and exception prone. Shapeless comes to the rescue and allows for elegant, abstract generic approach while keeping your code safe.
Ok, good results so far. I’m using this challenge to (re)warm my mind around Scala. My last and sole system in production written in scala was released almost two years ago and it is wise to refresh knowledge from time to time.
My next steps includes getting some inspiration from Underscore.io using Using shapeless HLists with Slick 3 blog post as basis and studying more about shapeless using the Underscore.io book and some E.near blog posts.
I wouldn’t be surprised if a experienced shapeless professional drop a comment and say: hey newbie, just do this and that :). Nonetheless, I’m enjoying the path to solve the case. If you know how to solve it or need more context on what I’m trying to do, send me a email or leave a comment.