Schema from a macro generation

时间:2016-08-31 17:35:19

标签: scala graphql sangria

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!

1 个答案:

答案 0 :(得分:1)

仅供参考,我们在gitter聊天中讨论过它:

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

文档包含错误,但现在已修复。

buildFromAst方法接受第二个参数,即模式构建器。它允许您自定义模式生成的任何方面,其中还包括字段解析器。这是一个小例子:

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
  })

在测试中可以找到更复杂和完整的示例。例如,这一个:https://github.com/sangria-graphql/sangria/blob/e5a5d2c5ced3ce03c2e9437886be4683cf11ce6a/src/test/scala/sangria/schema/AstSchemaMaterializerSpec.scala#L901-L901