嘿,所有。我正在使用sangria-graphql,并且一切工作正常...除了内联片段。我在架构以下几种类型:sangria-graphql:内联碎片未解决
interface Entity {
oid: ID!
}
type Dataset implements Entity {
oid: ID!
name: String
... (other fields)
}
... (other types implementing Entity)
type Issue {
entity: Entity!
... (other fields)
}
type Query {
validate(many arguments): [Issue!]
... (other queries)
}
我发送一个查询是这样的:
{
validate(many arguments) {
entity {
oid
... on Dataset {
name
}
}
}
即使返回的OID是一个DataSet实例的,没有名字则返回它。就好像解析器不知道这是Dataset的一个实例,并仅将它作为实体的实例对待。
一些实现细节。该架构使用从GraphQL文档Schema.buildFromAst
方法构建,用resolveField
方法实现:
import sangria.schema._
import sangria.ast.Document
import play.api.libs.json._
// document is an instance of Document
lazy val schema: Schema[Ctx, Any] =
Schema.buildFromAst(document, new DefaultAstSchemaBuilder[Ctx] {
override def resolveField(typeDefinition: TypeDefinition,
fieldDefinition: FieldDefinition) =
typeDefinition.name match {
case "Mutation" => context =>
fieldDefinition.name match {
... // cases for specific mutations
}
case "Query" => context =>
fieldDefinition.name match {
case "validate" =>
... // implementation that returns a Seq[JsValue],
// where the Json values are serializations of Issue
... // cases for other queries
}
case _ => context =>
... // resolve the sub-selection fields as described below
}
}
子选择字段被解析如下:
- 如果
context.value
是JsObject
,那么它的Json字段的名称为context.field.name
; - 如果
context.value
是JsString
,则它被解释为一个实体的OID,实体在商店抬头,使用由Ctx
上下文中提供的手柄;该实体被检索为JsObject
,并且其名称为context.field.name
的Json字段被采用。
正如我所提到的,问题在于内联片段不受尊重。也许,我错过了一些东西。也许,不仅仅是resolveField
,还有其他的东西也需要适当的实施。也许,我的实施resolveField
出了问题。
你会建议什么?在你看来,问题在哪里?你会推荐我怎样解决这个问题?
谢谢!看起来像我正在寻找的方法。 – silverberry