X Tutup
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
17 changes: 17 additions & 0 deletions src/main/java/graphql/ErrorClassification.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,21 @@ public interface ErrorClassification {
default Object toSpecification(GraphQLError error) {
return String.valueOf(this);
}

/**
* This produces a simple ErrorClassification that represents the provided String. You can
* use this factory method to give out simple but custom error classifications.
*
* @param errorClassification the string that represents the error classification
*
* @return a ErrorClassification that is that provided string
*/
static ErrorClassification errorClassification(String errorClassification) {
return new ErrorClassification() {
@Override
public String toString() {
return errorClassification;
}
};
}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I found myself having to make custom error classification recently and while and enum works, a string is just as good

        GraphQLError err = GraphQLError.newError()
                .message("direct")
                .errorType(ErrorClassification.of("oneOffClassification"))
                .build();

}
82 changes: 82 additions & 0 deletions src/main/java/graphql/GraphQLError.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package graphql;


import graphql.execution.ResultPath;
import graphql.language.SourceLocation;
import org.jetbrains.annotations.Nullable;

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

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

/**
* A builder of {@link GraphQLError}s
*/
interface Builder<B extends Builder<B>> {

/**
* Sets the message of the error using {@link String#format(String, Object...)} with the arguments
*
* @param message the message
* @param formatArgs the arguments to use
*
* @return this builder
*/
B message(String message, Object... formatArgs);

/**
* This adds locations to the error
*
* @param locations the locations to add
*
* @return this builder
*/
B locations(@Nullable List<SourceLocation> locations);

/**
* This adds a location to the error
*
* @param location the locations to add
*
* @return this builder
*/
B location(@Nullable SourceLocation location);

/**
* Sets the path of the message
*
* @param path can be null
*
* @return this builder
*/
B path(@Nullable ResultPath path);

/**
* Sets the path of the message
*
* @param path can be null
*
* @return this builder
*/
B path(@Nullable List<Object> path);

/**
* Sets the {@link ErrorClassification} of the message
*
* @param errorType the error classification to use
*
* @return this builder
*/
B errorType(ErrorClassification errorType);

/**
* Sets the extensions of the message
*
* @param extensions the extensions to use
*
* @return this builder
*/
B extensions(@Nullable Map<String, Object> extensions);

/**
* @return a newly built GraphqlError
*/
GraphQLError build();
}
}
4 changes: 2 additions & 2 deletions src/main/java/graphql/GraphqlErrorBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
import graphql.execution.ResultPath;
import graphql.language.SourceLocation;
import graphql.schema.DataFetchingEnvironment;

import org.jetbrains.annotations.Nullable;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
Expand All @@ -20,7 +20,7 @@
*/
@SuppressWarnings("unchecked")
@PublicApi
public class GraphqlErrorBuilder<B extends GraphqlErrorBuilder<B>> {
public class GraphqlErrorBuilder<B extends GraphqlErrorBuilder<B>> implements GraphQLError.Builder<B> {

private String message;
private List<Object> path;
Expand Down
15 changes: 15 additions & 0 deletions src/test/groovy/graphql/GraphqlErrorBuilderTest.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -137,4 +137,19 @@ class GraphqlErrorBuilderTest extends Specification {
error.path == null
error.extensions == null
}

def "can use a builder direct from graphql error"() {
when:
def error = GraphQLError.newError().message("msg")
.locations(null)
.extensions([x : "y"])
.path(null)
.build()
then:
error.message == "msg"
error.locations == null
error.extensions == [x : "y"]
error.path == null

}
}
6 changes: 5 additions & 1 deletion src/test/groovy/readme/ReadmeExamples.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package readme;

import graphql.ErrorClassification;
import graphql.GraphQLError;
import graphql.GraphqlErrorBuilder;
import graphql.InvalidSyntaxError;
Expand Down Expand Up @@ -470,7 +471,10 @@ public SpecialError build() {
}

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

SpecialError specialErr = new SpecialErrorBuilder().message("special").build();
}
Expand Down
X Tutup