ghostdogpr / caliban

Functional GraphQL library for Scala
https://ghostdogpr.github.io/caliban/
Apache License 2.0
945 stars 247 forks source link

Client: autogenerate view classes from query #574

Open benwaffle opened 4 years ago

benwaffle commented 4 years ago

It would be useful to write the graphql query as a string and generate the view classes

val query = gql"""
{
  character("Amos Burton") {
    name
    nicknames
    origin
  }
}
"""

// autogenerate this
case class CharacterView(name: String, nickname: List[String], origin: Origin)
case class QueryResponse(character: List[CharacterView])
viclovsky commented 3 years ago

Do you have any plan to implement the feature? Anyway, is there any workaround how to generate view classes?

ghostdogpr commented 3 years ago

I think that would require some macros, I have no knowledge about that (and not really willing to learn now since Scala 3 will make it obsolete :D), but open to contributions.

askmrsinh commented 3 years ago

@viclovsky @benwaffle I am working on something like this for a project (using macros). Right now I am able to generate case classes for simple queries but I hope to cover more complex selections in the near future. If you have any particular need/suggestion please feel free to share.

ghostdogpr commented 3 years ago

FYI it is now possible to generate view classes when running the code gen (it's slightly different from your example though: it generates view classes with all fields (not based on any query).

DGolubets commented 3 weeks ago

This would be a nice addition. Maybe rather than inline, the views could be stored in files. I like the way it's implemented in https://github.com/jhnnsrs/turms for Python.

I'll give an example why I need it. We have many GraphQL micro-services joined with Apollo Federation. In one of my services I need to access a subset of federated schema (top query is owned by one service, but some nested fields are resolved by another). Generating a client for the whole federated schema results in too much code and slow compilation.

So for now I just manually define my case classes for that query...