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
15 changes: 15 additions & 0 deletions src/main/java/graphql/incremental/DeferPayload.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
* Represents a defer payload
Expand Down Expand Up @@ -41,6 +42,20 @@ public Map<String, Object> toSpecification() {
return map;
}

@Override
public int hashCode() {
return Objects.hash(super.hashCode(), data);
}

@Override
public boolean equals(Object obj) {
if (this == obj) return true;
if (obj == null || getClass() != obj.getClass()) return false;
if (!super.equals(obj)) return false;
DeferPayload that = (DeferPayload) obj;
return Objects.equals(data, that.data);
}

/**
* @return a {@link DeferPayload.Builder} that can be used to create an instance of {@link DeferPayload}
*/
Expand Down
14 changes: 14 additions & 0 deletions src/main/java/graphql/incremental/IncrementalPayload.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

import static java.util.stream.Collectors.toList;

Expand Down Expand Up @@ -82,6 +83,19 @@ protected Object errorsToSpec(List<GraphQLError> errors) {
return errors.stream().map(GraphQLError::toSpecification).collect(toList());
}

public int hashCode() {
return Objects.hash(path, label, errors, extensions);
}

public boolean equals(Object obj) {
if (this == obj) return true;
if (obj == null || getClass() != obj.getClass()) return false;
IncrementalPayload that = (IncrementalPayload) obj;
return Objects.equals(path, that.path) &&
Objects.equals(label, that.label) &&
Objects.equals(errors, that.errors) &&
Objects.equals(extensions, that.extensions);
}

protected static abstract class Builder<T extends Builder<T>> {
protected List<Object> path;
Expand Down
15 changes: 15 additions & 0 deletions src/main/java/graphql/incremental/StreamPayload.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
* Represents a stream payload
Expand Down Expand Up @@ -40,6 +41,20 @@ public Map<String, Object> toSpecification() {
return map;
}

@Override
public int hashCode() {
return Objects.hash(super.hashCode(), items);
}

@Override
public boolean equals(Object obj) {
if (this == obj) return true;
if (obj == null || getClass() != obj.getClass()) return false;
if (!super.equals(obj)) return false;
StreamPayload that = (StreamPayload) obj;
return Objects.equals(items, that.items);
}

/**
* @return a {@link Builder} that can be used to create an instance of {@link StreamPayload}
*/
Expand Down
46 changes: 46 additions & 0 deletions src/test/groovy/graphql/incremental/DeferPayloadTest.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import graphql.GraphqlErrorBuilder
import graphql.execution.ResultPath
import spock.lang.Specification

import static graphql.GraphQLError.newError

class DeferPayloadTest extends Specification {
def "null data is included"() {
def payload = DeferPayload.newDeferredItem()
Expand Down Expand Up @@ -96,4 +98,48 @@ class DeferPayloadTest extends Specification {
path : ["test", "echo"],
]
}

def "equals and hashcode methods work correctly"() {
when:
def payload = DeferPayload.newDeferredItem()
.data("data1")
.path(["path1"])
.label("label1")
.errors([newError().message("message1").build()])
.extensions([key: "value1"])
.build()

def equivalentPayload = DeferPayload.newDeferredItem()
.data("data1")
.path(["path1"])
.label("label1")
.errors([newError().message("message1").build()])
.extensions([key: "value1"])
.build()

def totallyDifferentPayload = DeferPayload.newDeferredItem()
.data("data2")
.path(["path2"])
.label("label2")
.errors([newError().message("message2").build()])
.extensions([key: "value2"])
.build()

def slightlyDifferentPayload = DeferPayload.newDeferredItem()
.data("data1")
.path(["path1"])
.label("label1")
.errors([newError().message("message1").build()])
.extensions([key: "value2"])
.build()

then:
payload == equivalentPayload
payload != totallyDifferentPayload
payload != slightlyDifferentPayload

payload.hashCode() == equivalentPayload.hashCode()
payload.hashCode() != totallyDifferentPayload.hashCode()
payload.hashCode() != slightlyDifferentPayload.hashCode()
}
}
64 changes: 59 additions & 5 deletions src/test/groovy/graphql/incremental/StreamPayloadTest.groovy
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package graphql.incremental

import graphql.GraphqlErrorBuilder
import graphql.execution.ResultPath
import spock.lang.Specification

import static graphql.GraphqlErrorBuilder.newError

class StreamPayloadTest extends Specification {
def "null data is included"() {
def payload = StreamPayload.newStreamedItem()
Expand All @@ -25,11 +26,11 @@ class StreamPayloadTest extends Specification {
.items(["twow is that a bee"])
.path(["hello"])
.errors([])
.addError(GraphqlErrorBuilder.newError()
.addError(newError()
.message("wow")
.build())
.addErrors([
GraphqlErrorBuilder.newError()
newError()
.message("yep")
.build(),
])
Expand Down Expand Up @@ -65,12 +66,12 @@ class StreamPayloadTest extends Specification {
def payload = StreamPayload.newStreamedItem()
.items(["twow is that a bee"])
.path(ResultPath.fromList(["test", "echo"]))
.addError(GraphqlErrorBuilder.newError()
.addError(newError()
.message("wow")
.build())
.addErrors([])
.errors([
GraphqlErrorBuilder.newError()
newError()
.message("yep")
.build(),
])
Expand All @@ -96,4 +97,57 @@ class StreamPayloadTest extends Specification {
path : ["test", "echo"],
]
}

def "test equals and hashCode methods work"() {
given:
def items1 = ["test1"]
def items2 = ["test2", "test3"]
def path1 = ["test", "echo"]
def path2 = ["test", "echo", "foo"]
def errors1 = [newError().message("error1").build()]
def errors2 = [newError().message("error2").build()]
def extensions1 = [echo: "1"]
def extensions2 = [echo: "2"]

def payload = new StreamPayload.Builder()
.items(items1)
.path(path1)
.label("label1")
.errors(errors1)
.extensions(extensions1)
.build()

def equivalentPayload = new StreamPayload.Builder()
.items(items1)
.path(path1)
.label("label1")
.errors(errors1)
.extensions(extensions1)
.build()

def totallyDifferentPayload = new StreamPayload.Builder()
.items(items2)
.path(path2)
.label("label2")
.errors(errors2)
.extensions(extensions2)
.build()

def slightlyDifferentPayload = new StreamPayload.Builder()
.items(items2)
.path(path2)
.label("label1")
.errors(errors1)
.extensions(extensions2)
.build()

expect:
payload == equivalentPayload
payload != totallyDifferentPayload
payload != slightlyDifferentPayload

payload.hashCode() == equivalentPayload.hashCode()
payload.hashCode() != totallyDifferentPayload.hashCode()
payload.hashCode() != slightlyDifferentPayload.hashCode()
}
}