Skip to content

Commit d5cdc07

Browse files
committed
fix: treat empty operation name the same as null
previously, if an empty operation name was given and multiple operations were provided in the query, then the first operation would be returned, rather than complaining that no operation name was provided.
1 parent e5f21b8 commit d5cdc07

File tree

2 files changed

+7
-3
lines changed

2 files changed

+7
-3
lines changed

src/main/java/graphql/language/NodeUtil.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,12 +73,13 @@ public static GetOperationResult getOperation(Document document, String operatio
7373
fragmentsByName.put(fragmentDefinition.getName(), fragmentDefinition);
7474
}
7575
}
76-
if (operationName == null && operationsByName.size() > 1) {
76+
boolean operationNameProvided = operationName != null && !operationName.isEmpty();
77+
if (!operationNameProvided && operationsByName.size() > 1) {
7778
throw new UnknownOperationException("Must provide operation name if query contains multiple operations.");
7879
}
7980
OperationDefinition operation;
8081

81-
if (operationName == null || operationName.isEmpty()) {
82+
if (!operationNameProvided) {
8283
operation = operationsByName.values().iterator().next();
8384
} else {
8485
operation = operationsByName.get(operationName);

src/test/groovy/graphql/language/NodeUtilTest.groovy

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,14 @@ class NodeUtilTest extends Specification {
1515
''')
1616

1717
when:
18-
NodeUtil.getOperation(doc, null)
18+
NodeUtil.getOperation(doc, operationName)
1919

2020
then:
2121
def ex = thrown(UnknownOperationException)
2222
ex.message == "Must provide operation name if query contains multiple operations."
23+
24+
where:
25+
operationName << [null, '']
2326
}
2427

2528
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"() {

0 commit comments

Comments
 (0)