question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

deriveInputObjectType should handle Optional values

See original GitHub issue

If 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:closed
  • Created 7 years ago
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
OlegIlyenkocommented, Mar 4, 2017

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 use ScalaInput.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

0reactions
OlegIlyenkocommented, Aug 18, 2017

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found