Skip to content

Commit

Permalink
Refuse to execute query if it contains unexpected definitions. Closes #…
Browse files Browse the repository at this point in the history
  • Loading branch information
OlegIlyenko committed May 23, 2016
1 parent 4973614 commit 620934b
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
11 changes: 9 additions & 2 deletions src/main/scala/sangria/execution/Executor.scala
Expand Up @@ -112,9 +112,16 @@ case class Executor[Ctx, Root](
if (document.operations.size != 1 && operationName.isEmpty)
Failure(OperationSelectionError("Must provide operation name if query contains multiple operations", exceptionHandler))
else {
val operation = operationName flatMap (opName document.operations get Some(opName)) orElse document.operations.values.headOption
val unexpectedDefinition = document.definitions.find(d !(d.isInstanceOf[ast.OperationDefinition] || d.isInstanceOf[ast.FragmentDefinition]))

operation map (Success(_)) getOrElse Failure(OperationSelectionError(s"Unknown operation name: ${operationName.get}", exceptionHandler))
unexpectedDefinition match {
case Some(unexpected)
Failure(new ExecutionError(s"GraphQL cannot execute a request containing a ${unexpected.getClass.getSimpleName}.", exceptionHandler))
case None
val operation = operationName flatMap (opName document.operations get Some(opName)) orElse document.operations.values.headOption

operation map (Success(_)) getOrElse Failure(OperationSelectionError(s"Unknown operation name: ${operationName.get}", exceptionHandler))
}
}

def executeOperation[Input](
Expand Down
17 changes: 17 additions & 0 deletions src/test/scala/sangria/execution/ExecutorSpec.scala
Expand Up @@ -635,5 +635,22 @@ class ExecutorSpec extends WordSpec with Matchers with FutureResultSupport {
"field" "defFutFail",
"locations" List(Map("line" 4, "column" 11))))))
}

"fails to execute a query containing a type definition" in {
val Success(doc) = QueryParser.parse(
"""
{ a }
type Query { foo: String }
""")

val schema = Schema(DataType)

val error = intercept[ExecutionError] {
Executor.execute(schema, doc, root = new TestSubject,userContext = Ctx()).await
}

error.getMessage should be ("GraphQL cannot execute a request containing a ObjectTypeDefinition.")
}
}
}

0 comments on commit 620934b

Please sign in to comment.