Expose map of "extensions" in GraphQlRequest
This commit adds support for protocol, vendor specific protocol extensions in the GraphQL client requests. Closes gh-371
This commit is contained in:
@@ -75,7 +75,7 @@ abstract class AbstractDirectGraphQlTransport implements GraphQlTransport {
|
||||
|
||||
private ExecutionGraphQlRequest toExecutionRequest(GraphQlRequest request) {
|
||||
return new DefaultExecutionGraphQlRequest(
|
||||
request.getDocument(), request.getOperationName(), request.getVariables(),
|
||||
request.getDocument(), request.getOperationName(), request.getVariables(), request.getExtensions(),
|
||||
idGenerator.generateId().toString(), null);
|
||||
}
|
||||
|
||||
|
||||
@@ -125,6 +125,8 @@ final class DefaultGraphQlTester implements GraphQlTester {
|
||||
|
||||
private final Map<String, Object> variables = new LinkedHashMap<>();
|
||||
|
||||
private final Map<String, Object> extensions = new LinkedHashMap<>();
|
||||
|
||||
private DefaultRequest(String document) {
|
||||
Assert.notNull(document, "`document` is required");
|
||||
this.document = document;
|
||||
@@ -142,6 +144,12 @@ final class DefaultGraphQlTester implements GraphQlTester {
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public DefaultRequest extension(String name, Object value) {
|
||||
this.extensions.put(name, value);
|
||||
return this;
|
||||
}
|
||||
|
||||
@SuppressWarnings("ConstantConditions")
|
||||
@Override
|
||||
public Response execute() {
|
||||
@@ -159,7 +167,7 @@ final class DefaultGraphQlTester implements GraphQlTester {
|
||||
}
|
||||
|
||||
private GraphQlRequest request() {
|
||||
return new DefaultGraphQlRequest(this.document, this.operationName, this.variables);
|
||||
return new DefaultGraphQlRequest(this.document, this.operationName, this.variables, this.extensions);
|
||||
}
|
||||
|
||||
private DefaultResponse mapResponse(GraphQlResponse response, GraphQlRequest request) {
|
||||
|
||||
@@ -149,6 +149,15 @@ public interface GraphQlTester {
|
||||
*/
|
||||
T variable(String name, @Nullable Object value);
|
||||
|
||||
/**
|
||||
* 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.
|
||||
* @return this request spec
|
||||
*/
|
||||
T extension(String name, @Nullable Object value);
|
||||
|
||||
/**
|
||||
* Execute the GraphQL request and return a spec for further inspection of
|
||||
* response data and errors.
|
||||
|
||||
@@ -217,6 +217,23 @@ public class GraphQlTesterTests extends GraphQlTesterTestSupport {
|
||||
assertThat(request.getVariables()).containsEntry("keyOnly", null);
|
||||
}
|
||||
|
||||
@Test
|
||||
void protocolExtensions() {
|
||||
String document = "{me {name, friends}}";
|
||||
getGraphQlService().setDataAsJson(document, "{\"me\": {\"name\":\"Luke Skywalker\", \"friends\":[]}}");
|
||||
|
||||
graphQlTester().document(document)
|
||||
.extension("firstExt", Collections.singletonMap("key", "value"))
|
||||
.extension("secondExt", "value")
|
||||
.execute();
|
||||
|
||||
ExecutionGraphQlRequest request = getGraphQlService().getGraphQlRequest();
|
||||
assertThat(request.getDocument()).contains(document);
|
||||
assertThat(request.getExtensions()).hasSize(2);
|
||||
assertThat(request.getExtensions()).containsEntry("firstExt", Collections.singletonMap("key", "value"))
|
||||
.containsEntry("secondExt", "value");
|
||||
}
|
||||
|
||||
@Test
|
||||
void errorsEmptyOnExecuteAndVerify() {
|
||||
|
||||
|
||||
Reference in New Issue
Block a user