1

我有以下带有选项字段的案例类:

case class BusinessUserRow(id: String, firstName: Option[String], lastName: Option[String], email: Option[String])

我正在尝试为业务用户对象创建一个 inputType 对象

val BusinessUserInputType =
    deriveInputObjectType[BusinessUserRow](
      InputObjectTypeName("input"),
      InputObjectTypeDescription("A Business user")
    )

我想将此 BusinessInputObject 作为参数传递给 addBusinessUser 突变

  val businessUserInputArg = Argument("input", BusinessUserInputType)

val Mutation = ObjectType("Mutation", fields[RepoContext, Unit](
    Field("addBusinessUser", BusinessUserType,
      arguments = businessUserInputArg :: Nil,
      resolve = c ⇒ c.ctx.BusinessUserRepo.create(c.arg(businessUserInputArg)))))

但我收到以下编译错误:

Type dao.Tables.BusinessUserRow @@ sangria.marshalling.FromInput.InputObjectResult cannot be used as an input. Please consider defining an implicit instance of `FromInput` for it.
[error]   val businessUserInputArg = Argument("input", BusinessUserInputType)

但是 BusinessRow 中的所有字段都是标量值。我不明白是什么导致了这个问题。有什么我没有看到的吗?

4

2 回答 2

2

为了反序列化BusinessUserRow案例类中的输入,您需要提供一个FromInput[BusinessUserRow]类型类的实例。你可以在这里找到更多关于它的文档:

http://sangria-graphql.org/learn/#frominput-type-class

因此,例如,如果您使用的是 spray-json,那么您需要JsonFormat定义BusinessUserRow

于 2017-08-20T16:26:23.613 回答
0

谢谢!只需添加这一行就解决了我的问题:

implicit val businessUserFormat = Json.format[BusinessUserRow]
于 2017-08-20T20:57:29.730 回答