【问题标题】:Sangria: How to handle custom types桑格利亚汽酒:如何处理自定义类型
【发布时间】:2019-07-14 20:06:09
【问题描述】:

尝试与 SangriaSlick 合作。对他们俩来说都是新手。

我有一堆表格,它们共享一个公共字段列表。 Slick 对此的表示如下:

case class CommonFields(created_by: Int = 0, is_deleted: Boolean = false)

trait CommonModel {
  def commonFields: CommonFields
  def created_by = commonFields.created_by
  def is_deleted = commonFields.is_deleted
}

case class User(id: Int,
                name: String,
                commonFields: CommonFields = CommonFields()) extends CommonModel

光滑的桌子:

  abstract class CommonTable [Model <: CommonModel] (tag: Tag, tableName: String) extends Table[Model](tag, tableName) {
    def created_by = column[Int]("created_by")
    def is_deleted = column[Boolean]("is_deleted")
  }

  case class CommonColumns(created_by: Rep[Int], is_deleted: Rep[Boolean])

  implicit object CommonShape extends CaseClassShape(
    CommonColumns.tupled, CommonFields.tupled
  )

  class UsersTable(tag: Tag) extends CommonTable[User](tag, "USERS") {
    def id = column[Int]("ID", O.PrimaryKey, O.AutoInc)
    def name = column[String]("NAME")

    def * = (id,
      name,
      CommonColumns(created_by, is_deleted)) <> (User.tupled, User.unapply)
  }

  val Users = TableQuery[UsersTable]

Graphql 有问题:

 lazy val UserType: ObjectType[Unit, User] = deriveObjectType[Unit, User]()

当我尝试使用 derivedObjectType 宏创建 UserType 时,它​​会抱怨

找不到适合 .CommonFields 的 GraphQL 输出类型。如果您已经定义了它,请考虑使其隐含并确保它在范围内可用。

[错误] 惰性值 UserType: ObjectType[Unit, User] = derivedObjectType[Unit, User](

我如何告诉 Sangria/Graphql 如何处理这个嵌套的字段列表(来自 CommonFields)?

请帮忙。

【问题讨论】:

    标签: scala graphql slick sangria


    【解决方案1】:

    您正在为用户派生类型,但用户也有您尚未派生的 CommonFields。所以它无法找到 CommonFields 的类型信息。对两者都进行推导,并使推导对 CommonFields 隐含。

    implicit val CommonFieldsType: ObjectType[Unit, CommonFields] = deriveObjectType[Unit, CommonFields]     
    implicit val UserType: ObjectType[Unit, User] = deriveObjectType[Unit, User]
    

    【讨论】:

      猜你喜欢
      • 2017-10-23
      • 1970-01-01
      • 2019-08-22
      • 2019-03-25
      • 2016-07-03
      • 1970-01-01
      • 2014-03-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多