deriveInputObjectType should handle Optional values
See original GitHub issueIf I have a class with an Option in it, even if that option is a scalar value, I seem to have to redefine it to “help” the deriveInputObjectType macro:
case class A(id: Int, b: Option[Int] = None)
implicit lazy val AType: InputObjectType[A] = deriveInputObjectType[A](
ReplaceInputField("b", InputField("b", OptionInputType(IntType))))
Otherwise I get the error:
Type Option[Int] cannot be used as a default value
Is there something I am doing wrong here? Why can’t the deriveInputObjectType seem to handle Option[_] values the way that deriveObjectType does?
Issue Analytics
- State:
- Created 7 years ago
- Comments:5 (4 by maintainers)
Top Results From Across the Web
sangria-graphql/sangria - Gitter
{Argument, InputObjectType} object XXX { // when you have FromInput for all types in case class (Int, String) you can derive it case...
Read more >Using the same object for ObjectType and InputObjectType
In your example, both GraphQL types would have the same name. This is not allowed since all types within a GraphQL schema share...
Read more >Learn Sangria
deriveInputObjectType [T] - constructs an InputObjectType[T] with fields found in T ... You can use @GraphQLDefault as well as normal Scala default values...
Read more >Schemas and Types - GraphQL
When an argument is optional, we can define a default value - if the unit argument is not passed, it will be set...
Read more >Built-in reference types - C# reference - Microsoft Learn
You can assign values of any type to variables of type object . Any object variable can be assigned to its default value...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
For optional values, you can just skip
= None
and it will work as intended. Because of the way default values are implemented in scala and sangria, it is not always possible to use the default value provided in a signature. For these cases, I would suggest to provide a default value via@GraphQLDefault
compile-time annotation. It’s also possible that you would need to useScalaInput.scalaInput
(e.g.@GraphQLDefault(scalaInput(FooBar(1, ,"foo")))
) for more complex defaults.This part of the library is not very well documented, I believe. I will try to improve the docs for it. At some point, I also wanted to revisit the handling of default values and see how it can be improved
I will close this issue for the moment being due to inactivity and the technical limitation I mentioned in previous comment. Please feel free to reopen this issue if you would like to continue this discussion.