-
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.
LoneAnonymousOperation rule implemented
- Loading branch information
1 parent
d751be4
commit cf23a34
Showing
3 changed files
with
106 additions
and
0 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
30 changes: 30 additions & 0 deletions
30
src/main/scala/sangria/validation/rules/LoneAnonymousOperation.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,30 @@ | ||
package sangria.validation.rules | ||
|
||
import sangria.ast | ||
import sangria.ast.AstVisitorCommand._ | ||
import sangria.validation._ | ||
|
||
import scala.language.postfixOps | ||
|
||
/** | ||
* Lone anonymous operation | ||
* | ||
* A GraphQL document is only valid if when it contains an anonymous operation | ||
* (the query short-hand) that it contains only that one operation definition. | ||
*/ | ||
class LoneAnonymousOperation extends ValidationRule { | ||
override def visitor(ctx: ValidationContext) = new AstValidatingVisitor { | ||
var operationCount = 0 | ||
|
||
override val onEnter: ValidationVisit = { | ||
case ast.Document(definitions, _, _) => | ||
operationCount = definitions.count(_.isInstanceOf[ast.OperationDefinition]) | ||
Right(Continue) | ||
case op: ast.OperationDefinition => | ||
if (op.name.isEmpty && operationCount > 1) | ||
Left(Vector(AnonOperationNotAloneViolation(ctx.sourceMapper, op.position.toList))) | ||
else | ||
Right(Continue) | ||
} | ||
} | ||
} |
73 changes: 73 additions & 0 deletions
73
src/test/scala/sangria/validation/rules/LoneAnonymousOperationSpec.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,73 @@ | ||
package sangria.validation.rules | ||
|
||
import org.scalatest.WordSpec | ||
import sangria.util.{Pos, ValidationSupport} | ||
|
||
class LoneAnonymousOperationSpec extends WordSpec with ValidationSupport { | ||
|
||
override val defaultRule = Some(new LoneAnonymousOperation) | ||
|
||
"Validate: Anonymous operation must be alone" should { | ||
"no operations" in expectPasses( | ||
""" | ||
fragment fragA on Type { | ||
field | ||
} | ||
""") | ||
|
||
"one anon operation" in expectPasses( | ||
""" | ||
{ | ||
field | ||
} | ||
""") | ||
|
||
"multiple named operations" in expectPasses( | ||
""" | ||
query Foo { | ||
field | ||
} | ||
query Bar { | ||
field | ||
} | ||
""") | ||
|
||
"anon operation with fragment" in expectPasses( | ||
""" | ||
{ | ||
...Foo | ||
} | ||
fragment Foo on Type { | ||
field | ||
} | ||
""") | ||
|
||
"multiple anon operations" in expectFails( | ||
""" | ||
{ | ||
fieldA | ||
} | ||
{ | ||
fieldB | ||
} | ||
""", | ||
List( | ||
"This anonymous operation must be the only defined operation." -> Some(Pos(2, 9)), | ||
"This anonymous operation must be the only defined operation." -> Some(Pos(5, 9)) | ||
)) | ||
|
||
"anon operation with another operation" in expectFails( | ||
""" | ||
{ | ||
fieldA | ||
} | ||
mutation Foo { | ||
fieldB | ||
} | ||
""", | ||
List( | ||
"This anonymous operation must be the only defined operation." -> Some(Pos(2, 9)) | ||
)) | ||
} | ||
} |