Sangria-graphql: error when passing in derivedInputObjectType as an mutation argument

故事扮演 提交于 2020-05-31 06:33:07

问题


I have the following case class with option fields:

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

I am trying to create an inputType Object for Business User Object

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

and I want to pass this BusinessInputObject as an argument to a addBusinessUser mutation

  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)))))

But I get the following compilation error:

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)

But All fields in BusinessRow are scalar values. I don't understand what is causing the issue.Is there something I am not seeing?


回答1:


In order to deserialize the input in the BusinessUserRow case class, you need to provide an instance of FromInput[BusinessUserRow] type class. You can find more docs on it here:

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

So if you are, for instance, using spray-json, then you need to define JsonFormat for BusinessUserRow




回答2:


thanks! just adding this line solved my problem:

implicit val businessUserFormat = Json.format[BusinessUserRow]


来源:https://stackoverflow.com/questions/45784283/sangria-graphql-error-when-passing-in-derivedinputobjecttype-as-an-mutation-arg

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!