From 5682740dee4cae9c266d8e87460e26cc9ea7f820 Mon Sep 17 00:00:00 2001 From: rstoyanchev Date: Mon, 3 Mar 2025 10:54:32 +0000 Subject: [PATCH] GraphQlTester provides access to underlying response Closes gh-1131 --- .../graphql/test/tester/DefaultGraphQlTester.java | 8 ++++++++ .../graphql/test/tester/GraphQlTester.java | 10 +++++++++- .../graphql/test/tester/GraphQlTesterTests.java | 13 ++++++++++++- 3 files changed, 29 insertions(+), 2 deletions(-) diff --git a/spring-graphql-test/src/main/java/org/springframework/graphql/test/tester/DefaultGraphQlTester.java b/spring-graphql-test/src/main/java/org/springframework/graphql/test/tester/DefaultGraphQlTester.java index 75c5f600..e538009b 100644 --- a/spring-graphql-test/src/main/java/org/springframework/graphql/test/tester/DefaultGraphQlTester.java +++ b/spring-graphql-test/src/main/java/org/springframework/graphql/test/tester/DefaultGraphQlTester.java @@ -309,12 +309,15 @@ final class DefaultGraphQlTester implements GraphQlTester { */ private static final class DefaultResponse implements Response, Errors { + private final GraphQlResponse response; + private final ResponseDelegate delegate; private DefaultResponse( GraphQlResponse response, @Nullable Predicate errorFilter, Consumer assertDecorator, Configuration jsonPathConfig) { + this.response = response; this.delegate = new ResponseDelegate(response, errorFilter, assertDecorator, jsonPathConfig); } @@ -335,6 +338,11 @@ final class DefaultGraphQlTester implements GraphQlTester { return this; } + @Override + public GraphQlResponse returnResponse() { + return this.response; + } + @Override public Errors filter(Predicate predicate) { this.delegate.filterErrors(predicate); diff --git a/spring-graphql-test/src/main/java/org/springframework/graphql/test/tester/GraphQlTester.java b/spring-graphql-test/src/main/java/org/springframework/graphql/test/tester/GraphQlTester.java index e6d4d4df..61c51113 100644 --- a/spring-graphql-test/src/main/java/org/springframework/graphql/test/tester/GraphQlTester.java +++ b/spring-graphql-test/src/main/java/org/springframework/graphql/test/tester/GraphQlTester.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2024 the original author or authors. + * Copyright 2002-2025 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -25,6 +25,7 @@ import java.util.function.Predicate; import reactor.core.publisher.Flux; import org.springframework.core.ParameterizedTypeReference; +import org.springframework.graphql.GraphQlResponse; import org.springframework.graphql.ResponseError; import org.springframework.graphql.client.GraphQlTransport; import org.springframework.graphql.support.DocumentSource; @@ -262,6 +263,13 @@ public interface GraphQlTester { */ Errors errors(); + + /** + * Return the underlying {@link GraphQlResponse} for direct access. + * @since 1.3.5 + */ + GraphQlResponse returnResponse(); + } /** diff --git a/spring-graphql-test/src/test/java/org/springframework/graphql/test/tester/GraphQlTesterTests.java b/spring-graphql-test/src/test/java/org/springframework/graphql/test/tester/GraphQlTesterTests.java index 869e910c..b45536a1 100644 --- a/spring-graphql-test/src/test/java/org/springframework/graphql/test/tester/GraphQlTesterTests.java +++ b/spring-graphql-test/src/test/java/org/springframework/graphql/test/tester/GraphQlTesterTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2024 the original author or authors. + * Copyright 2002-2025 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -30,6 +30,7 @@ import org.springframework.core.ParameterizedTypeReference; import org.springframework.graphql.ExecutionGraphQlRequest; import org.springframework.graphql.ExecutionGraphQlService; import org.springframework.graphql.GraphQlRequest; +import org.springframework.graphql.GraphQlResponse; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatIllegalStateException; @@ -500,4 +501,14 @@ public class GraphQlTesterTests extends GraphQlTesterTestSupport { assertThat(getActualRequestDocument()).contains(document); } + @Test + void returnGraphQlResponse() { + String document = "{me {name, friends}}"; + getGraphQlService().setDataAsJson(document, "{\"me\": {\"name\":\"Luke Skywalker\", \"friends\":[]}}"); + + GraphQlResponse response = graphQlTester().documentName("me").execute().returnResponse(); + String value = response.field("me.name").getValue(); + assertThat(value).isEqualTo("Luke Skywalker"); + } + }