I'm looking at the Sangria library for coding a GraphQL server in Scala. It feels odd, however, that the same type system must be implemented twice: (1) as part of the GraphQL type declarations, and (2) also at the server side, as Scala case classes, with accompanying ObjectType, InterfaceType, etc. vals.
Hardcoding the type system in Scala is especially irksome, since my purpose is to be able to CRUD aggregates of arbitrary shape, where each shape is defined as a GraphQL collection of types. For example, say an instance of type Shape contains a GraphQL document as a field; and an instance of type Entity has a reference to its Shape and also contains a Json object of the shape defined in that Shape.
case class Shape(id: String, name: String, doc: sangria.ast.Document)
case class Entity(id: String, name: String, shape: Shape, content: JsValue)
For example, if the shape document is something like this:
type Person {
firstName: String!
lastName: String!
age: Int
}
then the Json content in the entity could be something like this:
{
"firstName": "John",
"lastName": "Smith",
"age": 30
}
(A real example would, of course, also have nested types, etc.)
Thus, I seek to be able to define instances of type Entity whose shape is defined in their corresponding Shape. I do NOT want to hardcode the corresponding sangria.schema.Schema but want to derive it directly from the shape document.
Is there a ready way to generate a GraphQL schema programmatically from a GraphQL document containing type declarations?