-
Notifications
You must be signed in to change notification settings - Fork 221
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
validate query using oneOf input type
- Loading branch information
Amanda Steinwedel
committed
Jul 18, 2024
1 parent
096ee1c
commit 79704d3
Showing
5 changed files
with
132 additions
and
2 deletions.
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
37 changes: 37 additions & 0 deletions
37
modules/core/src/main/scala/sangria/validation/rules/ExactlyOneOfFieldGiven.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,37 @@ | ||
package sangria.validation.rules | ||
|
||
import sangria.ast | ||
import sangria.schema | ||
import sangria.ast.AstVisitorCommand | ||
import sangria.validation._ | ||
|
||
/** For oneOf input objects, exactly one field should be non-null. */ | ||
class ExactlyOneOfFieldGiven extends ValidationRule { | ||
override def visitor(ctx: ValidationContext) = new AstValidatingVisitor { | ||
override val onEnter: ValidationVisit = { case ast.ObjectValue(fields, _, pos) => | ||
ctx.typeInfo.inputType match { | ||
case Some(inputType) => | ||
inputType.namedInputType match { | ||
case schema.InputObjectType(name, _, _, directives, _) if directives.exists { d => | ||
d.name == schema.OneOfDirective.name | ||
} => | ||
val nonNullFields = fields.filter { field => | ||
field.value match { | ||
case ast.NullValue(_, _) => false | ||
case _ => true | ||
} | ||
} | ||
|
||
nonNullFields.size match { | ||
case 1 => AstVisitorCommand.RightContinue | ||
case _ => | ||
Left(Vector(NotExactlyOneOfField(name, ctx.sourceMapper, pos.toList))) | ||
} | ||
|
||
case _ => AstVisitorCommand.RightContinue | ||
} | ||
case None => AstVisitorCommand.RightContinue | ||
} | ||
} | ||
} | ||
} |
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
70 changes: 70 additions & 0 deletions
70
modules/core/src/test/scala/sangria/validation/rules/ExactlyOneOfFieldGivenSpec.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,70 @@ | ||
package sangria.validation.rules | ||
|
||
import sangria.util.{Pos, ValidationSupport} | ||
import org.scalatest.wordspec.AnyWordSpec | ||
|
||
class ExactlyOneOfFieldGivenSpec extends AnyWordSpec with ValidationSupport { | ||
|
||
override val defaultRule = Some(new ExactlyOneOfFieldGiven) | ||
|
||
"Validate: exactly oneOf field given" should { | ||
"with exactly one non-null field given" in expectPasses(""" | ||
query OneOfQuery { | ||
oneOfQuery(input: { | ||
catName: "Gretel" | ||
}) { | ||
... on Cat { | ||
name | ||
} | ||
} | ||
} | ||
""") | ||
|
||
"with exactly one null field given" in expectFails( | ||
""" | ||
query OneOfQuery { | ||
oneOfQuery(input: { | ||
catName: null | ||
}) { | ||
... on Cat { | ||
name | ||
} | ||
} | ||
} | ||
""", | ||
List("Exactly one key must be specified for oneOf type 'OneOfInput'." -> Some(Pos(3, 31))) | ||
) | ||
|
||
"with no fields given" in expectFails( | ||
""" | ||
query OneOfQuery { | ||
oneOfQuery(input: {}) { | ||
... on Cat { | ||
name | ||
} | ||
} | ||
} | ||
""", | ||
List("Exactly one key must be specified for oneOf type 'OneOfInput'." -> Some(Pos(3, 31))) | ||
) | ||
|
||
"with more than one non-null fields given" in expectFails( | ||
""" | ||
query OneOfQuery { | ||
oneOfQuery(input: { | ||
catName: "Gretel", | ||
dogId: 123 | ||
}) { | ||
... on Cat { | ||
name | ||
} | ||
... on Dog { | ||
name | ||
} | ||
} | ||
} | ||
""", | ||
List("Exactly one key must be specified for oneOf type 'OneOfInput'." -> Some(Pos(3, 31))) | ||
) | ||
} | ||
} |