未验证 提交 26fa6f46 编写于 作者: B Bogdan Kobylynskyi 提交者: GitHub

Add operation name to serialized GraphQL query #590 (#604)

上级 25ef1740
......@@ -34,19 +34,23 @@ public class GraphQLRequestSerializer {
if (graphQLRequests.getRequests().isEmpty()) {
throw new IllegalArgumentException("At least one GraphQL request should be supplied");
}
GraphQLOperation operation = graphQLRequests.getRequests().get(0).getRequest().getOperationType();
GraphQLOperationRequest firstRequest = graphQLRequests.getRequests().get(0).getRequest();
StringBuilder queryBuilder = new StringBuilder();
for (GraphQLRequest request : graphQLRequests.getRequests()) {
if (request == null || request.getRequest() == null) {
throw new IllegalArgumentException("Null GraphQL request was supplied");
}
if (operation != null && operation != request.getRequest().getOperationType()) {
if (firstRequest.getOperationType() != null &&
firstRequest.getOperationType() != request.getRequest().getOperationType()) {
throw new IllegalArgumentException(
"Only operations of the same type (query/mutation/subscription) can be executed at once");
}
queryBuilder.append(buildQuery(request)).append(" ");
}
return jsonQuery(operationWrapper(queryBuilder.toString(), operation));
return jsonQuery(operationWrapper(
firstRequest.getOperationType(),
null, // combined request does not have operation name
queryBuilder.toString()));
}
/**
......@@ -60,10 +64,7 @@ public class GraphQLRequestSerializer {
if (graphQLRequest == null || graphQLRequest.getRequest() == null) {
return null;
}
GraphQLOperation operationType = graphQLRequest.getRequest().getOperationType();
String query = buildQuery(graphQLRequest);
String queryString = operationWrapper(query, operationType);
return jsonQuery(queryString);
return jsonQuery(toQueryString(graphQLRequest));
}
/**
......@@ -76,14 +77,20 @@ public class GraphQLRequestSerializer {
if (graphQLRequest == null || graphQLRequest.getRequest() == null) {
return null;
}
GraphQLOperation operationType = graphQLRequest.getRequest().getOperationType();
String query = buildQuery(graphQLRequest);
return operationWrapper(query, operationType);
return operationWrapper(
graphQLRequest.getRequest().getOperationType(),
graphQLRequest.getRequest().getOperationName(),
buildQuery(graphQLRequest));
}
private static String operationWrapper(String query, GraphQLOperation operationType) {
private static String operationWrapper(GraphQLOperation operationType, String operationName, String queryValue) {
assert operationType != null;
return operationType.name().toLowerCase() + " { " + query + " }";
String operationTypeLowerCased = operationType.name().toLowerCase();
if (operationName == null) {
return String.format("%s { %s }", operationTypeLowerCased, queryValue);
} else {
return String.format("%s %s { %s }", operationTypeLowerCased, operationName, queryValue);
}
}
private static String buildQuery(GraphQLRequest graphQLRequest) {
......
......@@ -90,7 +90,7 @@ class GraphQLRequestSerializerTest {
Function<String, String> expectedQueryDecorator) {
GraphQLRequest graphQLRequest = new GraphQLRequest(new VersionQueryRequest());
String serializedQuery = serializer.apply(graphQLRequest).replaceAll(" +", " ").trim();
String expectedQueryStr = "query { version }";
String expectedQueryStr = "query version { version }";
assertEquals(expectedQueryDecorator.apply(expectedQueryStr), serializedQuery);
}
......@@ -118,7 +118,7 @@ class GraphQLRequestSerializerTest {
.status()
);
String serializedQuery = serializer.apply(graphQLRequest).replaceAll(" +", " ").trim();
String expectedQueryStr = "query { " +
String expectedQueryStr = "query eventsByCategoryAndStatus { " +
"eventsByCategoryAndStatus(categoryId: \"categoryIdValue1\", status: OPEN){ " +
"id " +
"active " +
......@@ -155,7 +155,7 @@ class GraphQLRequestSerializerTest {
.status("myStatus")
);
String serializedQuery = serializer.apply(graphQLRequest).replaceAll(" +", " ").trim();
String expectedQueryStr = "query { " +
String expectedQueryStr = "query eventsByCategoryAndStatus { " +
"eventsByCategoryAndStatus(categoryId: \"categoryIdValue1\", status: OPEN){ " +
"myId : id " +
"myActive : active " +
......@@ -197,7 +197,7 @@ class GraphQLRequestSerializerTest {
.status()
);
String serializedQuery = serializer.apply(graphQLRequest).replaceAll(" +", " ").trim();
String expectedQueryStr = "query { " +
String expectedQueryStr = "query eventsByCategoryAndStatus { " +
"eventsByCategoryAndStatus(categoryId: \"categoryIdValue1\", status: OPEN){ " +
"id " +
"active " +
......@@ -241,7 +241,7 @@ class GraphQLRequestSerializerTest {
.status()
);
String serializedQuery = serializer.apply(graphQLRequest).replaceAll(" +", " ").trim();
String expectedQueryStr = "query { " +
String expectedQueryStr = "query eventsByCategoryAndStatus { " +
"eventsByCategoryAndStatus(categoryId: \"categoryIdValue1\", status: OPEN){ " +
"id " +
"active " +
......@@ -267,7 +267,7 @@ class GraphQLRequestSerializerTest {
.activeLockReason())
);
String serializedQuery = serializer.apply(graphQLRequest).replaceAll(" +", " ").trim();
String expectedQueryStr = "mutation { updateIssue(input: { " +
String expectedQueryStr = "mutation updateIssue { updateIssue(input: { " +
"floatVal: 1.23, booleanVal: false, intVal: 42, " +
"stringVal: \"default \\\" \\\\ \\b \\f \\n \\r \\t ሴ \", " +
"enumVal: OPEN, intList: [ 1, 2, 3 ], intListEmptyDefault: [ ] }){ " +
......@@ -289,7 +289,7 @@ class GraphQLRequestSerializerTest {
GraphQLRequest graphQLRequest = new GraphQLRequest(updateDateMutationRequest);
String serializedQuery = serializer.apply(graphQLRequest).replaceAll(" +", " ").trim();
String expectedQueryStr = "mutation { updateDate(input: { " +
String expectedQueryStr = "mutation updateDate { updateDate(input: { " +
"dateTime: \"2020-07-31T03:17:17.884Z\" }) }";
assertEquals(expectedQueryDecorator.apply(expectedQueryStr), serializedQuery);
}
......@@ -306,7 +306,7 @@ class GraphQLRequestSerializerTest {
GraphQLRequest graphQLRequest = new GraphQLRequest(updateDateMutationRequest);
String serializedQuery = serializer.apply(graphQLRequest).replaceAll(" +", " ").trim();
String expectedQueryStr = "mutation { updateDate(input: \"2020-07-31T03:17:17.884Z\") }";
String expectedQueryStr = "mutation updateDate { updateDate(input: \"2020-07-31T03:17:17.884Z\") }";
assertEquals(expectedQueryDecorator.apply(expectedQueryStr), serializedQuery);
}
......@@ -337,7 +337,7 @@ class GraphQLRequestSerializerTest {
.id())))
);
String serializedQuery = serializer.apply(graphQLRequest).replaceAll(" +", " ").trim();
String expectedQueryStr = "query { " +
String expectedQueryStr = "query eventsByCategoryAndStatus { " +
"eventsByCategoryAndStatus(categoryId: \"categoryIdValue1\", status: OPEN){ " +
"properties { " +
"child { parent (createdAfter: \"2007-01-09T09:41:00Z\") { id } } } " +
......@@ -361,7 +361,7 @@ class GraphQLRequestSerializerTest {
.activeLockReason())
);
String serializedQuery = serializer.apply(graphQLRequest).replaceAll(" +", " ").trim();
String expectedQueryStr = "mutation { updateIssue(input: { " +
String expectedQueryStr = "mutation updateIssue { updateIssue(input: { " +
"floatVal: 1.23, booleanVal: false, intVal: 42, " +
"stringVal: \"default \\\" \\\\ \\b \\f \\n \\r \\t ሴ \", " +
"enumVal: OPEN, intList: [ 1, 2, 3 ], intListEmptyDefault: [ \"\", \"1\", null, " +
......@@ -384,7 +384,7 @@ class GraphQLRequestSerializerTest {
.name())
.typename()));
String serializedQuery = serializer.apply(graphQLRequest).replaceAll(" +", " ").trim();
String expectedQueryStr = "mutation { updateIssue{ " +
String expectedQueryStr = "mutation updateIssue { updateIssue{ " +
"union { ...on Issue { activeLockReason } ...on Organization { name } __typename } " +
"} }";
assertEquals(expectedQueryDecorator.apply(expectedQueryStr), serializedQuery);
......@@ -402,7 +402,7 @@ class GraphQLRequestSerializerTest {
.id()
);
String serializedQuery = serializer.apply(graphQLRequest).replaceAll(" +", " ").trim();
String expectedQueryStr = "query { eventsByIds(ids: [ " +
String expectedQueryStr = "query eventsByIds { eventsByIds(ids: [ " +
"\"\\\"\", " +
"\"\\\\\", " +
"\"\\b\", " +
......@@ -428,7 +428,8 @@ class GraphQLRequestSerializerTest {
.id()
);
String serializedQuery = serializer.apply(graphQLRequest).replaceAll(" +", " ").trim();
String expectedQueryStr = "query { eventsByIds(contextId: \"something\", translated: false){ id } }";
String expectedQueryStr = "query eventsByIds { " +
"eventsByIds(contextId: \"something\", translated: false){ id } }";
assertEquals(expectedQueryDecorator.apply(expectedQueryStr), serializedQuery);
}
......@@ -446,7 +447,7 @@ class GraphQLRequestSerializerTest {
.id()
);
String serializedQuery = serializer.apply(graphQLRequest).replaceAll(" +", " ").trim();
String expectedQueryStr = "query { eventsByIds{ id } }";
String expectedQueryStr = "query eventsByIds { eventsByIds{ id } }";
assertEquals(expectedQueryDecorator.apply(expectedQueryStr), serializedQuery);
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册