Comments (3)
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.
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.
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)
- Scala 3 schema derivation duplicate types in union
- Scala 3 different schema derivation nested enum items missing
- Codegen: Incorrect order of description in server sealed trait generation HOT 1
- Outdated `SchemaReporting` docs
- Allow executing mutations / subscriptions in parallel / batched mode
- Support for IO in the tapir interop adapter HOT 12
- Add an apollo rover template HOT 1
- `SchemaComparison` from `caliban-tools` does not detect changes in base interfaces HOT 2
- Duplicate keys when querying with a fragment compared to inlining HOT 4
- Connection leak with http4s websocket subscriptions HOT 11
- Having a single `FieldWrapper(wrapPureValues=true)` in API causes all FieldWrappers to wrap pure values
- ` print(caliban.InputValue.ObjectValue(Map("name" ->caliban.Value.StringValue("string") )).toString)` HOT 1
- Fine-grained support for `null` values. HOT 4
- Allow overriding Tapir endpoint inputs created by Caliban's HttpInterpreter
- Setup CI to run benchmarks and access results HOT 3
- Broken backward compatibility in CatsInterop since v2.7.0 HOT 1
- Since 2.8.0 compilation take forever HOT 8
- Interpreter hangs when using enums with the same name HOT 4
- Deprecated arguments are missing in introspection query HOT 7
- [Feature request] Support for feature toggle directives HOT 6
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from caliban.