Replace GraphQLError with GraphQlResponseError

This allows exposing additional conveniences for inspecting errors.

See gh-10
This commit is contained in:
rstoyanchev
2022-03-18 08:35:11 +00:00
parent 96135183f5
commit db24c8f62b
15 changed files with 297 additions and 213 deletions

View File

@@ -19,13 +19,13 @@ package org.springframework.graphql.test.tester;
import java.util.List;
import graphql.ExecutionResult;
import graphql.GraphQLError;
import org.reactivestreams.Publisher;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import org.springframework.graphql.GraphQlRequest;
import org.springframework.graphql.GraphQlResponse;
import org.springframework.graphql.GraphQlResponseError;
import org.springframework.graphql.RequestOutput;
import org.springframework.graphql.client.GraphQlTransport;
import org.springframework.test.util.AssertionErrors;
@@ -58,7 +58,7 @@ abstract class AbstractDirectTransport implements GraphQlTransport {
Object data = output.getData();
AssertionErrors.assertTrue("Not a Publisher: " + data, data instanceof Publisher);
List<GraphQLError> errors = output.getErrors();
List<GraphQlResponseError> errors = output.getErrors();
AssertionErrors.assertTrue("Subscription errors: " + errors, CollectionUtils.isEmpty(errors));
return Flux.from((Publisher<ExecutionResult>) data)

View File

@@ -24,8 +24,8 @@ import com.jayway.jsonpath.Configuration;
import com.jayway.jsonpath.spi.json.JacksonJsonProvider;
import com.jayway.jsonpath.spi.mapper.JacksonMappingProvider;
import com.jayway.jsonpath.spi.mapper.MappingProvider;
import graphql.GraphQLError;
import org.springframework.graphql.GraphQlResponseError;
import org.springframework.graphql.client.AbstractGraphQlClientBuilder;
import org.springframework.graphql.client.GraphQlTransport;
import org.springframework.graphql.support.CachingDocumentSource;
@@ -57,7 +57,7 @@ public abstract class AbstractGraphQlTesterBuilder<B extends AbstractGraphQlTest
@Nullable
private Predicate<GraphQLError> errorFilter;
private Predicate<GraphQlResponseError> errorFilter;
private DocumentSource documentSource = new CachingDocumentSource(new ResourceDocumentSource());
@@ -67,7 +67,7 @@ public abstract class AbstractGraphQlTesterBuilder<B extends AbstractGraphQlTest
@Override
public B errorFilter(Predicate<GraphQLError> predicate) {
public B errorFilter(Predicate<GraphQlResponseError> predicate) {
this.errorFilter = (this.errorFilter != null ? errorFilter.and(predicate) : predicate);
return self();
}

View File

@@ -31,12 +31,12 @@ import com.jayway.jsonpath.Configuration;
import com.jayway.jsonpath.DocumentContext;
import com.jayway.jsonpath.JsonPath;
import com.jayway.jsonpath.TypeRef;
import graphql.GraphQLError;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.core.ResolvableType;
import org.springframework.graphql.GraphQlRequest;
import org.springframework.graphql.GraphQlResponse;
import org.springframework.graphql.GraphQlResponseError;
import org.springframework.graphql.client.GraphQlTransport;
import org.springframework.graphql.support.DocumentSource;
import org.springframework.lang.Nullable;
@@ -61,7 +61,7 @@ final class DefaultGraphQlTester implements GraphQlTester {
private final GraphQlTransport transport;
@Nullable
private final Predicate<GraphQLError> errorFilter;
private final Predicate<GraphQlResponseError> errorFilter;
private final Configuration jsonPathConfig;
@@ -76,7 +76,7 @@ final class DefaultGraphQlTester implements GraphQlTester {
* Package private constructor for use from {@link AbstractGraphQlTesterBuilder}.
*/
DefaultGraphQlTester(
GraphQlTransport transport, @Nullable Predicate<GraphQLError> errorFilter,
GraphQlTransport transport, @Nullable Predicate<GraphQlResponseError> errorFilter,
Configuration jsonPathConfig, DocumentSource documentSource, Duration timeout,
Consumer<AbstractGraphQlTesterBuilder<?>> builderInitializer) {
@@ -209,15 +209,15 @@ final class DefaultGraphQlTester implements GraphQlTester {
private final Supplier<String> jsonContent;
private final List<GraphQLError> errors;
private final List<GraphQlResponseError> errors;
private final List<GraphQLError> unexpectedErrors;
private final List<GraphQlResponseError> unexpectedErrors;
private final Consumer<Runnable> assertDecorator;
private ResponseDelegate(
GraphQlResponse response, @Nullable Predicate<GraphQLError> errorFilter,
GraphQlResponse response, @Nullable Predicate<GraphQlResponseError> errorFilter,
Consumer<Runnable> assertDecorator, Configuration jsonPathConfig) {
this.jsonDoc = JsonPath.parse(response.toMap(), jsonPathConfig);
@@ -253,9 +253,9 @@ final class DefaultGraphQlTester implements GraphQlTester {
this.assertDecorator.accept(task);
}
boolean filterErrors(Predicate<GraphQLError> predicate) {
boolean filterErrors(Predicate<GraphQlResponseError> predicate) {
boolean filtered = false;
for (GraphQLError error : this.errors) {
for (GraphQlResponseError error : this.errors) {
if (predicate.test(error)) {
this.unexpectedErrors.remove(error);
filtered = true;
@@ -264,12 +264,12 @@ final class DefaultGraphQlTester implements GraphQlTester {
return filtered;
}
void expectErrors(Predicate<GraphQLError> predicate) {
void expectErrors(Predicate<GraphQlResponseError> predicate) {
boolean filtered = filterErrors(predicate);
this.assertDecorator.accept(() -> AssertionErrors.assertTrue("No matching errors.", filtered));
}
void consumeErrors(Consumer<List<GraphQLError>> consumer) {
void consumeErrors(Consumer<List<GraphQlResponseError>> consumer) {
filterErrors(error -> true);
consumer.accept(this.errors);
}
@@ -293,7 +293,7 @@ final class DefaultGraphQlTester implements GraphQlTester {
private final ResponseDelegate delegate;
private DefaultResponse(
GraphQlResponse response, @Nullable Predicate<GraphQLError> errorFilter,
GraphQlResponse response, @Nullable Predicate<GraphQlResponseError> errorFilter,
Consumer<Runnable> assertDecorator, Configuration jsonPathConfig) {
this.delegate = new ResponseDelegate(response, errorFilter, assertDecorator, jsonPathConfig);
@@ -311,13 +311,13 @@ final class DefaultGraphQlTester implements GraphQlTester {
}
@Override
public Errors filter(Predicate<GraphQLError> predicate) {
public Errors filter(Predicate<GraphQlResponseError> predicate) {
this.delegate.filterErrors(predicate);
return this;
}
@Override
public Errors expect(Predicate<GraphQLError> predicate) {
public Errors expect(Predicate<GraphQlResponseError> predicate) {
this.delegate.expectErrors(predicate);
return this;
}
@@ -329,7 +329,7 @@ final class DefaultGraphQlTester implements GraphQlTester {
}
@Override
public Traversable satisfy(Consumer<List<GraphQLError>> consumer) {
public Traversable satisfy(Consumer<List<GraphQlResponseError>> consumer) {
this.delegate.consumeErrors(consumer);
return this;
}

View File

@@ -21,10 +21,10 @@ import java.util.List;
import java.util.function.Consumer;
import java.util.function.Predicate;
import graphql.GraphQLError;
import reactor.core.publisher.Flux;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.graphql.GraphQlResponseError;
import org.springframework.graphql.client.GraphQlTransport;
import org.springframework.graphql.support.DocumentSource;
import org.springframework.graphql.support.ResourceDocumentSource;
@@ -103,7 +103,7 @@ public interface GraphQlTester {
* @param predicate the error filter to add
* @return the same builder instance
*/
B errorFilter(Predicate<GraphQLError> predicate);
B errorFilter(Predicate<GraphQlResponseError> predicate);
/**
* Configure a {@link DocumentSource} for use with
@@ -448,7 +448,7 @@ public interface GraphQlTester {
* @param errorPredicate the error filter to add
* @return the same spec to add more filters before {@link #verify()}
*/
Errors filter(Predicate<GraphQLError> errorPredicate);
Errors filter(Predicate<GraphQlResponseError> errorPredicate);
/**
* Use this to declare errors that are expected.
@@ -461,7 +461,7 @@ public interface GraphQlTester {
* @param errorPredicate the predicate for the expected error
* @return the same spec to add more filters or expected errors
*/
Errors expect(Predicate<GraphQLError> errorPredicate);
Errors expect(Predicate<GraphQlResponseError> errorPredicate);
/**
* Verify there are either no errors or that there no unexpected errors that have
@@ -477,7 +477,7 @@ public interface GraphQlTester {
* @param errorsConsumer to inspect errors with
* @return a spec to switch to a data path
*/
Traversable satisfy(Consumer<List<GraphQLError>> errorsConsumer);
Traversable satisfy(Consumer<List<GraphQlResponseError>> errorsConsumer);
}