sangria-graphql / sangria

Scala GraphQL implementation
https://sangria-graphql.github.io
Apache License 2.0
1.96k stars 222 forks source link

DeriveInputObjectType for higher-kinded types #279

Open fokot opened 7 years ago

fokot commented 7 years ago

I would like to have this code work

case class User[F[_]] (
  id: F[Int],
  name: F[String]
)

object User {
  type UserCursor = User[Option]
  type UserFilter = User[Filter]

  implicit val decoderCursor: Decoder[UserCursor] = deriveDecoder
  implicit val gTypeCursor = deriveInputObjectType[UserCursor](InputObjectTypeName("UserCursor"))

  implicit val decoderFilter: Decoder[UserFilter] = deriveDecoder
  implicit val gTypeFilter = deriveInputObjectType[UserFilter](InputObjectTypeName("UserFilter"))
}

It fails on

Error:(88, 63) Can't find companion object for User.UserCursor'. This can happen when it's nested too deeply. Please consider defining it as a top-level object or directly inside of another class or object.
  implicit val gTypeCursor = deriveInputObjectType[UserCursor](InputObjectTypeName("UserCursor"))

Circe decoders are auto generated

OlegIlyenko commented 7 years ago

I think it is related to #261. Haven't investigated it in depth yet. But I think in this case it might be related to the fact that you are using a type alias. Maybe you can also check the macro, looks like it needs more code to analyse the type parameter and it's shape: https://github.com/sangria-graphql/sangria/blob/master/src/main/scala/sangria/macros/derive/DeriveObjectTypeMacro.scala#L22

fokot commented 7 years ago

Yes as I see the code problem is in Type.companion in scala.reflect.api. It doesn't return companion object for type aliases: https://github.com/scala/scala/blob/2.12.x/src/reflect/scala/reflect/api/Types.scala#L135 But I never worked with macros so I can't help here much

OlegIlyenko commented 6 years ago

Thanks for the hint! I will check it out