Skip to content

Commit

Permalink
Polishing contribution
Browse files Browse the repository at this point in the history
Closes gh-969
  • Loading branch information
rstoyanchev committed May 21, 2024
1 parent 1318ca4 commit 4d2cde8
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -145,8 +145,8 @@ public DefaultRequest variable(String name, @Nullable Object value) {
}

@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;
}

Expand All @@ -159,7 +159,9 @@ public DefaultRequest extension(String name, @Nullable Object value) {
@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
Expand All @@ -169,15 +171,18 @@ public void executeAndVerify() {

@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() {
return new DefaultGraphQlRequest(this.document, this.operationName, this.variables, this.extensions);
}

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) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -149,19 +149,19 @@ interface Request<T extends Request<T>> {
* 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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
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;
Expand Down Expand Up @@ -197,13 +198,13 @@ void entityList() {
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
Expand All @@ -215,11 +216,12 @@ void nestedPath() {
@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\"}}");

Expand All @@ -242,36 +244,24 @@ void operationNameAndVariables() {
}

@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<>();

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();
Map<String, Object> vars = Map.of(
"episode", Optional.of("JEDI"), "foo", Optional.of("bar"), "keyOnly", Optional.empty());

response.path("hero").entity(MovieCharacter.class).isEqualTo(MovieCharacter.create("R2-D2"));
graphQlTester().document(document).variables(vars).execute();

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
Expand Down

0 comments on commit 4d2cde8

Please sign in to comment.