Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

argOpt returns unexpected type at runtime #434

Closed
cneijenhuis opened this issue Jan 16, 2019 · 2 comments · Fixed by #958
Closed

argOpt returns unexpected type at runtime #434

cneijenhuis opened this issue Jan 16, 2019 · 2 comments · Fixed by #958

Comments

@cneijenhuis
Copy link

cneijenhuis commented Jan 16, 2019

Given an argument like this:

val OptionalStringArg: Argument[Option[String]] = Argument("foo", OptionInputType(StringType))

I'd expect that argOpt returns Option[Option[String]]. The compiler also thinks so, however at runtime I get Option[String].

val t1: Option[Option[String]] = ctx.argOpt(OptionalStringArg)
val t2: Option[String] = ctx.argOpt(OptionalStringArg) // Does not compile

println(t1) // prints "Some(foobar)", instead of the expected "Some(Some(foobar))"

I believe this instanceOf is to blame: https://github.com/sangria-graphql/sangria/blob/master/src/main/scala/sangria/schema/Context.scala#L301

@paulpdaniels
Copy link
Contributor

I'm not sure that type of argument makes much sense in GraphQL, what would the schema for an option of an option of a string look like?

@cneijenhuis
Copy link
Author

Right, it looks a little strange without context 😉 I wanted to use this inside a middleware. The middleware is called for any field, including fields that don't have this argument declared in the schema. Therefore:

  • None - the field does not have the argument
  • Some(None) - the field has the argument, but it was not set

I don't really need that distinction, but I can't call arg() instead of argOpt() because it will throw an exception in the first case.

My workaround is to first check if the field has the argument at all, and only if so, call arg().

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants