sangria:如何处理自定义类型

p4rjhz4m  于 2021-07-14  发布在  Java
关注(0)|答案(1)|浏览(397)

试着和桑格里亚和斯里克一起工作。他们两个都是新手。
我有一堆表,它们共享一个公共字段列表。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

光滑的table:

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输出类型。如果您已经定义了它,请考虑将其隐式化,并确保它在范围中可用。
[错误]lazy val usertype:objecttype[unit,user]=deriveobjecttype[unit,user](
如何告诉sangria/graphql如何处理这个嵌套字段列表(来自commonfields)?
请帮忙。

r7knjye2

r7knjye21#

您正在为user派生类型,但user也有尚未派生的公共字段。所以它无法找到公共字段的类型信息。对公共字段进行二者派生并使派生隐式化。

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

相关问题