diff --git a/src/main/java/graphql/incremental/IncrementalPayload.java b/src/main/java/graphql/incremental/IncrementalPayload.java index 76b1fbb405..78f918fcf2 100644 --- a/src/main/java/graphql/incremental/IncrementalPayload.java +++ b/src/main/java/graphql/incremental/IncrementalPayload.java @@ -1,6 +1,5 @@ package graphql.incremental; -import graphql.ExecutionResult; import graphql.ExperimentalApi; import graphql.GraphQLError; import graphql.execution.ResultPath; @@ -61,7 +60,7 @@ public Map getExtensions() { return this.extensions; } - protected Map toSpecification() { + public Map toSpecification() { Map result = new LinkedHashMap<>(); result.put("path", path); @@ -122,25 +121,25 @@ public T errors(List errors) { return (T) this; } - public Builder addErrors(List errors) { + public T addErrors(List errors) { this.errors.addAll(errors); - return this; + return (T) this; } - public Builder addError(GraphQLError error) { + public T addError(GraphQLError error) { this.errors.add(error); - return this; + return (T) this; } - public Builder extensions(Map extensions) { + public T extensions(Map extensions) { this.extensions = extensions; - return this; + return (T) this; } - public Builder addExtension(String key, Object value) { + public T addExtension(String key, Object value) { this.extensions = (this.extensions == null ? new LinkedHashMap<>() : this.extensions); this.extensions.put(key, value); - return this; + return (T) this; } } } diff --git a/src/test/groovy/graphql/incremental/DeferPayloadTest.groovy b/src/test/groovy/graphql/incremental/DeferPayloadTest.groovy index 90d6a0899c..a37fa3ca71 100644 --- a/src/test/groovy/graphql/incremental/DeferPayloadTest.groovy +++ b/src/test/groovy/graphql/incremental/DeferPayloadTest.groovy @@ -1,6 +1,7 @@ package graphql.incremental - +import graphql.GraphqlErrorBuilder +import graphql.execution.ResultPath import spock.lang.Specification class DeferPayloadTest extends Specification { @@ -14,8 +15,85 @@ class DeferPayloadTest extends Specification { then: spec == [ - data : null, - path : null, + data: null, + path: null, + ] + } + + def "can construct an instance using builder"() { + def payload = DeferPayload.newDeferredItem() + .data("twow is that a bee") + .path(["hello"]) + .errors([]) + .addError(GraphqlErrorBuilder.newError() + .message("wow") + .build()) + .addErrors([ + GraphqlErrorBuilder.newError() + .message("yep") + .build(), + ]) + .extensions([echo: "Hello world"]) + .build() + + when: + def serialized = payload.toSpecification() + + then: + serialized == [ + data : "twow is that a bee", + path : ["hello"], + errors : [ + [ + message : "wow", + locations : [], + extensions: [classification: "DataFetchingException"], + ], + [ + message : "yep", + locations : [], + extensions: [classification: "DataFetchingException"], + ], + ], + extensions: [ + echo: "Hello world", + ], + ] + } + + def "errors replaces existing errors"() { + def payload = DeferPayload.newDeferredItem() + .data("twow is that a bee") + .path(ResultPath.fromList(["test", "echo"])) + .addError(GraphqlErrorBuilder.newError() + .message("wow") + .build()) + .addErrors([]) + .errors([ + GraphqlErrorBuilder.newError() + .message("yep") + .build(), + ]) + .extensions([echo: "Hello world"]) + .build() + + when: + def serialized = payload.toSpecification() + + then: + serialized == [ + data : "twow is that a bee", + errors : [ + [ + message : "yep", + locations : [], + extensions: [classification: "DataFetchingException"], + ], + ], + extensions: [ + echo: "Hello world", + ], + path : ["test", "echo"], ] } } diff --git a/src/test/groovy/graphql/incremental/StreamPayloadTest.groovy b/src/test/groovy/graphql/incremental/StreamPayloadTest.groovy index bee0d88631..7386cb8efd 100644 --- a/src/test/groovy/graphql/incremental/StreamPayloadTest.groovy +++ b/src/test/groovy/graphql/incremental/StreamPayloadTest.groovy @@ -1,5 +1,7 @@ package graphql.incremental +import graphql.GraphqlErrorBuilder +import graphql.execution.ResultPath import spock.lang.Specification class StreamPayloadTest extends Specification { @@ -17,4 +19,81 @@ class StreamPayloadTest extends Specification { path : null, ] } + + def "can construct an instance using builder"() { + def payload = StreamPayload.newStreamedItem() + .items(["twow is that a bee"]) + .path(["hello"]) + .errors([]) + .addError(GraphqlErrorBuilder.newError() + .message("wow") + .build()) + .addErrors([ + GraphqlErrorBuilder.newError() + .message("yep") + .build(), + ]) + .extensions([echo: "Hello world"]) + .build() + + when: + def serialized = payload.toSpecification() + + then: + serialized == [ + items : ["twow is that a bee"], + path : ["hello"], + errors : [ + [ + message : "wow", + locations : [], + extensions: [classification: "DataFetchingException"], + ], + [ + message : "yep", + locations : [], + extensions: [classification: "DataFetchingException"], + ], + ], + extensions: [ + echo: "Hello world", + ], + ] + } + + def "errors replaces existing errors"() { + def payload = StreamPayload.newStreamedItem() + .items(["twow is that a bee"]) + .path(ResultPath.fromList(["test", "echo"])) + .addError(GraphqlErrorBuilder.newError() + .message("wow") + .build()) + .addErrors([]) + .errors([ + GraphqlErrorBuilder.newError() + .message("yep") + .build(), + ]) + .extensions([echo: "Hello world"]) + .build() + + when: + def serialized = payload.toSpecification() + + then: + serialized == [ + items : ["twow is that a bee"], + errors : [ + [ + message : "yep", + locations : [], + extensions: [classification: "DataFetchingException"], + ], + ], + extensions: [ + echo: "Hello world", + ], + path : ["test", "echo"], + ] + } }