Skip to content
This repository has been archived by the owner on Sep 26, 2019. It is now read-only.

GraphQL library upgrade changes. #1508

Merged
merged 3 commits into from
May 29, 2019
Merged
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 @@ -37,6 +37,7 @@
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Splitter;
import com.google.common.collect.Iterables;
import com.google.common.net.MediaType;
import graphql.ExecutionInput;
import graphql.ExecutionResult;
import graphql.GraphQL;
Expand Down Expand Up @@ -65,6 +66,7 @@ public class GraphQLHttpService {

private static final InetSocketAddress EMPTY_SOCKET_ADDRESS = new InetSocketAddress("0.0.0.0", 0);
private static final String APPLICATION_JSON = "application/json";
private static final MediaType MEDIA_TYPE_JUST_JSON = MediaType.JSON_UTF_8.withoutParameters();
private static final String EMPTY_RESPONSE = "";

private static final TypeReference<Map<String, Object>> MAP_TYPE =
Expand Down Expand Up @@ -263,14 +265,19 @@ private void handleGraphQLRequest(final RoutingContext routingContext) {
}
break;
case POST:
if (request.getHeader(HttpHeaders.CONTENT_TYPE).equalsIgnoreCase(APPLICATION_JSON)) {

final String contentType = request.getHeader(HttpHeaders.CONTENT_TYPE);
if (contentType != null && MediaType.parse(contentType).is(MEDIA_TYPE_JUST_JSON)) {
final String requestBody = routingContext.getBodyAsString().trim();
final GraphQLJsonRequest jsonRequest =
Json.decodeValue(requestBody, GraphQLJsonRequest.class);
query = jsonRequest.getQuery();
operationName = jsonRequest.getOperationName();
variables = jsonRequest.getVariables();
Map<String, Object> jsonVariables = jsonRequest.getVariables();
if (jsonVariables != null) {
variables = jsonVariables;
} else {
variables = Collections.emptyMap();
}
} else {
// treat all else as application/graphql
query = routingContext.getBodyAsString().trim();
Expand Down Expand Up @@ -298,7 +305,7 @@ private void handleGraphQLRequest(final RoutingContext routingContext) {
},
false,
(res) -> {
response.putHeader("Content-Type", APPLICATION_JSON);
response.putHeader("Content-Type", MediaType.JSON_UTF_8.toString());
if (res.failed()) {
response.setStatusCode(HttpResponseStatus.INTERNAL_SERVER_ERROR.code());
response.end(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ public abstract class AbstractEthGraphQLHttpServiceTest {
String baseUrl;

final MediaType JSON = MediaType.parse("application/json; charset=utf-8");
protected static final MediaType GRAPHQL = MediaType.parse("application/graphql; charset=utf-8");

private MutableBlockchain blockchain;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ private void graphQLCall(final String name) throws IOException {
EthGraphQLHttpBySpecTest.class.getResource(testSpecFile), Charsets.UTF_8);
final JsonObject spec = new JsonObject(json);
final String rawRequestBody = spec.getString("request");
final RequestBody requestBody = RequestBody.create(JSON, rawRequestBody);
final RequestBody requestBody = RequestBody.create(GRAPHQL, rawRequestBody);
final Request request = new Request.Builder().post(requestBody).url(baseUrl).build();

importBlocks(1, BLOCKS.size());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public class GraphQLHttpServiceHostWhitelistTest {
private static GraphQLHttpService service;
private static OkHttpClient client;
private static String baseUrl;
private static final MediaType JSON = MediaType.parse("application/json; charset=utf-8");
protected static final MediaType GRAPHQL = MediaType.parse("application/graphql; charset=utf-8");
private final GraphQLConfiguration graphQLConfig = createGraphQLConfig();
private final List<String> hostsWhitelist = Arrays.asList("ally", "friend");

Expand Down Expand Up @@ -135,7 +135,7 @@ public void requestWithUnknownHostIsRejected() throws IOException {
}

private int doRequest(final String hostname) throws IOException {
final RequestBody body = RequestBody.create(JSON, "{protocolVersion}");
final RequestBody body = RequestBody.create(GRAPHQL, "{protocolVersion}");

final Request build =
new Request.Builder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ public class GraphQLHttpServiceTest {
private static OkHttpClient client;
private static String baseUrl;
protected static final MediaType JSON = MediaType.parse("application/json; charset=utf-8");
protected static final MediaType GRAPHQL = MediaType.parse("application/graphql; charset=utf-8");
private static BlockchainQuery blockchainQueries;
private static Synchronizer synchronizer;
private static GraphQL graphQL;
Expand Down Expand Up @@ -165,18 +166,62 @@ public void handleEmptyRequest() throws Exception {

@Test
public void handleInvalidQuerySchema() throws Exception {
final RequestBody body = RequestBody.create(JSON, "{gasPrice1}");
final RequestBody body = RequestBody.create(GRAPHQL, "{gasPrice1}");

try (final Response resp = client.newCall(buildPostRequest(body)).execute()) {
// assertThat(resp.code()).isEqualTo(200); // Check general format of result
final JsonObject json = new JsonObject(resp.body().string());
testHelper.assertValidGraphQLError(json); // Check result final
testHelper.assertValidGraphQLError(json);
assertThat(resp.code()).isEqualTo(400);
}
}

@Test
public void getGasprice() throws Exception {
final RequestBody body = RequestBody.create(JSON, "{gasPrice}");
public void query_get() throws Exception {
final Wei price = Wei.of(16);
when(miningCoordinatorMock.getMinTransactionGasPrice()).thenReturn(price);

try (final Response resp = client.newCall(buildGetRequest("?query={gasPrice}")).execute()) {
assertThat(resp.code()).isEqualTo(200);
final JsonObject json = new JsonObject(resp.body().string());
testHelper.assertValidGraphQLResult(json);
final String result = json.getJsonObject("data").getString("gasPrice");
assertThat(result).isEqualTo("0x10");
}
}

@Test
public void query_jsonPost() throws Exception {
final RequestBody body = RequestBody.create(JSON, "{\"query\":\"{gasPrice}\"}");
final Wei price = Wei.of(16);
when(miningCoordinatorMock.getMinTransactionGasPrice()).thenReturn(price);

try (final Response resp = client.newCall(buildPostRequest(body)).execute()) {
assertThat(resp.code()).isEqualTo(200); // Check general format of result
final JsonObject json = new JsonObject(resp.body().string());
testHelper.assertValidGraphQLResult(json);
final String result = json.getJsonObject("data").getString("gasPrice");
assertThat(result).isEqualTo("0x10");
}
}

@Test
public void query_graphqlPost() throws Exception {
final RequestBody body = RequestBody.create(GRAPHQL, "{gasPrice}");
final Wei price = Wei.of(16);
when(miningCoordinatorMock.getMinTransactionGasPrice()).thenReturn(price);

try (final Response resp = client.newCall(buildPostRequest(body)).execute()) {
assertThat(resp.code()).isEqualTo(200); // Check general format of result
final JsonObject json = new JsonObject(resp.body().string());
testHelper.assertValidGraphQLResult(json);
final String result = json.getJsonObject("data").getString("gasPrice");
assertThat(result).isEqualTo("0x10");
}
}

@Test
public void query_untypedPost() throws Exception {
final RequestBody body = RequestBody.create(null, "{gasPrice}");
final Wei price = Wei.of(16);
when(miningCoordinatorMock.getMinTransactionGasPrice()).thenReturn(price);

Expand Down Expand Up @@ -226,10 +271,10 @@ public void getSocketAddressWhenBindingToAllInterfaces() throws Exception {
@Test
public void responseContainsJsonContentTypeHeader() throws Exception {

final RequestBody body = RequestBody.create(JSON, "{gasPrice}");
final RequestBody body = RequestBody.create(GRAPHQL, "{gasPrice}");

try (final Response resp = client.newCall(buildPostRequest(body)).execute()) {
assertThat(resp.header("Content-Type")).isEqualTo("application/json");
assertThat(resp.header("Content-Type")).isEqualTo(JSON.toString());
}
}

Expand All @@ -248,7 +293,7 @@ public void ethGetUncleCountByBlockHash() throws Exception {

final String query = "{block(hash:\"" + blockHash.toString() + "\") {ommerCount}}";

final RequestBody body = RequestBody.create(JSON, query);
final RequestBody body = RequestBody.create(GRAPHQL, query);
try (final Response resp = client.newCall(buildPostRequest(body)).execute()) {
assertThat(resp.code()).isEqualTo(200);
final String jsonStr = resp.body().string();
Expand All @@ -272,7 +317,7 @@ public void ethGetUncleCountByBlockNumber() throws Exception {

final String query = "{block(number:\"3\") {ommerCount}}";

final RequestBody body = RequestBody.create(JSON, query);
final RequestBody body = RequestBody.create(GRAPHQL, query);
try (final Response resp = client.newCall(buildPostRequest(body)).execute()) {
assertThat(resp.code()).isEqualTo(200);
final String jsonStr = resp.body().string();
Expand All @@ -296,7 +341,7 @@ public void ethGetUncleCountByBlockLatest() throws Exception {

final String query = "{block {ommerCount}}";

final RequestBody body = RequestBody.create(JSON, query);
final RequestBody body = RequestBody.create(GRAPHQL, query);
try (final Response resp = client.newCall(buildPostRequest(body)).execute()) {
assertThat(resp.code()).isEqualTo(200);
final String jsonStr = resp.body().string();
Expand Down