Skip to content

Allow passing Map of variables in GraphQlTester #969

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,12 @@ public DefaultRequest variable(String name, @Nullable Object value) {
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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import java.time.Duration;
import java.util.List;
import java.util.Map;
import java.util.function.Consumer;
import java.util.function.Predicate;

Expand Down Expand Up @@ -153,6 +154,15 @@ interface Request<T extends Request<T>> {
*/
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
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;
Expand Down Expand Up @@ -240,6 +241,39 @@ void operationNameAndVariables() {
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}}";
Expand Down