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
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 61 additions & 21 deletions core/src/main/scala/caliban/parsing/VariablesUpdater.scala
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package caliban.parsing

import caliban.GraphQLRequest
import caliban.InputValue.ListValue
import caliban.Value.StringValue
import caliban.introspection.adt._
import caliban.parsing.adt.Definition.ExecutableDefinition.OperationDefinition
import caliban.parsing.adt.Type.{ ListType, NamedType }
import caliban.parsing.adt._
import caliban.schema.RootType
import caliban.{ InputValue, Value }
Expand All @@ -16,38 +19,75 @@ object VariablesUpdater {
): GraphQLRequest = {
val variableDefinitions = doc.operationDefinitions.flatMap(_.variableDefinitions)
val updated = req.variables.getOrElse(Map.empty).map { case (key, value) =>
val v = variableDefinitions.find(_.name == key).map(resolveEnumValues(value, _, rootType)).getOrElse(value)
val v =
variableDefinitions
.find(_.name == key)
.map { definition =>
rewriteValues(value, definition.variableType, rootType)
}
.getOrElse(value)

key -> v
}

req.copy(variables = Some(updated))
}

private def rewriteValues(value: InputValue, `type`: Type, rootType: RootType): InputValue =
`type` match {
case ListType(ofType, _) =>
value match {
case ListValue(values) =>
ListValue(values.map(v => rewriteValues(v, ofType, rootType)))
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")))
)
},

}

// Since we cannot separate a String from an Enum when variables
// are parsed, we need to translate from strings to enums here
// if we have a valid enum field.
private def resolveEnumValues(
value: InputValue,
definition: VariableDefinition,
typ: __Type,
rootType: RootType
): InputValue = {
val t = Type
.innerType(definition.variableType)

rootType.types
.get(t)
.map(_.kind)
.flatMap { kind =>
(kind, value) match {
case (__TypeKind.ENUM, InputValue.ListValue(v)) =>
Some(
InputValue.ListValue(v.map(resolveEnumValues(_, definition, rootType)))
)
case (__TypeKind.ENUM, Value.StringValue(v)) =>
Some(Value.EnumValue(v))
case _ => None
): InputValue =
typ.kind match {
case __TypeKind.INPUT_OBJECT =>
value match {
case InputValue.ObjectValue(fields) =>
val defs = typ.inputFields.getOrElse(List.empty)
InputValue.ObjectValue(fields.map { case (k, v) =>
val updated =
defs.find(_.name == k).map(field => resolveEnumValues(v, field.`type`(), rootType)).getOrElse(value)

(k, updated)
})
case _ =>
value
}
}
.getOrElse(value)
}

case __TypeKind.LIST =>
value match {
case ListValue(values) =>
typ.ofType
.map(innerType => ListValue(values.map(value => resolveEnumValues(value, innerType, rootType))))
.getOrElse(value)
case _ => value
}

case __TypeKind.NON_NULL =>
typ.ofType
.map(innerType => resolveEnumValues(value, innerType, rootType))
.getOrElse(value)

case __TypeKind.ENUM =>
value match {
case StringValue(value) => Value.EnumValue(value)
case _ => value
}
case _ =>
value
}
}
38 changes: 38 additions & 0 deletions core/src/test/scala/caliban/execution/FieldArgsSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,44 @@ object FieldArgsSpec extends DefaultRunnableSpec {
interpreter <- api.interpreter
res <- interpreter.execute(query)
} yield assert(res.errors.headOption)(isSome(anything))
},
testM("it correctly handles lists of objects with enums") {
case class QueryInput(filter: List[Filter])
case class Filter(color: COLOR)
case class Query(query: QueryInput => String)
val query =
"""query MyQuery($filter: [FilterInput!]!) {
| query(filter: $filter)
|}""".stripMargin

val api = graphQL(
RootResolver(
Query(
query = q => q.filter.headOption.map(_.color.toString).getOrElse("Missing")
)
)
)

for {
interpreter <- api.interpreter
res <- interpreter.executeRequest(
request = GraphQLRequest(
query = Some(query),
variables = Some(
Map(
"filter" ->
InputValue.ListValue(
List(
InputValue.ObjectValue(
Map("color" -> Value.StringValue("BLUE"))
)
)
)
)
)
)
)
} yield assertTrue(res.data.toString == "{\"query\":\"BLUE\"}")
}
)
}