Extract GraphQlRequest and rename "query" to "document"

Extract GraphQlRequest, as a parent of RequestInput, that holds the
actual request inputs sent from the client side. RequestInput then
adds server-side transport details and ExecutionInput support.

Also replace "query" with "document" in GraphQlRequest and in
GraphQlClient.

Closes gh-310
This commit is contained in:
rstoyanchev
2022-03-01 11:21:02 +00:00
parent bc7500f33a
commit b49c63f376
29 changed files with 437 additions and 365 deletions

View File

@@ -68,25 +68,25 @@ public class GraphQlTesterTests {
@Test
void pathAndValueExist() throws Exception {
String query = "{me {name, friends}}";
String document = "{me {name, friends}}";
setResponse("{\"me\": {\"name\":\"Luke Skywalker\", \"friends\":[]}}");
GraphQlTester.ResponseSpec spec = this.graphQlTester.query(query).execute();
GraphQlTester.ResponseSpec spec = this.graphQlTester.query(document).execute();
spec.path("me.name").pathExists().valueExists();
spec.path("me.friends").pathExists().valueExists();
spec.path("hero").pathDoesNotExist().valueDoesNotExist();
assertThat(this.inputCaptor.getValue().getQuery()).contains(query);
assertThat(this.inputCaptor.getValue().getDocument()).contains(document);
}
@Test
void valueIsEmpty() throws Exception {
String query = "{me {name, friends}}";
String document = "{me {name, friends}}";
setResponse("{\"me\": {\"name\":null, \"friends\":[]}}");
GraphQlTester.ResponseSpec spec = this.graphQlTester.query(query).execute();
GraphQlTester.ResponseSpec spec = this.graphQlTester.query(document).execute();
spec.path("me.name").valueIsEmpty();
spec.path("me.friends").valueIsEmpty();
@@ -95,16 +95,16 @@ public class GraphQlTesterTests {
.as("Path does not even exist")
.hasMessageContaining("No value at JSON path \"$['data']['hero']");
assertThat(this.inputCaptor.getValue().getQuery()).contains(query);
assertThat(this.inputCaptor.getValue().getDocument()).contains(document);
}
@Test
void matchesJson() throws Exception {
String query = "{me {name}}";
String document = "{me {name}}";
setResponse("{\"me\": {\"name\":\"Luke Skywalker\", \"friends\":[]}}");
GraphQlTester.ResponseSpec spec = this.graphQlTester.query(query).execute();
GraphQlTester.ResponseSpec spec = this.graphQlTester.query(document).execute();
spec.path("").matchesJson("{\"me\": {\"name\":\"Luke Skywalker\",\"friends\":[]}}");
spec.path("me").matchesJson("{\"name\":\"Luke Skywalker\"}");
@@ -115,16 +115,16 @@ public class GraphQlTesterTests {
.as("Extended fields should fail in strict mode")
.hasMessageContaining("Unexpected: name");
assertThat(this.inputCaptor.getValue().getQuery()).contains(query);
assertThat(this.inputCaptor.getValue().getDocument()).contains(document);
}
@Test
void entity() throws Exception {
String query = "{me {name}}";
String document = "{me {name}}";
setResponse("{\"me\": {\"name\":\"Luke Skywalker\"}}");
GraphQlTester.ResponseSpec spec = this.graphQlTester.query(query).execute();
GraphQlTester.ResponseSpec spec = this.graphQlTester.query(document).execute();
MovieCharacter luke = MovieCharacter.create("Luke Skywalker");
MovieCharacter han = MovieCharacter.create("Han Solo");
@@ -145,13 +145,13 @@ public class GraphQlTesterTests {
.entity(new ParameterizedTypeReference<Map<String, MovieCharacter>>() {})
.isEqualTo(Collections.singletonMap("me", luke));
assertThat(this.inputCaptor.getValue().getQuery()).contains(query);
assertThat(this.inputCaptor.getValue().getDocument()).contains(document);
}
@Test
void entityList() throws Exception {
String query = "{me {name, friends}}";
String document = "{me {name, friends}}";
setResponse("{" +
" \"me\":{" +
" \"name\":\"Luke Skywalker\","
@@ -159,7 +159,7 @@ public class GraphQlTesterTests {
" }" +
"}");
GraphQlTester.ResponseSpec spec = this.graphQlTester.query(query).execute();
GraphQlTester.ResponseSpec spec = this.graphQlTester.query(document).execute();
MovieCharacter han = MovieCharacter.create("Han Solo");
MovieCharacter leia = MovieCharacter.create("Leia Organa");
@@ -181,13 +181,13 @@ public class GraphQlTesterTests {
.entityList(new ParameterizedTypeReference<MovieCharacter>() {})
.containsExactly(han, leia);
assertThat(this.inputCaptor.getValue().getQuery()).contains(query);
assertThat(this.inputCaptor.getValue().getDocument()).contains(document);
}
@Test
void operationNameAndVariables() throws Exception {
String query = "query HeroNameAndFriends($episode: Episode) {" +
String document = "query HeroNameAndFriends($episode: Episode) {" +
" hero(episode: $episode) {" +
" name"
+ " }" +
@@ -195,7 +195,7 @@ public class GraphQlTesterTests {
setResponse("{\"hero\": {\"name\":\"R2-D2\"}}");
GraphQlTester.ResponseSpec spec = this.graphQlTester.query(query)
GraphQlTester.ResponseSpec spec = this.graphQlTester.query(document)
.operationName("HeroNameAndFriends")
.variable("episode", "JEDI")
.variable("foo", "bar")
@@ -205,7 +205,7 @@ public class GraphQlTesterTests {
spec.path("hero").entity(MovieCharacter.class).isEqualTo(MovieCharacter.create("R2-D2"));
RequestInput input = this.inputCaptor.getValue();
assertThat(input.getQuery()).contains(query);
assertThat(input.getDocument()).contains(document);
assertThat(input.getOperationName()).isEqualTo("HeroNameAndFriends");
assertThat(input.getVariables()).hasSize(3);
assertThat(input.getVariables()).containsEntry("episode", "JEDI");
@@ -216,55 +216,55 @@ public class GraphQlTesterTests {
@Test
void errorsCheckedOnExecuteAndVerify() throws Exception {
String query = "{me {name, friends}}";
String document = "{me {name, friends}}";
setResponse(GraphqlErrorBuilder.newError().message("Invalid query").build());
assertThatThrownBy(() -> this.graphQlTester.query(query).executeAndVerify())
assertThatThrownBy(() -> this.graphQlTester.query(document).executeAndVerify())
.hasMessageContaining("Response has 1 unexpected error(s).");
assertThat(this.inputCaptor.getValue().getQuery()).contains(query);
assertThat(this.inputCaptor.getValue().getDocument()).contains(document);
}
@Test
void errorsCheckedOnTraverse() throws Exception {
String query = "{me {name, friends}}";
String document = "{me {name, friends}}";
setResponse(GraphqlErrorBuilder.newError().message("Invalid query").build());
assertThatThrownBy(() -> this.graphQlTester.query(query).execute().path("me"))
assertThatThrownBy(() -> this.graphQlTester.query(document).execute().path("me"))
.hasMessageContaining("Response has 1 unexpected error(s).");
assertThat(this.inputCaptor.getValue().getQuery()).contains(query);
assertThat(this.inputCaptor.getValue().getDocument()).contains(document);
}
@Test
void errorsPartiallyFiltered() throws Exception {
String query = "{me {name, friends}}";
String document = "{me {name, friends}}";
setResponse(
GraphqlErrorBuilder.newError().message("some error").build(),
GraphqlErrorBuilder.newError().message("some other error").build());
assertThatThrownBy(() ->
this.graphQlTester.query(query)
this.graphQlTester.query(document)
.execute()
.errors()
.filter((error) -> error.getMessage().equals("some error"))
.verify())
.hasMessageContaining("Response has 1 unexpected error(s) of 2 total.");
assertThat(this.inputCaptor.getValue().getQuery()).contains(query);
assertThat(this.inputCaptor.getValue().getDocument()).contains(document);
}
@Test
void errorsFiltered() throws Exception {
String query = "{me {name, friends}}";
String document = "{me {name, friends}}";
setResponse(
GraphqlErrorBuilder.newError().message("some error").build(),
GraphqlErrorBuilder.newError().message("some other error").build());
this.graphQlTester.query(query)
this.graphQlTester.query(document)
.execute()
.errors()
.filter((error) -> error.getMessage().startsWith("some "))
@@ -272,13 +272,13 @@ public class GraphQlTesterTests {
.path("me")
.pathDoesNotExist();
assertThat(this.inputCaptor.getValue().getQuery()).contains(query);
assertThat(this.inputCaptor.getValue().getDocument()).contains(document);
}
@Test
void errorsFilteredGlobally() throws Exception {
String query = "{me {name, friends}}";
String document = "{me {name, friends}}";
setResponse(
GraphqlErrorBuilder.newError().message("some error").build(),
GraphqlErrorBuilder.newError().message("some other error").build());
@@ -286,25 +286,25 @@ public class GraphQlTesterTests {
GraphQlTester.builder(this.service)
.errorFilter((error) -> error.getMessage().startsWith("some "))
.build()
.query(query)
.query(document)
.execute()
.errors()
.verify()
.path("me")
.pathDoesNotExist();
assertThat(this.inputCaptor.getValue().getQuery()).contains(query);
assertThat(this.inputCaptor.getValue().getDocument()).contains(document);
}
@Test
void errorsExpected() throws Exception {
String query = "{me {name, friends}}";
String document = "{me {name, friends}}";
setResponse(
GraphqlErrorBuilder.newError().message("some error").build(),
GraphqlErrorBuilder.newError().message("some other error").build());
this.graphQlTester.query(query)
this.graphQlTester.query(document)
.execute()
.errors()
.expect((error) -> error.getMessage().startsWith("some "))
@@ -312,19 +312,19 @@ public class GraphQlTesterTests {
.path("me")
.pathDoesNotExist();
assertThat(this.inputCaptor.getValue().getQuery()).contains(query);
assertThat(this.inputCaptor.getValue().getDocument()).contains(document);
}
@Test
void errorsExpectedButNotFound() throws Exception {
String query = "{me {name, friends}}";
String document = "{me {name, friends}}";
setResponse(
GraphqlErrorBuilder.newError().message("some error").build(),
GraphqlErrorBuilder.newError().message("some other error").build());
assertThatThrownBy(() ->
this.graphQlTester.query(query)
this.graphQlTester.query(document)
.execute()
.errors().expect((error) -> error.getMessage().startsWith("another ")))
.hasMessageStartingWith("No matching errors.");
@@ -333,13 +333,13 @@ public class GraphQlTesterTests {
@Test
void errorsConsumed() throws Exception {
String query = "{me {name, friends}}";
String document = "{me {name, friends}}";
setResponse(GraphqlErrorBuilder.newError()
.message("Invalid query")
.location(new SourceLocation(1, 2))
.build());
this.graphQlTester.query(query)
this.graphQlTester.query(document)
.execute()
.errors()
.satisfy((errors) -> {
@@ -352,7 +352,7 @@ public class GraphQlTesterTests {
.path("me")
.pathDoesNotExist();
assertThat(this.inputCaptor.getValue().getQuery()).contains(query);
assertThat(this.inputCaptor.getValue().getDocument()).contains(document);
}
private void setResponse(String data) throws Exception {

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2021 the original author or authors.
* Copyright 2002-2022 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.
@@ -91,7 +91,7 @@ public class WebGraphQlTesterTests {
spec.path("me.name").entity(String.class).isEqualTo("Luke Skywalker");
setup.verifyRequest((input) -> {
assertThat(input.getQuery()).contains(query);
assertThat(input.getDocument()).contains(query);
assertThat(input.getHeaders().get("myHeader1")).containsExactly("myValue1a", "myValue1b");
assertThat(input.getHeaders().getFirst("myHeader2")).isEqualTo("myValue2");
});
@@ -116,7 +116,7 @@ public class WebGraphQlTesterTests {
spec.path("me.name").entity(String.class).isEqualTo("Luke Skywalker");
setup.verifyRequest((input) -> {
assertThat(input.getQuery()).contains(query);
assertThat(input.getDocument()).contains(query);
assertThat(input.getHeaders().get("myHeader1")).containsExactly("myValue1a", "myValue1b");
assertThat(input.getHeaders().getFirst("myHeader2")).isEqualTo("myValue2");
});
@@ -143,7 +143,7 @@ public class WebGraphQlTesterTests {
.path("me")
.pathDoesNotExist();
setup.verifyRequest((input) -> assertThat(input.getQuery()).contains(query));
setup.verifyRequest((input) -> assertThat(input.getDocument()).contains(query));
}