Code Monkey home page Code Monkey logo

Comments (3)

ghostdogpr avatar ghostdogpr commented on August 25, 2024

Hi @jmpicnic
Thanks for the kind words 😄

As I mentioned (briefly) here, any type you pass as an argument needs an instance of the ArgBuilder typeclass, which defines how to turn an input GraphQL Value into an actual value of that type.

In your case let's say you pass the enum as a String:

  implicit val thingTypeArgBuilder: ArgBuilder[ThingType] = {
    case Value.StringValue(value) =>
      Task(ThingType.withName(value)).mapError(ex => ExecutionError(s"Invalid input received for ThingType", Some(ex)))
    case other => IO.fail(ExecutionError(s"Can't build a ThingType from input $other"))
  }

You could also support passing the enum as a number by adding a case Value.IntValue.

PS: you have "name" instead of just name in enumSchema.

PS2: you decided to use a Scalar for your enum, which might be okay for your needs. In case you would like to have a GraphQL Enum type, you could do something like this:

  implicit val thingTypeSchema: Schema[Any, ThingType] = new Schema[Any, ThingType] {
    override def toType(isInput: Boolean): __Type =
      Types.makeEnum(
        Some("ThingType"),
        None,
        ThingType.values.toList.map(v => __EnumValue(v.toString, None, isDeprecated = false, None))
      )
    override def resolve(value: ThingType, arguments: Map[String, Value]): ZIO[Any, ExecutionError, ResolvedValue] =
      UIO(EnumValue(value.toString))
  }

Might even be possible to make it generic for any Enumeration.

from caliban.

jmpicnic avatar jmpicnic commented on August 25, 2024

Thanks a lot, and thanks for the patience. I did not see that part of the doc. I focused on the CustomTypes and missed that completely.

Cheers.

from caliban.

jmpicnic avatar jmpicnic commented on August 25, 2024

With two little generic functions, it is a breeze to get them working:

	import caliban.schema
	def enumSchema[E <: Enumeration](name: String) = schema.Schema.scalarSchema[E#Value]("name", None, e => caliban.ResponseValue.StringValue(e.toString))
	def enumArgBuilder[E <: Enumeration](e: E) : ArgBuilder[E#Value] = {
		case Value.StringValue(value) =>
			Task(e.withName(value)).mapError(ex => ExecutionError(s"Invalid input received for Enumeration Value", Some(ex)))
		case other => IO.fail(ExecutionError(s"Can't build The enumeration ${e.getClass} value from input $other"))
	}
       // simply used
	implicit val thingTypeSchema = enumSchema[ThingType.type]("ThingType")
	implicit val thingTypeArgBuilder = enumArgBuilder(ThingType)

Thanks

Miguel

from caliban.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.