Skip to content

Commit 5682740

Browse files
committed
GraphQlTester provides access to underlying response
Closes gh-1131
1 parent 5c2164c commit 5682740

File tree

3 files changed

+29
-2
lines changed

3 files changed

+29
-2
lines changed

spring-graphql-test/src/main/java/org/springframework/graphql/test/tester/DefaultGraphQlTester.java

+8
Original file line numberDiff line numberDiff line change
@@ -309,12 +309,15 @@ void verifyErrors() {
309309
*/
310310
private static final class DefaultResponse implements Response, Errors {
311311

312+
private final GraphQlResponse response;
313+
312314
private final ResponseDelegate delegate;
313315

314316
private DefaultResponse(
315317
GraphQlResponse response, @Nullable Predicate<ResponseError> errorFilter,
316318
Consumer<Runnable> assertDecorator, Configuration jsonPathConfig) {
317319

320+
this.response = response;
318321
this.delegate = new ResponseDelegate(response, errorFilter, assertDecorator, jsonPathConfig);
319322
}
320323

@@ -335,6 +338,11 @@ public Errors errors() {
335338
return this;
336339
}
337340

341+
@Override
342+
public GraphQlResponse returnResponse() {
343+
return this.response;
344+
}
345+
338346
@Override
339347
public Errors filter(Predicate<ResponseError> predicate) {
340348
this.delegate.filterErrors(predicate);

spring-graphql-test/src/main/java/org/springframework/graphql/test/tester/GraphQlTester.java

+9-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2024 the original author or authors.
2+
* Copyright 2002-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -25,6 +25,7 @@
2525
import reactor.core.publisher.Flux;
2626

2727
import org.springframework.core.ParameterizedTypeReference;
28+
import org.springframework.graphql.GraphQlResponse;
2829
import org.springframework.graphql.ResponseError;
2930
import org.springframework.graphql.client.GraphQlTransport;
3031
import org.springframework.graphql.support.DocumentSource;
@@ -262,6 +263,13 @@ interface Response extends Traversable {
262263
*/
263264
Errors errors();
264265

266+
267+
/**
268+
* Return the underlying {@link GraphQlResponse} for direct access.
269+
* @since 1.3.5
270+
*/
271+
GraphQlResponse returnResponse();
272+
265273
}
266274

267275
/**

spring-graphql-test/src/test/java/org/springframework/graphql/test/tester/GraphQlTesterTests.java

+12-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2024 the original author or authors.
2+
* Copyright 2002-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -30,6 +30,7 @@
3030
import org.springframework.graphql.ExecutionGraphQlRequest;
3131
import org.springframework.graphql.ExecutionGraphQlService;
3232
import org.springframework.graphql.GraphQlRequest;
33+
import org.springframework.graphql.GraphQlResponse;
3334

3435
import static org.assertj.core.api.Assertions.assertThat;
3536
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
@@ -500,4 +501,14 @@ void errorsConsumed() {
500501
assertThat(getActualRequestDocument()).contains(document);
501502
}
502503

504+
@Test
505+
void returnGraphQlResponse() {
506+
String document = "{me {name, friends}}";
507+
getGraphQlService().setDataAsJson(document, "{\"me\": {\"name\":\"Luke Skywalker\", \"friends\":[]}}");
508+
509+
GraphQlResponse response = graphQlTester().documentName("me").execute().returnResponse();
510+
String value = response.field("me.name").getValue();
511+
assertThat(value).isEqualTo("Luke Skywalker");
512+
}
513+
503514
}

0 commit comments

Comments
 (0)