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: Unpack nested enum values for input values #1101

Merged
merged 1 commit into from
Oct 18, 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
8 changes: 6 additions & 2 deletions core/src/main/scala/caliban/execution/Field.scala
Original file line number Diff line number Diff line change
Expand Up @@ -131,9 +131,13 @@ object Field {
.map(_.kind)
.flatMap { kind =>
(kind, value) match {
case (__TypeKind.ENUM, Value.StringValue(v)) =>
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
case _ => None
}
}
.getOrElse(value)
Expand Down
96 changes: 94 additions & 2 deletions core/src/test/scala/caliban/execution/FieldArgsSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,7 @@ object FieldArgsSpec extends DefaultRunnableSpec {
} yield assertTrue(
res.get.arguments.get("color").get == EnumValue("BLUE")
)
}
) +
},
testM("it forward args as correct type from variables") {
case class QueryInput(color: COLOR, string: String)
case class Query(query: Field => QueryInput => UIO[String])
Expand Down Expand Up @@ -88,5 +87,98 @@ object FieldArgsSpec extends DefaultRunnableSpec {
} yield assertTrue(
res.get.arguments.get("color").get == EnumValue("BLUE")
)
},
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)) *>
ZIO.succeed(i.string)
}
)
)
)
interpreter <- api.interpreter
qres <- 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")))
)
)
),
skipValidation = false,
enableIntrospection = true,
queryExecution = QueryExecution.Parallel
)
res <- ref.get
} yield assertTrue(
res.get.arguments.get("color").get == InputValue.ListValue(List(EnumValue("BLUE")))
)
},
testM("it correctly handles objects of enums") {
case class QueryInput(nested: QueryInputInput)
case class QueryInputInput(color: List[COLOR], string: String)
case class Query(query: Field => QueryInput => UIO[String])
val query =
"""query MyQuery($color: [COLOR!]!) {
| query(nested: { color: $color, string: "foo" })
|}""".stripMargin

for {
ref <- Ref.make[Option[Field]](None)
api = graphQL(
RootResolver(
Query(
query = info => { i =>
println(s"i: $i")
ref.set(Option(info)) *>
ZIO.succeed(i.nested.string)
}
)
)
)
interpreter <- api.interpreter
qres <- 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")))
)
)
),
skipValidation = false,
enableIntrospection = true,
queryExecution = QueryExecution.Parallel
)
res <- ref.get
} yield assertTrue(
res.get.arguments.get("nested").get ==
InputValue.ObjectValue(
Map(
"color" -> InputValue.ListValue(List(EnumValue("BLUE"))),
"string" -> Value.StringValue("foo")
)
)
)
}
)
}