Skip to content

Commit f0464d2

Browse files
committed
add source location to invalid syntax errors
1 parent d194776 commit f0464d2

4 files changed

Lines changed: 58 additions & 8 deletions

File tree

src/main/java/graphql/GraphQL.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33

44
import graphql.execution.Execution;
55
import graphql.language.Document;
6+
import graphql.language.SourceLocation;
67
import graphql.parser.Parser;
78
import graphql.schema.GraphQLSchema;
89
import graphql.validation.ValidationError;
9-
import graphql.validation.ValidationErrorType;
1010
import graphql.validation.Validator;
1111
import org.antlr.v4.runtime.RecognitionException;
1212

@@ -57,8 +57,9 @@ public ExecutionResult execute(String requestString, String operationName, Objec
5757
try {
5858
document = parser.parseDocument(requestString);
5959
} catch (RecognitionException e) {
60-
ValidationError validationError = new ValidationError(ValidationErrorType.InvalidSyntax);
61-
return new ExecutionResultImpl(Arrays.asList(validationError));
60+
SourceLocation sourceLocation = new SourceLocation(e.getOffendingToken().getLine(), e.getOffendingToken().getCharPositionInLine());
61+
InvalidSyntaxError invalidSyntaxError = new InvalidSyntaxError(sourceLocation);
62+
return new ExecutionResultImpl(Arrays.asList(invalidSyntaxError));
6263
}
6364

6465
Validator validator = new Validator();

src/main/java/graphql/InvalidSyntaxError.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,20 @@
11
package graphql;
22

33

4+
import graphql.language.SourceLocation;
5+
46
public class InvalidSyntaxError implements GraphQLError {
7+
8+
public InvalidSyntaxError(SourceLocation sourceLocation) {
9+
this.sourceLocation = sourceLocation;
10+
}
11+
12+
private final SourceLocation sourceLocation;
13+
14+
public SourceLocation getSourceLocation() {
15+
return sourceLocation;
16+
}
17+
518
@Override
619
public ErrorType geErrorType() {
720
return ErrorType.InvalidSyntax;

src/main/java/graphql/language/SourceLocation.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,39 @@ public SourceLocation(int line, int positionInLine) {
1010
this.line = line;
1111
this.positionInLine = positionInLine;
1212
}
13+
14+
public int getLine() {
15+
return line;
16+
}
17+
18+
public int getPositionInLine() {
19+
return positionInLine;
20+
}
21+
22+
@Override
23+
public boolean equals(Object o) {
24+
if (this == o) return true;
25+
if (o == null || getClass() != o.getClass()) return false;
26+
27+
SourceLocation that = (SourceLocation) o;
28+
29+
if (line != that.line) return false;
30+
return positionInLine == that.positionInLine;
31+
32+
}
33+
34+
@Override
35+
public int hashCode() {
36+
int result = line;
37+
result = 31 * result + positionInLine;
38+
return result;
39+
}
40+
41+
@Override
42+
public String toString() {
43+
return "SourceLocation{" +
44+
"line=" + line +
45+
", positionInLine=" + positionInLine +
46+
'}';
47+
}
1348
}

src/test/groovy/graphql/GraphQLTest.groovy

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
package graphql
22

3+
import graphql.language.SourceLocation
34
import graphql.schema.GraphQLArgument
45
import graphql.schema.GraphQLFieldDefinition
56
import graphql.schema.GraphQLObjectType
67
import graphql.schema.GraphQLSchema
7-
import graphql.validation.ValidationErrorType
88
import spock.lang.Specification
99

1010
import static graphql.Scalars.GraphQLString
@@ -66,7 +66,7 @@ class GraphQLTest extends Specification {
6666
).build();
6767

6868
when:
69-
def result = new GraphQL(graphQLSchema).execute( '{ simpson { id, name } }').result
69+
def result = new GraphQL(graphQLSchema).execute('{ simpson { id, name } }').result
7070

7171
then:
7272
result == [simpson: [id: '123', name: 'homer']]
@@ -88,7 +88,7 @@ class GraphQLTest extends Specification {
8888
).build()
8989

9090
when:
91-
def errors = new GraphQL(schema).execute( '{ hello(arg:11) }').errors
91+
def errors = new GraphQL(schema).execute('{ hello(arg:11) }').errors
9292

9393
then:
9494
errors.size() == 1
@@ -103,10 +103,11 @@ class GraphQLTest extends Specification {
103103
).build()
104104

105105
when:
106-
def errors = new GraphQL(schema).execute( '{ hello(() }').errors
106+
def errors = new GraphQL(schema).execute('{ hello(() }').errors
107107

108108
then:
109109
errors.size() == 1
110-
errors[0].validationErrorType == ValidationErrorType.InvalidSyntax
110+
errors[0].geErrorType() == ErrorType.InvalidSyntax
111+
errors[0].sourceLocation == new SourceLocation(1, 8)
111112
}
112113
}

0 commit comments

Comments
 (0)