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

fix: recursively translate input value strings to enums #1136

Merged
merged 3 commits into from
Nov 11, 2021
Merged

fix: recursively translate input value strings to enums #1136

merged 3 commits into from
Nov 11, 2021

Conversation

frekw
Copy link
Collaborator

@frekw frekw commented Nov 11, 2021

No description provided.

core/src/main/scala/caliban/parsing/VariablesUpdater.scala Outdated Show resolved Hide resolved
core/src/main/scala/caliban/parsing/VariablesUpdater.scala Outdated Show resolved Hide resolved
core/src/main/scala/caliban/parsing/VariablesUpdater.scala Outdated Show resolved Hide resolved
core/src/main/scala/caliban/parsing/VariablesUpdater.scala Outdated Show resolved Hide resolved
)
} yield assertTrue(res.data.toString == "{\"query\":\"BLUE\"}")
},
testM("it doesn't allow strings as enums in GQL syntax") {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this test is duplicated?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Whoops, must've copy pasted too much. Sorry, was a tad stressed :D

case _ => value
}
case NamedType(name, _) =>
rootType.types.get(name).map(t => resolveEnumValues(value, t, rootType)).getOrElse(value)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if this one has a list inside. It doesn't need to recurse on this function?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lists should be handled here -- this is more that we need to special case the top level Type before we get the "real" __Type.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should be covered by this test case

testM("it correctly handles lists of enums") {
case class QueryInput(color: List[COLOR], string: String)
case class Query(query: Field => QueryInput => UIO[String])
val query =
"""query MyQuery($color: [COLOR!]!) {
| query(string: "test", color: $color)
|}""".stripMargin
for {
ref <- Ref.make[Option[Field]](None)
api = graphQL(
RootResolver(
Query(
query = info => { i =>
ref.set(Option(info)).as(i.string)
}
)
)
)
interpreter <- api.interpreter
_ <- interpreter.executeRequest(
request = GraphQLRequest(
query = Some(query),
// "color" is a string here since it will come directly from
// parsed JSON which is unaware that it should be an Enum
variables = Some(
Map(
"color" ->
InputValue.ListValue(List(Value.StringValue("BLUE")))
)
)
)
)
res <- ref.get
} yield assertTrue(
res.get.arguments("color") == InputValue.ListValue(List(EnumValue("BLUE")))
)
},

@ghostdogpr ghostdogpr merged commit 93aa19f into ghostdogpr:master Nov 11, 2021
@frekw frekw deleted the fix/recurive-enum-values branch November 12, 2021 07:30
@darl
Copy link
Contributor

darl commented Nov 17, 2021

Why this transformation necessary?
Aren't those line enough?
https://github.com/ghostdogpr/caliban/blob/master/core/src/main/scala-2/caliban/schema/ArgBuilderDerivation.scala#L44-L45

I encountered same problem with validation of Int/Float.
And don't sure how to fix.

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

Successfully merging this pull request may close these issues.

3 participants