Jarlakxen / drunk

A simple GraphQL client on top of Sangria, Akka HTTP and Circe.
Apache License 2.0
61 stars 21 forks source link

String interpolation is not supported for graphql / gql macro at the moment #5

Closed evbo closed 6 years ago

evbo commented 6 years ago

I am trying to use drunk for the client to make user selected requests, where vars will vary depending on what the user has selected.

Since String interpolation isn't supported yet, I tried following how variables are defined in the readme, but now I'm getting compilation error: not found: value fams.

Here's what I'm attempting to do. ``:

families = List("ABC", "XYZ")

val query =
    graphql"""
      query getPeople{
        getPeople(families: [$fams]) {
          name
        }
      }
    """
val vars = Map("fams" -> families.mkString("\"", "\",\"", "\"")
val result = client.query[List[People],Map[String,String]](query, vars)

If I skip variable definitions, and just hardcode the families input, I'm still getting an error about a decoder missing:

could not find implicit value for parameter dec: io.circe.Decoder[List[People]]
Jarlakxen commented 6 years ago

Hi @evbo !

I think you miss the var definition in the query sentence:

val query =
    graphql"""
      query getPeople($fams: String) {
        getPeople(families: [$fams]) {
          name
        }
      }
    """

also the last error could be that you miss to import circe, to have the json decoder for People and List. Try with:

  import io.circe._, io.circe.generic.auto._
evbo commented 6 years ago

Thanks, and I really appreciate you making this library, but since posting I've been under a tight deadline to get something working so I've implemented a fairly light weight Sangria client of my own. Just a bare bones simple akka post or get request with circe marshaling using only simple case classes to avoid complex encoding/decoding. Unless there's something else I haven't accounted for I may just stick with this approach.

thanks for getting back to me though. Love the title of this repo too, though it's a challenge to google :)

protometa commented 5 years ago

Rats. I'm having these exact same issues.

protometa commented 5 years ago

Unlike the examples, I had to escape $ in the query with $$, as my Scala compiler was interpreting $ as string interpolation. So something like

val query =
    graphql"""
      query getPeople($$fams: String) {
        getPeople(families: [$$fams]) {
          name
        }
      }
    """

worked for me.

Regarding the decoders, I did have to use io.circe.generic.auto._ as shown above instead of io.circe.generic.semiauto._ as shown in the README.

There was also some Akka version dependency incompatibility that I was able to fix by including "com.typesafe.akka" %% "akka-actor" % "2.5.19" as a dependency in my project.

kjhf commented 3 years ago

Hello from a future Google searcher -- I gave up with this and used QueryParser.parse(s""" ... ${InterpolationVariable} ... """).get instead. Hope this helps.