Skip to content

Commit c4e0f3b

Browse files
tinnoubbakerman
authored andcommitted
Add specific UnknownOperationException when operation name is missing
1 parent 1171c95 commit c4e0f3b

File tree

4 files changed

+63
-3
lines changed

4 files changed

+63
-3
lines changed

docs/exceptions.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,11 @@ These are not graphql errors in execution but rather totally unacceptable condit
4242
is thrown if the schema is not valid when built via
4343
graphql.schema.GraphQLSchema.Builder#build()`
4444

45+
- `graphql.execution.UnknownOperationException`
46+
47+
if multiple operations are defined in the query and
48+
the operation name is missing or there is no matching operation name
49+
contained in the GraphQL query.
4550

4651
- `graphql.GraphQLException`
4752

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package graphql.execution;
2+
3+
import graphql.GraphQLException;
4+
import graphql.PublicApi;
5+
6+
/**
7+
* This is thrown if multiple operations are defined in the query and
8+
* the operation name is missing or there is no matching operation name
9+
* contained in the GraphQL query.
10+
*/
11+
@PublicApi
12+
public class UnknownOperationException extends GraphQLException {
13+
14+
public UnknownOperationException(String message) {
15+
super(message);
16+
}
17+
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package graphql.language;
22

3-
import graphql.GraphQLException;
43
import graphql.Internal;
4+
import graphql.execution.UnknownOperationException;
55
import graphql.util.FpKit;
66

77
import java.util.LinkedHashMap;
@@ -59,7 +59,7 @@ public static GetOperationResult getOperation(Document document, String operatio
5959
}
6060
}
6161
if (operationName == null && operationsByName.size() > 1) {
62-
throw new GraphQLException("missing operation name");
62+
throw new UnknownOperationException("Must provide operation name if query contains multiple operations.");
6363
}
6464
OperationDefinition operation;
6565

@@ -69,7 +69,7 @@ public static GetOperationResult getOperation(Document document, String operatio
6969
operation = operationsByName.get(operationName);
7070
}
7171
if (operation == null) {
72-
throw new GraphQLException("no operation found");
72+
throw new UnknownOperationException(String.format("Unknown operation named '%s'.", operationName));
7373
}
7474
GetOperationResult result = new GetOperationResult();
7575
result.fragmentsByName = fragmentsByName;
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package graphql.language
2+
3+
import graphql.TestUtil
4+
import graphql.execution.UnknownOperationException
5+
import spock.lang.Specification
6+
7+
class NodeUtilTest extends Specification {
8+
9+
def "getOperation: when multiple operations are defined in the query and operation name is missing then it should throw UnknownOperationException"() {
10+
setup:
11+
def doc = TestUtil.parseQuery('''
12+
query Q1 { id }
13+
query Q2 { id }
14+
''')
15+
16+
when:
17+
NodeUtil.getOperation(doc, null)
18+
19+
then:
20+
def ex = thrown(UnknownOperationException)
21+
ex.message == "Must provide operation name if query contains multiple operations."
22+
}
23+
24+
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"() {
25+
setup:
26+
def doc = TestUtil.parseQuery('''
27+
query Q1 { id }
28+
query Q2 { id }
29+
''')
30+
31+
when:
32+
NodeUtil.getOperation(doc, 'Unknown')
33+
34+
then:
35+
def ex = thrown(UnknownOperationException)
36+
ex.message == "Unknown operation named 'Unknown'."
37+
}
38+
}

0 commit comments

Comments
 (0)