-
-
Notifications
You must be signed in to change notification settings - Fork 251
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: GQL syntax strings are incorrectly validated as enums #1134
Merged
Merged
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
core/src/main/scala/caliban/parsing/VariablesUpdater.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package caliban.parsing | ||
|
||
import caliban.GraphQLRequest | ||
import caliban.introspection.adt._ | ||
import caliban.parsing.adt.Definition.ExecutableDefinition.OperationDefinition | ||
import caliban.parsing.adt._ | ||
import caliban.schema.RootType | ||
import caliban.{ InputValue, Value } | ||
import zio.{ IO, UIO } | ||
|
||
object VariablesUpdater { | ||
def updateVariables( | ||
req: GraphQLRequest, | ||
doc: Document, | ||
rootType: RootType | ||
): 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) | ||
key -> v | ||
} | ||
|
||
req.copy(variables = Some(updated)) | ||
} | ||
|
||
// 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, | ||
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 | ||
} | ||
} | ||
.getOrElse(value) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,7 @@ import caliban.Value.EnumValue | |
import zio._ | ||
import zio.test._ | ||
import zio.test.environment.TestEnvironment | ||
import Assertion._ | ||
|
||
object FieldArgsSpec extends DefaultRunnableSpec { | ||
sealed trait COLOR | ||
|
@@ -155,6 +156,27 @@ object FieldArgsSpec extends DefaultRunnableSpec { | |
) | ||
) | ||
) | ||
}, | ||
testM("it doesn't allow strings as enums in GQL syntax") { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. indeed |
||
case class QueryInput(color: COLOR) | ||
case class Query(query: QueryInput => UIO[String]) | ||
val query = | ||
"""query { | ||
| query(color: "BLUE") | ||
|}""".stripMargin | ||
|
||
val api = graphQL( | ||
RootResolver( | ||
Query( | ||
query = i => ZIO.succeed(i.toString) | ||
) | ||
) | ||
) | ||
|
||
for { | ||
interpreter <- api.interpreter | ||
res <- interpreter.execute(query) | ||
} yield assert(res.errors.headOption)(isSome(anything)) | ||
} | ||
) | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
minor: I usually use full paths, so
zio.test.Assertion._
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍