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
34 changes: 24 additions & 10 deletions src/main/java/graphql/GraphqlErrorBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import graphql.language.SourceLocation;
import graphql.schema.DataFetchingEnvironment;

import javax.annotation.Nullable;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
Expand All @@ -31,10 +32,12 @@ public String getMessage() {
return message;
}

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

@Nullable
public List<SourceLocation> getLocations() {
return locations;
}
Expand All @@ -43,6 +46,7 @@ public ErrorClassification getErrorType() {
return errorType;
}

@Nullable
public Map<String, Object> getExtensions() {
return extensions;
}
Expand Down Expand Up @@ -80,23 +84,33 @@ public B message(String message, Object... formatArgs) {
return (B) this;
}

public B locations(List<SourceLocation> locations) {
this.locations.addAll(assertNotNull(locations));
public B locations(@Nullable List<SourceLocation> locations) {
if (locations != null) {
Copy link
Member

Choose a reason for hiding this comment

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

can we add the Nullable annotation to the argument to make this more clear?

Copy link
Member

Choose a reason for hiding this comment

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

@bbakerman I think that is a missunderstanding: the result is never null, but the argument. So the directive should be on the argument.

Copy link
Member Author

Choose a reason for hiding this comment

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

done

this.locations.addAll(locations);
} else {
this.locations = null;
}
return (B) this;
}

public B location(SourceLocation location) {
this.locations.add(assertNotNull(location));
public B location(@Nullable SourceLocation location) {
if (locations != null) {
this.locations.add(location);
}
return (B) this;
}

public B path(ResultPath path) {
this.path = assertNotNull(path).toList();
public B path(@Nullable ResultPath path) {
if (path != null) {
this.path = path.toList();
} else {
this.path = null;
}
return (B) this;
}

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

Expand All @@ -105,8 +119,8 @@ public B errorType(ErrorClassification errorType) {
return (B) this;
}

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

Expand Down
14 changes: 14 additions & 0 deletions src/test/groovy/graphql/GraphqlErrorBuilderTest.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -123,4 +123,18 @@ class GraphqlErrorBuilderTest extends Specification {
then:
thrown(AssertException)
}

def "can have nullable attributes"() {
when:
def error = GraphqlErrorBuilder.newError().message("msg")
.locations(null)
.extensions(null)
.path(null)
.build()
then:
error.message == "msg"
error.locations == null
error.path == null
error.extensions == null
}
}