Polishing contribution
Closes gh-969
This commit is contained in:
@@ -145,8 +145,8 @@ final class DefaultGraphQlTester implements GraphQlTester {
|
||||
}
|
||||
|
||||
@Override
|
||||
public DefaultRequest variable(Map<String, Object> values) {
|
||||
this.variables.putAll(values);
|
||||
public DefaultRequest variables(Map<String, Object> variables) {
|
||||
this.variables.putAll(variables);
|
||||
return this;
|
||||
}
|
||||
|
||||
@@ -159,7 +159,9 @@ final class DefaultGraphQlTester implements GraphQlTester {
|
||||
@SuppressWarnings("ConstantConditions")
|
||||
@Override
|
||||
public Response execute() {
|
||||
return DefaultGraphQlTester.this.transport.execute(request()).map((response) -> mapResponse(response, request())).block(DefaultGraphQlTester.this.responseTimeout);
|
||||
return DefaultGraphQlTester.this.transport.execute(request())
|
||||
.map((response) -> mapResponse(response, request()))
|
||||
.block(DefaultGraphQlTester.this.responseTimeout);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -169,7 +171,8 @@ final class DefaultGraphQlTester implements GraphQlTester {
|
||||
|
||||
@Override
|
||||
public Subscription executeSubscription() {
|
||||
return () -> DefaultGraphQlTester.this.transport.executeSubscription(request()).map((result) -> mapResponse(result, request()));
|
||||
return () -> DefaultGraphQlTester.this.transport.executeSubscription(request())
|
||||
.map((result) -> mapResponse(result, request()));
|
||||
}
|
||||
|
||||
private GraphQlRequest request() {
|
||||
@@ -177,7 +180,9 @@ final class DefaultGraphQlTester implements GraphQlTester {
|
||||
}
|
||||
|
||||
private DefaultResponse mapResponse(GraphQlResponse response, GraphQlRequest request) {
|
||||
return new DefaultResponse(response, DefaultGraphQlTester.this.errorFilter, assertDecorator(request), DefaultGraphQlTester.this.jsonPathConfig);
|
||||
return new DefaultResponse(
|
||||
response, DefaultGraphQlTester.this.errorFilter,
|
||||
assertDecorator(request), DefaultGraphQlTester.this.jsonPathConfig);
|
||||
}
|
||||
|
||||
private Consumer<Runnable> assertDecorator(GraphQlRequest request) {
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2023 the original author or authors.
|
||||
* Copyright 2002-2024 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.
|
||||
@@ -149,19 +149,19 @@ public interface GraphQlTester {
|
||||
* Add a variable.
|
||||
* @param name the variable name
|
||||
* @param value the variable value, possibly {@code null} since GraphQL
|
||||
* supports providing null value vs not providing a value at all.
|
||||
* supports providing null for a value vs not providing a value at all.
|
||||
* @return this request spec
|
||||
*/
|
||||
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.
|
||||
* Add variables from a {@link Map}.
|
||||
* @param values the variables to add, possibly with {@code null} values
|
||||
* since GraphQL supports null for a value vs not providing it at all.
|
||||
* @return this request spec
|
||||
* @since 1.3.0
|
||||
*/
|
||||
T variable(Map<String, Object> values);
|
||||
T variables(Map<String, Object> values);
|
||||
|
||||
/**
|
||||
* Add a value for a protocol extension.
|
||||
|
||||
@@ -29,6 +29,7 @@ import org.junit.jupiter.api.Test;
|
||||
import org.springframework.core.ParameterizedTypeReference;
|
||||
import org.springframework.graphql.ExecutionGraphQlRequest;
|
||||
import org.springframework.graphql.ExecutionGraphQlService;
|
||||
import org.springframework.graphql.GraphQlRequest;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.assertThatThrownBy;
|
||||
@@ -197,13 +198,13 @@ public class GraphQlTesterTests extends GraphQlTesterTestSupport {
|
||||
void nestedPath() {
|
||||
|
||||
String document = "{me {name, friends}}";
|
||||
getGraphQlService().setDataAsJson(document,
|
||||
"{" +
|
||||
" \"me\":{" +
|
||||
" \"name\":\"Luke Skywalker\","
|
||||
+ " \"friends\":[{\"name\":\"Han Solo\"}, {\"name\":\"Leia Organa\"}]" +
|
||||
" }" +
|
||||
"}");
|
||||
getGraphQlService().setDataAsJson(document, """
|
||||
{
|
||||
"me":{
|
||||
"name":"Luke Skywalker",
|
||||
"friends":[{"name":"Han Solo"}, {"name":"Leia Organa"}]
|
||||
}
|
||||
}""");
|
||||
|
||||
graphQlTester().document(document).execute()
|
||||
.path("me", me -> me
|
||||
@@ -215,11 +216,12 @@ public class GraphQlTesterTests extends GraphQlTesterTestSupport {
|
||||
@Test
|
||||
void operationNameAndVariables() {
|
||||
|
||||
String document = "query HeroNameAndFriends($episode: Episode) {" +
|
||||
" hero(episode: $episode) {" +
|
||||
" name"
|
||||
+ " }" +
|
||||
"}";
|
||||
String document = """
|
||||
query HeroNameAndFriends($episode: Episode) {
|
||||
hero(episode: $episode) {
|
||||
name
|
||||
}
|
||||
}""";
|
||||
|
||||
getGraphQlService().setDataAsJson(document, "{\"hero\": {\"name\":\"R2-D2\"}}");
|
||||
|
||||
@@ -242,36 +244,24 @@ public class GraphQlTesterTests extends GraphQlTesterTestSupport {
|
||||
}
|
||||
|
||||
@Test
|
||||
void operationNameAndVariablesAsMap() {
|
||||
void variablesAsMap() {
|
||||
|
||||
String document = "query HeroNameAndFriends($episode: Episode) {" +
|
||||
" hero(episode: $episode) {" +
|
||||
" name"
|
||||
+ " }" +
|
||||
"}";
|
||||
String document = """
|
||||
query HeroNameAndFriends($episode: Episode) {
|
||||
hero(episode: $episode) {
|
||||
name
|
||||
}
|
||||
}""";
|
||||
|
||||
getGraphQlService().setDataAsJson(document, "{\"hero\": {\"name\":\"R2-D2\"}}");
|
||||
|
||||
Map<String, Object> variableMap = new LinkedHashMap<>();
|
||||
Map<String, Object> vars = Map.of(
|
||||
"episode", Optional.of("JEDI"), "foo", Optional.of("bar"), "keyOnly", Optional.empty());
|
||||
|
||||
variableMap.put("episode", Optional.of("JEDI"));
|
||||
variableMap.put("foo", Optional.of("bar"));
|
||||
variableMap.put("keyOnly", Optional.ofNullable(null));
|
||||
graphQlTester().document(document).variables(vars).execute();
|
||||
|
||||
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));
|
||||
GraphQlRequest request = getGraphQlService().getGraphQlRequest();
|
||||
assertThat(request.getVariables()).containsExactlyEntriesOf(vars);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
Reference in New Issue
Block a user