john john - 6 months ago 106
Scala Question

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

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 =
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?

Answer Source

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:

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download