Skip to content

Commit df2a717

Browse files
authored
Adds an error builder on GraphQLError (graphql-java#3011)
* An error builder on GraphQLError * An error builder on GraphQLError - wat * ErrorClassification builder support * ErrorClassification builder support - readme example * ErrorClassification builder support - renamed method
1 parent 04df386 commit df2a717

5 files changed

Lines changed: 121 additions & 3 deletions

File tree

src/main/java/graphql/ErrorClassification.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,21 @@ public interface ErrorClassification {
2323
default Object toSpecification(GraphQLError error) {
2424
return String.valueOf(this);
2525
}
26+
27+
/**
28+
* This produces a simple ErrorClassification that represents the provided String. You can
29+
* use this factory method to give out simple but custom error classifications.
30+
*
31+
* @param errorClassification the string that represents the error classification
32+
*
33+
* @return a ErrorClassification that is that provided string
34+
*/
35+
static ErrorClassification errorClassification(String errorClassification) {
36+
return new ErrorClassification() {
37+
@Override
38+
public String toString() {
39+
return errorClassification;
40+
}
41+
};
42+
}
2643
}

src/main/java/graphql/GraphQLError.java

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

33

4+
import graphql.execution.ResultPath;
45
import graphql.language.SourceLocation;
6+
import org.jetbrains.annotations.Nullable;
57

68
import java.io.Serializable;
79
import java.util.List;
@@ -66,5 +68,85 @@ default Map<String, Object> getExtensions() {
6668
return null;
6769
}
6870

71+
/**
72+
* @return a new builder of {@link GraphQLError}s
73+
*/
74+
static Builder<?> newError() {
75+
return new GraphqlErrorBuilder<>();
76+
}
77+
78+
/**
79+
* A builder of {@link GraphQLError}s
80+
*/
81+
interface Builder<B extends Builder<B>> {
82+
83+
/**
84+
* Sets the message of the error using {@link String#format(String, Object...)} with the arguments
85+
*
86+
* @param message the message
87+
* @param formatArgs the arguments to use
88+
*
89+
* @return this builder
90+
*/
91+
B message(String message, Object... formatArgs);
6992

93+
/**
94+
* This adds locations to the error
95+
*
96+
* @param locations the locations to add
97+
*
98+
* @return this builder
99+
*/
100+
B locations(@Nullable List<SourceLocation> locations);
101+
102+
/**
103+
* This adds a location to the error
104+
*
105+
* @param location the locations to add
106+
*
107+
* @return this builder
108+
*/
109+
B location(@Nullable SourceLocation location);
110+
111+
/**
112+
* Sets the path of the message
113+
*
114+
* @param path can be null
115+
*
116+
* @return this builder
117+
*/
118+
B path(@Nullable ResultPath path);
119+
120+
/**
121+
* Sets the path of the message
122+
*
123+
* @param path can be null
124+
*
125+
* @return this builder
126+
*/
127+
B path(@Nullable List<Object> path);
128+
129+
/**
130+
* Sets the {@link ErrorClassification} of the message
131+
*
132+
* @param errorType the error classification to use
133+
*
134+
* @return this builder
135+
*/
136+
B errorType(ErrorClassification errorType);
137+
138+
/**
139+
* Sets the extensions of the message
140+
*
141+
* @param extensions the extensions to use
142+
*
143+
* @return this builder
144+
*/
145+
B extensions(@Nullable Map<String, Object> extensions);
146+
147+
/**
148+
* @return a newly built GraphqlError
149+
*/
150+
GraphQLError build();
151+
}
70152
}

src/main/java/graphql/GraphqlErrorBuilder.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
import graphql.execution.ResultPath;
55
import graphql.language.SourceLocation;
66
import graphql.schema.DataFetchingEnvironment;
7-
87
import org.jetbrains.annotations.Nullable;
8+
99
import java.util.ArrayList;
1010
import java.util.List;
1111
import java.util.Map;
@@ -20,7 +20,7 @@
2020
*/
2121
@SuppressWarnings("unchecked")
2222
@PublicApi
23-
public class GraphqlErrorBuilder<B extends GraphqlErrorBuilder<B>> {
23+
public class GraphqlErrorBuilder<B extends GraphqlErrorBuilder<B>> implements GraphQLError.Builder<B> {
2424

2525
private String message;
2626
private List<Object> path;

src/test/groovy/graphql/GraphqlErrorBuilderTest.groovy

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,4 +137,19 @@ class GraphqlErrorBuilderTest extends Specification {
137137
error.path == null
138138
error.extensions == null
139139
}
140+
141+
def "can use a builder direct from graphql error"() {
142+
when:
143+
def error = GraphQLError.newError().message("msg")
144+
.locations(null)
145+
.extensions([x : "y"])
146+
.path(null)
147+
.build()
148+
then:
149+
error.message == "msg"
150+
error.locations == null
151+
error.extensions == [x : "y"]
152+
error.path == null
153+
154+
}
140155
}

src/test/groovy/readme/ReadmeExamples.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package readme;
22

3+
import graphql.ErrorClassification;
34
import graphql.GraphQLError;
45
import graphql.GraphqlErrorBuilder;
56
import graphql.InvalidSyntaxError;
@@ -470,7 +471,10 @@ public SpecialError build() {
470471
}
471472

472473
private void errorBuilderExample() {
473-
GraphQLError err = GraphqlErrorBuilder.newError().message("direct").build();
474+
GraphQLError err = GraphQLError.newError()
475+
.message("direct")
476+
.errorType(ErrorClassification.errorClassification("customClassification"))
477+
.build();
474478

475479
SpecialError specialErr = new SpecialErrorBuilder().message("special").build();
476480
}

0 commit comments

Comments
 (0)