Allow Map of variables in GraphQlTester

See gh-969
This commit is contained in:
kutlueren
2024-05-15 16:20:31 +02:00
committed by rstoyanchev
parent 987f88d22d
commit 1318ca4316
3 changed files with 50 additions and 0 deletions

View File

@@ -144,6 +144,12 @@ final class DefaultGraphQlTester implements GraphQlTester {
return this;
}
@Override
public DefaultRequest variable(Map<String, Object> values) {
this.variables.putAll(values);
return this;
}
@Override
public DefaultRequest extension(String name, @Nullable Object value) {
this.extensions.put(name, value);

View File

@@ -18,6 +18,7 @@ package org.springframework.graphql.test.tester;
import java.time.Duration;
import java.util.List;
import java.util.Map;
import java.util.function.Consumer;
import java.util.function.Predicate;
@@ -153,6 +154,15 @@ public interface GraphQlTester {
*/
T variable(String name, @Nullable Object value);
/**
* Add variables.
* @param values the variables to be set
* the variable of the values, possibly {@code null} since GraphQL
* supports providing null value vs not providing a value at all.
* @return this request spec
*/
T variable(Map<String, Object> values);
/**
* Add a value for a protocol extension.
* @param name the protocol extension name

View File

@@ -19,6 +19,7 @@ package org.springframework.graphql.test.tester;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.atomic.AtomicReference;
import graphql.GraphqlErrorBuilder;
@@ -240,6 +241,39 @@ public class GraphQlTesterTests extends GraphQlTesterTestSupport {
assertThat(request.getVariables()).containsEntry("keyOnly", null);
}
@Test
void operationNameAndVariablesAsMap() {
String document = "query HeroNameAndFriends($episode: Episode) {" +
" hero(episode: $episode) {" +
" name"
+ " }" +
"}";
getGraphQlService().setDataAsJson(document, "{\"hero\": {\"name\":\"R2-D2\"}}");
Map<String, Object> variableMap = new LinkedHashMap<>();
variableMap.put("episode", Optional.of("JEDI"));
variableMap.put("foo", Optional.of("bar"));
variableMap.put("keyOnly", Optional.ofNullable(null));
GraphQlTester.Response response = graphQlTester().document(document)
.operationName("HeroNameAndFriends")
.variable(variableMap)
.execute();
response.path("hero").entity(MovieCharacter.class).isEqualTo(MovieCharacter.create("R2-D2"));
ExecutionGraphQlRequest request = getGraphQlService().getGraphQlRequest();
assertThat(request.getDocument()).contains(document);
assertThat(request.getOperationName()).isEqualTo("HeroNameAndFriends");
assertThat(request.getVariables()).hasSize(3);
assertThat(request.getVariables()).containsEntry("episode", Optional.of("JEDI"));
assertThat(request.getVariables()).containsEntry("foo", Optional.of("bar"));
assertThat(request.getVariables()).containsEntry("keyOnly", Optional.ofNullable(null));
}
@Test
void protocolExtensions() {
String document = "{me {name, friends}}";