未验证 提交 b6d34be2 编写于 作者: R Robbert Noordzij 提交者: GitHub

Ability to add custom operation name for request (#682)

* Ability to add custom operation name for request
Co-authored-by: NRobbert Noordzij <robbert@robbertnoordzij.nl>
Co-authored-by: NBogdan Kobylynskyi <92bogdan@gmail.com>
上级 d2665348
......@@ -5,14 +5,25 @@ package com.kobylynskyi.graphql.codegen.model.graphql;
*/
public class GraphQLRequest {
private final String operationName;
private final GraphQLOperationRequest request;
private final GraphQLResponseProjection responseProjection;
public GraphQLRequest(GraphQLOperationRequest request) {
this(request, null);
this(null, request, null);
}
public GraphQLRequest(String operationName, GraphQLOperationRequest request) {
this(operationName, request, null);
}
public GraphQLRequest(GraphQLOperationRequest request, GraphQLResponseProjection responseProjection) {
this(null, request, responseProjection);
}
public GraphQLRequest(String operationName, GraphQLOperationRequest request,
GraphQLResponseProjection responseProjection) {
this.operationName = operationName;
this.request = request;
this.responseProjection = responseProjection;
}
......@@ -25,6 +36,10 @@ public class GraphQLRequest {
return responseProjection;
}
public String getOperationName() {
return operationName;
}
/**
* Serializes GraphQL request to be used as HTTP JSON body
* according to https://graphql.org/learn/serving-over-http specifications
......
......@@ -49,7 +49,7 @@ public class GraphQLRequestSerializer {
}
return jsonQuery(operationWrapper(
firstRequest.getOperationType(),
null, // combined request does not have operation name
graphQLRequests.getOperationName(),
queryBuilder.toString()));
}
......@@ -77,9 +77,13 @@ public class GraphQLRequestSerializer {
if (graphQLRequest == null || graphQLRequest.getRequest() == null) {
return null;
}
String operationName = graphQLRequest.getOperationName() == null ?
graphQLRequest.getRequest().getOperationName() : graphQLRequest.getOperationName();
return operationWrapper(
graphQLRequest.getRequest().getOperationType(),
graphQLRequest.getRequest().getOperationName(),
operationName,
buildQuery(graphQLRequest));
}
......
......@@ -9,9 +9,15 @@ import java.util.List;
*/
public class GraphQLRequests {
private final String operationName;
private final List<GraphQLRequest> requests = new ArrayList<>();
public GraphQLRequests(GraphQLRequest... requests) {
this(null, requests);
}
public GraphQLRequests(String operationName, GraphQLRequest... requests) {
this.operationName = operationName;
this.requests.addAll(Arrays.asList(requests));
}
......@@ -23,6 +29,10 @@ public class GraphQLRequests {
return new ArrayList<>(requests);
}
public String getOperationName() {
return operationName;
}
/**
* Serializes multiple GraphQL requests to be used as HTTP JSON body
* according to https://graphql.org/learn/serving-over-http specifications
......
......@@ -256,6 +256,27 @@ class GraphQLRequestSerializerTest {
assertEquals(expectedQueryDecorator.apply(expectedQueryStr), serializedQuery);
}
@ParameterizedTest(name = "{0}")
@MethodSource("provideAllSerializers")
void serialize_withCustomOpertionName(String name, Function<GraphQLRequest, String> serializer,
Function<String, String> expectedQueryDecorator) {
EventsByIdsQueryRequest request = new EventsByIdsQueryRequest.Builder()
.setContextId("something")
.setIds(null)
.setTranslated(false)
.build();
GraphQLRequest graphQLRequest = new GraphQLRequest(
"customOperationName",
request,
new EventResponseProjection()
.id()
);
String serializedQuery = serializer.apply(graphQLRequest).replaceAll(" +", " ").trim();
String expectedQueryStr = "query customOperationName { " +
"eventsByIds(contextId: \"something\", translated: false){ id } }";
assertEquals(expectedQueryDecorator.apply(expectedQueryStr), serializedQuery);
}
@ParameterizedTest(name = "{0}")
@MethodSource("provideAllSerializers")
void serialize_complexRequestWithDefaultData(String name, Function<GraphQLRequest, String> serializer,
......@@ -505,6 +526,38 @@ class GraphQLRequestSerializerTest {
assertEquals(expectedQueryDecorator.apply(expectedQueryStr), serializedQuery);
}
@ParameterizedTest(name = "{0}")
@MethodSource("provideStaticSerializerForMultiRequest")
void serialize_multipleRequestsWithCustomOperationName(String name, Function<GraphQLRequests, String> serializer,
Function<String, String> expectedQueryDecorator) {
EventsByCategoryAndStatusQueryRequest request1 = new EventsByCategoryAndStatusQueryRequest.Builder()
.alias("req1").setStatus(Status.OPEN).build();
GraphQLRequest graphQLRequest1 = new GraphQLRequest(request1, new EventResponseProjection().id());
EventsByCategoryAndStatusQueryRequest request2 = new EventsByCategoryAndStatusQueryRequest("req2");
GraphQLRequest graphQLRequest2 = new GraphQLRequest(request2, new EventResponseProjection().id().status());
EventsByCategoryAndStatusQueryRequest request21 = new EventsByCategoryAndStatusQueryRequest();
GraphQLRequest graphQLRequest21 = new GraphQLRequest(request21);
GraphQLRequests requests = new GraphQLRequests(
"customOperationName",
graphQLRequest1,
graphQLRequest2,
graphQLRequest21
);
String serializedQuery = serializer
.apply(requests).replaceAll(" +", " ")
.trim();
String expectedQueryStr = "query customOperationName { " +
"req1: eventsByCategoryAndStatus(status: OPEN){ id } " +
"req2: eventsByCategoryAndStatus{ id status } " +
"eventsByCategoryAndStatus " +
"}";
assertEquals(expectedQueryDecorator.apply(expectedQueryStr), serializedQuery);
}
@ParameterizedTest(name = "{0}")
@MethodSource("provideStaticSerializerForMultiRequest")
void serialize_multipleRequests_DiffTypes(String name, Function<GraphQLRequests, String> serializer,
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册