Skip to content

Commit

Permalink
Handle skipValidation in coerceVariables (#1421)
Browse files Browse the repository at this point in the history
  • Loading branch information
ghostdogpr authored Jul 28, 2022
1 parent 5472838 commit c443841
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 13 deletions.
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

0 comments on commit c443841

Please sign in to comment.