Skip to content

Commit 3b3bf3f

Browse files
authored
This allows you to derive new classes from the error builder class (graphql-java#2651)
1 parent 7bac61e commit 3b3bf3f

3 files changed

Lines changed: 80 additions & 21 deletions

File tree

‎src/main/java/graphql/GraphqlErrorBuilder.java‎

Lines changed: 43 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -14,22 +14,44 @@
1414
/**
1515
* This helps you build {@link graphql.GraphQLError}s and also has a quick way to make a {@link graphql.execution.DataFetcherResult}s
1616
* from that error.
17+
*
18+
* @param <B> this base class allows you to derive new classes from this base error builder
1719
*/
20+
@SuppressWarnings("unchecked")
1821
@PublicApi
19-
public class GraphqlErrorBuilder {
22+
public class GraphqlErrorBuilder<B extends GraphqlErrorBuilder<?>> {
2023

2124
private String message;
2225
private List<Object> path;
2326
private List<SourceLocation> locations = new ArrayList<>();
2427
private ErrorClassification errorType = ErrorType.DataFetchingException;
2528
private Map<String, Object> extensions = null;
2629

30+
public String getMessage() {
31+
return message;
32+
}
33+
34+
public List<Object> getPath() {
35+
return path;
36+
}
37+
38+
public List<SourceLocation> getLocations() {
39+
return locations;
40+
}
41+
42+
public ErrorClassification getErrorType() {
43+
return errorType;
44+
}
45+
46+
public Map<String, Object> getExtensions() {
47+
return extensions;
48+
}
2749

2850
/**
2951
* @return a builder of {@link graphql.GraphQLError}s
3052
*/
31-
public static GraphqlErrorBuilder newError() {
32-
return new GraphqlErrorBuilder();
53+
public static GraphqlErrorBuilder<?> newError() {
54+
return new GraphqlErrorBuilder<>();
3355
}
3456

3557
/**
@@ -40,52 +62,52 @@ public static GraphqlErrorBuilder newError() {
4062
*
4163
* @return a builder of {@link graphql.GraphQLError}s
4264
*/
43-
public static GraphqlErrorBuilder newError(DataFetchingEnvironment dataFetchingEnvironment) {
44-
return new GraphqlErrorBuilder()
65+
public static GraphqlErrorBuilder<?> newError(DataFetchingEnvironment dataFetchingEnvironment) {
66+
return new GraphqlErrorBuilder<>()
4567
.location(dataFetchingEnvironment.getField().getSourceLocation())
4668
.path(dataFetchingEnvironment.getExecutionStepInfo().getPath());
4769
}
4870

49-
private GraphqlErrorBuilder() {
71+
protected GraphqlErrorBuilder() {
5072
}
5173

52-
public GraphqlErrorBuilder message(String message, Object... formatArgs) {
74+
public B message(String message, Object... formatArgs) {
5375
if (formatArgs == null || formatArgs.length == 0) {
5476
this.message = assertNotNull(message);
5577
} else {
5678
this.message = String.format(assertNotNull(message), formatArgs);
5779
}
58-
return this;
80+
return (B) this;
5981
}
6082

61-
public GraphqlErrorBuilder locations(List<SourceLocation> locations) {
83+
public B locations(List<SourceLocation> locations) {
6284
this.locations.addAll(assertNotNull(locations));
63-
return this;
85+
return (B) this;
6486
}
6587

66-
public GraphqlErrorBuilder location(SourceLocation location) {
88+
public B location(SourceLocation location) {
6789
this.locations.add(assertNotNull(location));
68-
return this;
90+
return (B) this;
6991
}
7092

71-
public GraphqlErrorBuilder path(ResultPath path) {
93+
public B path(ResultPath path) {
7294
this.path = assertNotNull(path).toList();
73-
return this;
95+
return (B) this;
7496
}
7597

76-
public GraphqlErrorBuilder path(List<Object> path) {
98+
public B path(List<Object> path) {
7799
this.path = assertNotNull(path);
78-
return this;
100+
return (B) this;
79101
}
80102

81-
public GraphqlErrorBuilder errorType(ErrorClassification errorType) {
103+
public B errorType(ErrorClassification errorType) {
82104
this.errorType = assertNotNull(errorType);
83-
return this;
105+
return (B) this;
84106
}
85107

86-
public GraphqlErrorBuilder extensions(Map<String, Object> extensions) {
108+
public B extensions(Map<String, Object> extensions) {
87109
this.extensions = assertNotNull(extensions);
88-
return this;
110+
return (B) this;
89111
}
90112

91113
/**
@@ -147,7 +169,7 @@ public String toString() {
147169
*
148170
* @return a new data fetcher result that contains the built error
149171
*/
150-
public DataFetcherResult toResult() {
172+
public DataFetcherResult<?> toResult() {
151173
return DataFetcherResult.newResult()
152174
.error(build())
153175
.build();

‎src/test/groovy/graphql/GraphqlErrorBuilderTest.groovy‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,19 @@ class GraphqlErrorBuilderTest extends Specification {
3939
graphQLError.getErrorType() == ErrorType.DataFetchingException
4040
}
4141

42+
def "builder getters work"() {
43+
when:
44+
def errorBuilder = GraphqlErrorBuilder.newError()
45+
.message("Gunfight at the %s corral", "NotOK")
46+
.location(location)
47+
.path(["a","b"])
48+
then:
49+
errorBuilder.getMessage() == "Gunfight at the NotOK corral"
50+
errorBuilder.getErrorType() == ErrorType.DataFetchingException
51+
errorBuilder.getPath() == ["a","b"]
52+
errorBuilder.getLocations() == [location]
53+
}
54+
4255
def "data fetcher result building works"() {
4356
when:
4457
def result = GraphqlErrorBuilder.newError().message("Gunfight at the %s corral", "NotOK").toResult()

‎src/test/groovy/readme/ReadmeExamples.java‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package readme;
22

3+
import graphql.GraphQLError;
4+
import graphql.GraphqlErrorBuilder;
5+
import graphql.InvalidSyntaxError;
36
import graphql.Scalars;
47
import graphql.StarWarsData;
58
import graphql.TypeResolutionEnvironment;
@@ -448,6 +451,27 @@ public GraphQLObjectType getType(TypeResolutionEnvironment env) {
448451
};
449452
}
450453

454+
static class SpecialError extends InvalidSyntaxError {
455+
456+
public SpecialError(SpecialErrorBuilder builder) {
457+
super(builder.getLocations(), builder.getMessage());
458+
}
459+
}
460+
461+
static class SpecialErrorBuilder extends GraphqlErrorBuilder<SpecialErrorBuilder> {
462+
463+
@Override
464+
public SpecialError build() {
465+
return new SpecialError(this);
466+
}
467+
}
468+
469+
private void errorBuilderExample() {
470+
GraphQLError err = GraphqlErrorBuilder.newError().message("direct").build();
471+
472+
SpecialError specialErr = new SpecialErrorBuilder().message("special").build();
473+
}
474+
451475
private DataFetcher createDataFetcher(FieldDefinition definition, Directive directive) {
452476
throw new UnsupportedOperationException("Not implemented");
453477
}

0 commit comments

Comments
 (0)