Alex Alex - 3 months ago 21
Scala Question

Schema from a macro generation

I tried to do generate example schema using the graphql macro from the example here: http://sangria-graphql.org/learn/#based-on-idl-definitions 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?

Thanks!

Answer

FYI, we discussed it in the gitter chat:

https://gitter.im/sangria-graphql/sangria?at=57c70ec8ff952280079f484c

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 =
  graphql"""
    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: https://github.com/sangria-graphql/sangria/blob/e5a5d2c5ced3ce03c2e9437886be4683cf11ce6a/src/test/scala/sangria/schema/AstSchemaMaterializerSpec.scala#L901-L901

Comments