Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 43 additions & 21 deletions src/main/java/graphql/GraphqlErrorBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,44 @@
/**
* This helps you build {@link graphql.GraphQLError}s and also has a quick way to make a {@link graphql.execution.DataFetcherResult}s
* from that error.
*
* @param <B> this base class allows you to derive new classes from this base error builder
*/
@SuppressWarnings("unchecked")
@PublicApi
public class GraphqlErrorBuilder {
public class GraphqlErrorBuilder<B extends GraphqlErrorBuilder<?>> {

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

public String getMessage() {
return message;
}

public List<Object> getPath() {
return path;
}

public List<SourceLocation> getLocations() {
return locations;
}

public ErrorClassification getErrorType() {
return errorType;
}

public Map<String, Object> getExtensions() {
return extensions;
}

/**
* @return a builder of {@link graphql.GraphQLError}s
*/
public static GraphqlErrorBuilder newError() {
return new GraphqlErrorBuilder();
public static GraphqlErrorBuilder<?> newError() {
return new GraphqlErrorBuilder<>();
}

/**
Expand All @@ -40,52 +62,52 @@ public static GraphqlErrorBuilder newError() {
*
* @return a builder of {@link graphql.GraphQLError}s
*/
public static GraphqlErrorBuilder newError(DataFetchingEnvironment dataFetchingEnvironment) {
return new GraphqlErrorBuilder()
public static GraphqlErrorBuilder<?> newError(DataFetchingEnvironment dataFetchingEnvironment) {
return new GraphqlErrorBuilder<>()
.location(dataFetchingEnvironment.getField().getSourceLocation())
.path(dataFetchingEnvironment.getExecutionStepInfo().getPath());
}

private GraphqlErrorBuilder() {
protected GraphqlErrorBuilder() {
}

public GraphqlErrorBuilder message(String message, Object... formatArgs) {
public B message(String message, Object... formatArgs) {
if (formatArgs == null || formatArgs.length == 0) {
this.message = assertNotNull(message);
} else {
this.message = String.format(assertNotNull(message), formatArgs);
}
return this;
return (B) this;
}

public GraphqlErrorBuilder locations(List<SourceLocation> locations) {
public B locations(List<SourceLocation> locations) {
this.locations.addAll(assertNotNull(locations));
return this;
return (B) this;
}

public GraphqlErrorBuilder location(SourceLocation location) {
public B location(SourceLocation location) {
this.locations.add(assertNotNull(location));
return this;
return (B) this;
}

public GraphqlErrorBuilder path(ResultPath path) {
public B path(ResultPath path) {
this.path = assertNotNull(path).toList();
return this;
return (B) this;
}

public GraphqlErrorBuilder path(List<Object> path) {
public B path(List<Object> path) {
this.path = assertNotNull(path);
return this;
return (B) this;
}

public GraphqlErrorBuilder errorType(ErrorClassification errorType) {
public B errorType(ErrorClassification errorType) {
this.errorType = assertNotNull(errorType);
return this;
return (B) this;
}

public GraphqlErrorBuilder extensions(Map<String, Object> extensions) {
public B extensions(Map<String, Object> extensions) {
this.extensions = assertNotNull(extensions);
return this;
return (B) this;
}

/**
Expand Down Expand Up @@ -147,7 +169,7 @@ public String toString() {
*
* @return a new data fetcher result that contains the built error
*/
public DataFetcherResult toResult() {
public DataFetcherResult<?> toResult() {
return DataFetcherResult.newResult()
.error(build())
.build();
Expand Down
13 changes: 13 additions & 0 deletions src/test/groovy/graphql/GraphqlErrorBuilderTest.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,19 @@ class GraphqlErrorBuilderTest extends Specification {
graphQLError.getErrorType() == ErrorType.DataFetchingException
}

def "builder getters work"() {
when:
def errorBuilder = GraphqlErrorBuilder.newError()
.message("Gunfight at the %s corral", "NotOK")
.location(location)
.path(["a","b"])
then:
errorBuilder.getMessage() == "Gunfight at the NotOK corral"
errorBuilder.getErrorType() == ErrorType.DataFetchingException
errorBuilder.getPath() == ["a","b"]
errorBuilder.getLocations() == [location]
}

def "data fetcher result building works"() {
when:
def result = GraphqlErrorBuilder.newError().message("Gunfight at the %s corral", "NotOK").toResult()
Expand Down
24 changes: 24 additions & 0 deletions src/test/groovy/readme/ReadmeExamples.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package readme;

import graphql.GraphQLError;
import graphql.GraphqlErrorBuilder;
import graphql.InvalidSyntaxError;
import graphql.Scalars;
import graphql.StarWarsData;
import graphql.TypeResolutionEnvironment;
Expand Down Expand Up @@ -448,6 +451,27 @@ public GraphQLObjectType getType(TypeResolutionEnvironment env) {
};
}

static class SpecialError extends InvalidSyntaxError {

public SpecialError(SpecialErrorBuilder builder) {
super(builder.getLocations(), builder.getMessage());
}
}

static class SpecialErrorBuilder extends GraphqlErrorBuilder<SpecialErrorBuilder> {

@Override
public SpecialError build() {
return new SpecialError(this);
}
}

private void errorBuilderExample() {
GraphQLError err = GraphqlErrorBuilder.newError().message("direct").build();

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

private DataFetcher createDataFetcher(FieldDefinition definition, Directive directive) {
throw new UnsupportedOperationException("Not implemented");
}
Expand Down