Alex Alex - 11 months ago 92
Scala Question

Schema from a macro generation

I tried to do generate example schema using the graphql macro from the example here: and got "Must provide one query type in schema." from AstSchemaMaterializer.scala on line 46. Seems like it did not like both Hello and Yellow queries defined in the schema. After removing one, it was able to parse the examples correctly. I also don't see a way to specify resolve function for any of the types. Has anyone tried to do that?


Answer Source

FYI, we discussed it in the gitter chat:

The documentation contained an error, but it is now fixed.

The buildFromAst method accepts the second argument which is a schema builder. It allows you to customeze any aspect of schema generation, which also includes the field resolvers. Here is a small example:

val ast =
    schema {
      query: Hello

    type Hello {
      bar: Bar

    type Bar {
      isColor: Boolean

val clientSchema: Schema[Any, Any] =
  Schema.buildFromAst(ast, new DefaultAstSchemaBuilder[Any] {
    override def resolveField(typeDefinition: TypeDefinition, definition: FieldDefinition): Context[Any, _] ⇒ Action[Any, _] =
      // your resolve logic goes here

More complex and complete example can be found in the tests. For example this one: