Skip to content
Open
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
5 changes: 3 additions & 2 deletions src/main/java/graphql/language/NodeUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,13 @@ public static GetOperationResult getOperation(Document document, String operatio
fragmentsByName.put(fragmentDefinition.getName(), fragmentDefinition);
}
}
if (operationName == null && operationsByName.size() > 1) {
boolean operationNameProvided = operationName != null && !operationName.isEmpty();
if (!operationNameProvided && operationsByName.size() > 1) {
throw new UnknownOperationException("Must provide operation name if query contains multiple operations.");
}
OperationDefinition operation;

if (operationName == null || operationName.isEmpty()) {
if (!operationNameProvided) {
operation = operationsByName.values().iterator().next();
} else {
operation = operationsByName.get(operationName);
Expand Down
5 changes: 4 additions & 1 deletion src/test/groovy/graphql/language/NodeUtilTest.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,14 @@ class NodeUtilTest extends Specification {
''')

when:
NodeUtil.getOperation(doc, null)
NodeUtil.getOperation(doc, operationName)

then:
def ex = thrown(UnknownOperationException)
ex.message == "Must provide operation name if query contains multiple operations."

where:
operationName << [null, '']
}

def "getOperation: when multiple operations are defined in the query and operation name doesn't match any of the query operations then it should throw UnknownOperationException"() {
Expand Down