Skip to content

Commit 1318ca4

Browse files
kutluerenrstoyanchev
authored andcommitted
Allow Map of variables in GraphQlTester
See gh-969
1 parent 987f88d commit 1318ca4

File tree

3 files changed

+50
-0
lines changed

3 files changed

+50
-0
lines changed

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

+6
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,12 @@ public DefaultRequest variable(String name, @Nullable Object value) {
144144
return this;
145145
}
146146

147+
@Override
148+
public DefaultRequest variable(Map<String, Object> values) {
149+
this.variables.putAll(values);
150+
return this;
151+
}
152+
147153
@Override
148154
public DefaultRequest extension(String name, @Nullable Object value) {
149155
this.extensions.put(name, value);

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

+10
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import java.time.Duration;
2020
import java.util.List;
21+
import java.util.Map;
2122
import java.util.function.Consumer;
2223
import java.util.function.Predicate;
2324

@@ -153,6 +154,15 @@ interface Request<T extends Request<T>> {
153154
*/
154155
T variable(String name, @Nullable Object value);
155156

157+
/**
158+
* Add variables.
159+
* @param values the variables to be set
160+
* the variable of the values, possibly {@code null} since GraphQL
161+
* supports providing null value vs not providing a value at all.
162+
* @return this request spec
163+
*/
164+
T variable(Map<String, Object> values);
165+
156166
/**
157167
* Add a value for a protocol extension.
158168
* @param name the protocol extension name

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

+34
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import java.util.Collections;
2020
import java.util.List;
2121
import java.util.Map;
22+
import java.util.Optional;
2223
import java.util.concurrent.atomic.AtomicReference;
2324

2425
import graphql.GraphqlErrorBuilder;
@@ -240,6 +241,39 @@ void operationNameAndVariables() {
240241
assertThat(request.getVariables()).containsEntry("keyOnly", null);
241242
}
242243

244+
@Test
245+
void operationNameAndVariablesAsMap() {
246+
247+
String document = "query HeroNameAndFriends($episode: Episode) {" +
248+
" hero(episode: $episode) {" +
249+
" name"
250+
+ " }" +
251+
"}";
252+
253+
getGraphQlService().setDataAsJson(document, "{\"hero\": {\"name\":\"R2-D2\"}}");
254+
255+
Map<String, Object> variableMap = new LinkedHashMap<>();
256+
257+
variableMap.put("episode", Optional.of("JEDI"));
258+
variableMap.put("foo", Optional.of("bar"));
259+
variableMap.put("keyOnly", Optional.ofNullable(null));
260+
261+
GraphQlTester.Response response = graphQlTester().document(document)
262+
.operationName("HeroNameAndFriends")
263+
.variable(variableMap)
264+
.execute();
265+
266+
response.path("hero").entity(MovieCharacter.class).isEqualTo(MovieCharacter.create("R2-D2"));
267+
268+
ExecutionGraphQlRequest request = getGraphQlService().getGraphQlRequest();
269+
assertThat(request.getDocument()).contains(document);
270+
assertThat(request.getOperationName()).isEqualTo("HeroNameAndFriends");
271+
assertThat(request.getVariables()).hasSize(3);
272+
assertThat(request.getVariables()).containsEntry("episode", Optional.of("JEDI"));
273+
assertThat(request.getVariables()).containsEntry("foo", Optional.of("bar"));
274+
assertThat(request.getVariables()).containsEntry("keyOnly", Optional.ofNullable(null));
275+
}
276+
243277
@Test
244278
void protocolExtensions() {
245279
String document = "{me {name, friends}}";

0 commit comments

Comments
 (0)