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

Handle skipValidation in coerceVariables (1.x backport) #1421

Merged
merged 1 commit into from
Jul 28, 2022
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
2 changes: 1 addition & 1 deletion core/src/main/scala/caliban/GraphQL.scala
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ trait GraphQL[-R] { self =>
}
typeToValidate = if (intro) introspectionRootType else rootType
schemaToExecute = if (intro) introspectionRootSchema else schema
validatedRequest <- VariablesCoercer.coerceVariables(request, doc, typeToValidate)
validatedRequest <- VariablesCoercer.coerceVariables(request, doc, typeToValidate, skipValidation)

validate = (doc: Document) =>
Validator
Expand Down
34 changes: 22 additions & 12 deletions core/src/main/scala/caliban/parsing/VariablesCoercer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ object VariablesCoercer {
def coerceVariables(
req: GraphQLRequest,
doc: Document,
rootType: RootType
rootType: RootType,
skipValidation: Boolean
): IO[ValidationError, GraphQLRequest] = {
val variableDefinitions = doc.operationDefinitions.flatMap(_.variableDefinitions)
val variables = req.variables.getOrElse(Map.empty)
Expand All @@ -35,23 +36,32 @@ object VariablesCoercer {
s"Type of variable '$variableName' $e",
"Variables can only be input types. Objects, unions, and interfaces cannot be used as inputs."
)
) *> {
)
.unless(skipValidation) *> {
val value =
variables
.get(definition.name)
.map(rewriteValues(_, definition.variableType, rootTypeWithPrimitives, s"Variable '$variableName'"))
.map(inputValue =>
rewriteValues(
inputValue,
definition.variableType,
rootTypeWithPrimitives,
s"Variable '$variableName'"
).catchSome { case _ if skipValidation => ZIO.succeed(inputValue) }
)
.orElse(definition.defaultValue.map(IO.succeed(_)))

(value, definition.variableType.nonNull) match {
case (None, true) =>
IO.fail(
ValidationError(
s"Variable '$variableName' is null but is specified to be non-null.",
"The value of a variable must be compatible with its type."
(value, definition.variableType.nullable) match {
case (None, nullable) =>
if (skipValidation || nullable) ZIO.succeed(coercedValues)
else
IO.fail(
ValidationError(
s"Variable '$variableName' is null but is specified to be non-null.",
"The value of a variable must be compatible with its type."
)
)
)
case (None, false) => IO.succeed(coercedValues)
case (Some(v), _) =>
case (Some(v), _) =>
v.map(value => coercedValues + (definition.name -> value))
}
}
Expand Down