package modules.user.graphql.schema

import common.InputUnmarshallerGenerator
import core.graphql.{GraphQLSchema, UserContext}
import javax.inject.Inject
import modules.user.graphql.resolvers.UserResolver
import modules.user.model.{RegisterUserInput, User, UserPayload}
import sangria.schema.{Argument, Field, InputObjectType, ObjectType}
import sangria.macros.derive.{InputObjectTypeName, ObjectTypeName, deriveInputObjectType, deriveObjectType}
import sangria.marshalling.FromInput

class UserSchema @Inject()(userResolver: UserResolver) extends GraphQLSchema
  with InputUnmarshallerGenerator {

  implicit val registerUserInput: InputObjectType[RegisterUserInput] = deriveInputObjectType(InputObjectTypeName("RegisterUserInput"))
  implicit val user: ObjectType[UserContext, User] = deriveObjectType(ObjectTypeName("User"))
  implicit val UserPayload: ObjectType[UserContext, UserPayload] = deriveObjectType(ObjectTypeName("UserPayload"))

  implicit val registerUserInputUnmarshaller: FromInput[RegisterUserInput] = inputUnmarshaller {
    input =>
      RegisterUserInput(
        username = input("username").asInstanceOf[String],
        email = input("email").asInstanceOf[String],
        password = input("password").asInstanceOf[String]
      )
  }

  override def mutations: List[Field[UserContext, Unit]] = List(
    Field(
      name = "register",
      fieldType = UserPayload,
      arguments = List(Argument("input", registerUserInput)),
      resolve = sangriaContext => userResolver.register(sangriaContext.args.arg[RegisterUserInput]("input"))
    )
  )
}
Create 'GraphQL' register mutation for 'User'
Create 'GraphQL' register mutation for 'User'
