嘿,所有。我正在使用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