带有Sangria的GraphQL架构

sil*_*rry 5 json scala graphql sangria

我正在查看Sangria库,以便在Scala中编写GraphQL服务器.然而,奇怪的是,相同类型的系统必须实现两次:(1)作为GraphQL类型声明的一部分,(2)也在服务器端,作为Scala案例类,伴随ObjectType,InterfaceType等.瓦尔斯.

在Scala中对类型系统进行硬编码尤其令人讨厌,因为我的目的是能够使用任意形状的CRUD聚合,其中每个形状被定义为GraphQL类型集合.例如,假设Shape类型的实例包含GraphQL文档作为字段; 并且Entity类型的实例具有对其Shape的引用,并且还包含在该Shape中定义的形状的Json对象.

case class Shape(id: String, name: String, doc: sangria.ast.Document)
case class Entity(id: String, name: String, shape: Shape, content: JsValue)
Run Code Online (Sandbox Code Playgroud)

例如,如果形状文档是这样的:

type Person {
  firstName: String!
  lastName: String!
  age: Int
}
Run Code Online (Sandbox Code Playgroud)

那么实体中的Json内容可能是这样的:

{
  "firstName": "John",
  "lastName": "Smith",
  "age": 30
}
Run Code Online (Sandbox Code Playgroud)

(一个真实的例子当然也有嵌套类型等)

因此,我试图能够定义Entity类型的实例,其形状在相应的Shape中定义.我不想硬编码相应的sangria.schema.Schema,但想直接从形状文档中派生它.

是否有一种现成的方法可以从包含类型声明的GraphQL文档以编程方式生成GraphQL模式?

ten*_*shi 5

对于此类动态用例,sangria 提供了一种从 GraphQL IDL 构建模式的方法。这是您的操作方法(我稍微简化了您的示例,但是当所有这些数据来自像Shape和 之类的单独类时,也可以实现相同的操作Entity):

import sangria.ast._
import sangria.schema._
import sangria.macros._
import sangria.marshalling.sprayJson._
import sangria.execution.Executor

import scala.concurrent.ExecutionContext.Implicits.global
import spray.json._

val schemaAst =
  gql"""
    type Person {
      firstName: String!
      lastName: String!
      age: Int
    }

    type Query {
      people: [Person!]
    }
  """

val schema = Schema.buildFromAst(schemaAst, builder)

val query =
  gql"""
    {
      people {
        firstName
        age
      }
    }
  """

val data =
  """
    {
      "people": [{
        "firstName": "John",
        "lastName": "Smith",
        "age": 30
      }]
    }
  """.parseJson

val result = Executor.execute(schema, query, data)
Run Code Online (Sandbox Code Playgroud)

为了定义resolve函数应该如何生成,您需要创建一个自定义模式构建器,就像这样,并且只覆盖resolveField方法:

val builder =
  new DefaultAstSchemaBuilder[JsValue] {
    override def resolveField(typeDefinition: TypeDefinition, definition: FieldDefinition) =
      typeDefinition.name match {
        case "Query" ?
          c ? c.ctx.asJsObject.fields get c.field.name map fromJson
        case _ ?
          c ? fromJson(c.value.asInstanceOf[JsObject].fields(c.field.name))
      }

    def fromJson(v: JsValue) = v match {
      case JsArray(l) ? l
      case JsString(s) ? s
      case JsNumber(n) ? n.intValue()
      case other ? other
    }
  }
Run Code Online (Sandbox Code Playgroud)

执行此示例时,您将看到以下 JSON result

{
  "data": {
    "people": [{
      "firstName": "John",
      "age": 30
    }]
  }
}
Run Code Online (Sandbox Code Playgroud)

如果您想查看更复杂的示例,我建议您查看GraphohQL Toolbox "proxy"。这个项目更进一步,甚至添加了自定义指令来控制解析函数的生成。代码可以在这里找到:

https://github.com/OlegIlyenko/graphql-toolbox