Skip to content

Commit b6d34be

Browse files
robbertnoordzijRobbert Noordzijkobylynskyi
authored
Ability to add custom operation name for request (#682)
* Ability to add custom operation name for request Co-authored-by: Robbert Noordzij <robbert@robbertnoordzij.nl> Co-authored-by: Bogdan Kobylynskyi <92bogdan@gmail.com>
1 parent d266534 commit b6d34be

File tree

4 files changed

+85
-3
lines changed

4 files changed

+85
-3
lines changed

src/main/java/com/kobylynskyi/graphql/codegen/model/graphql/GraphQLRequest.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,25 @@
55
*/
66
public class GraphQLRequest {
77

8+
private final String operationName;
89
private final GraphQLOperationRequest request;
910
private final GraphQLResponseProjection responseProjection;
1011

1112
public GraphQLRequest(GraphQLOperationRequest request) {
12-
this(request, null);
13+
this(null, request, null);
14+
}
15+
16+
public GraphQLRequest(String operationName, GraphQLOperationRequest request) {
17+
this(operationName, request, null);
1318
}
1419

1520
public GraphQLRequest(GraphQLOperationRequest request, GraphQLResponseProjection responseProjection) {
21+
this(null, request, responseProjection);
22+
}
23+
24+
public GraphQLRequest(String operationName, GraphQLOperationRequest request,
25+
GraphQLResponseProjection responseProjection) {
26+
this.operationName = operationName;
1627
this.request = request;
1728
this.responseProjection = responseProjection;
1829
}
@@ -25,6 +36,10 @@ public GraphQLResponseProjection getResponseProjection() {
2536
return responseProjection;
2637
}
2738

39+
public String getOperationName() {
40+
return operationName;
41+
}
42+
2843
/**
2944
* Serializes GraphQL request to be used as HTTP JSON body
3045
* according to https://graphql.org/learn/serving-over-http specifications

src/main/java/com/kobylynskyi/graphql/codegen/model/graphql/GraphQLRequestSerializer.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public static String toHttpJsonBody(GraphQLRequests graphQLRequests) {
4949
}
5050
return jsonQuery(operationWrapper(
5151
firstRequest.getOperationType(),
52-
null, // combined request does not have operation name
52+
graphQLRequests.getOperationName(),
5353
queryBuilder.toString()));
5454
}
5555

@@ -77,9 +77,13 @@ public static String toQueryString(GraphQLRequest graphQLRequest) {
7777
if (graphQLRequest == null || graphQLRequest.getRequest() == null) {
7878
return null;
7979
}
80+
81+
String operationName = graphQLRequest.getOperationName() == null ?
82+
graphQLRequest.getRequest().getOperationName() : graphQLRequest.getOperationName();
83+
8084
return operationWrapper(
8185
graphQLRequest.getRequest().getOperationType(),
82-
graphQLRequest.getRequest().getOperationName(),
86+
operationName,
8387
buildQuery(graphQLRequest));
8488
}
8589

src/main/java/com/kobylynskyi/graphql/codegen/model/graphql/GraphQLRequests.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,15 @@
99
*/
1010
public class GraphQLRequests {
1111

12+
private final String operationName;
1213
private final List<GraphQLRequest> requests = new ArrayList<>();
1314

1415
public GraphQLRequests(GraphQLRequest... requests) {
16+
this(null, requests);
17+
}
18+
19+
public GraphQLRequests(String operationName, GraphQLRequest... requests) {
20+
this.operationName = operationName;
1521
this.requests.addAll(Arrays.asList(requests));
1622
}
1723

@@ -23,6 +29,10 @@ public List<GraphQLRequest> getRequests() {
2329
return new ArrayList<>(requests);
2430
}
2531

32+
public String getOperationName() {
33+
return operationName;
34+
}
35+
2636
/**
2737
* Serializes multiple GraphQL requests to be used as HTTP JSON body
2838
* according to https://graphql.org/learn/serving-over-http specifications

src/test/java/com/kobylynskyi/graphql/codegen/model/graphql/GraphQLRequestSerializerTest.java

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,27 @@ void serialize_withResponseProjectionAndParametrizedInputAndAlias(String name,
256256
assertEquals(expectedQueryDecorator.apply(expectedQueryStr), serializedQuery);
257257
}
258258

259+
@ParameterizedTest(name = "{0}")
260+
@MethodSource("provideAllSerializers")
261+
void serialize_withCustomOpertionName(String name, Function<GraphQLRequest, String> serializer,
262+
Function<String, String> expectedQueryDecorator) {
263+
EventsByIdsQueryRequest request = new EventsByIdsQueryRequest.Builder()
264+
.setContextId("something")
265+
.setIds(null)
266+
.setTranslated(false)
267+
.build();
268+
GraphQLRequest graphQLRequest = new GraphQLRequest(
269+
"customOperationName",
270+
request,
271+
new EventResponseProjection()
272+
.id()
273+
);
274+
String serializedQuery = serializer.apply(graphQLRequest).replaceAll(" +", " ").trim();
275+
String expectedQueryStr = "query customOperationName { " +
276+
"eventsByIds(contextId: \"something\", translated: false){ id } }";
277+
assertEquals(expectedQueryDecorator.apply(expectedQueryStr), serializedQuery);
278+
}
279+
259280
@ParameterizedTest(name = "{0}")
260281
@MethodSource("provideAllSerializers")
261282
void serialize_complexRequestWithDefaultData(String name, Function<GraphQLRequest, String> serializer,
@@ -505,6 +526,38 @@ void serialize_multipleRequests(String name, Function<GraphQLRequests, String> s
505526
assertEquals(expectedQueryDecorator.apply(expectedQueryStr), serializedQuery);
506527
}
507528

529+
@ParameterizedTest(name = "{0}")
530+
@MethodSource("provideStaticSerializerForMultiRequest")
531+
void serialize_multipleRequestsWithCustomOperationName(String name, Function<GraphQLRequests, String> serializer,
532+
Function<String, String> expectedQueryDecorator) {
533+
EventsByCategoryAndStatusQueryRequest request1 = new EventsByCategoryAndStatusQueryRequest.Builder()
534+
.alias("req1").setStatus(Status.OPEN).build();
535+
GraphQLRequest graphQLRequest1 = new GraphQLRequest(request1, new EventResponseProjection().id());
536+
537+
EventsByCategoryAndStatusQueryRequest request2 = new EventsByCategoryAndStatusQueryRequest("req2");
538+
GraphQLRequest graphQLRequest2 = new GraphQLRequest(request2, new EventResponseProjection().id().status());
539+
540+
EventsByCategoryAndStatusQueryRequest request21 = new EventsByCategoryAndStatusQueryRequest();
541+
GraphQLRequest graphQLRequest21 = new GraphQLRequest(request21);
542+
543+
GraphQLRequests requests = new GraphQLRequests(
544+
"customOperationName",
545+
graphQLRequest1,
546+
graphQLRequest2,
547+
graphQLRequest21
548+
);
549+
550+
String serializedQuery = serializer
551+
.apply(requests).replaceAll(" +", " ")
552+
.trim();
553+
String expectedQueryStr = "query customOperationName { " +
554+
"req1: eventsByCategoryAndStatus(status: OPEN){ id } " +
555+
"req2: eventsByCategoryAndStatus{ id status } " +
556+
"eventsByCategoryAndStatus " +
557+
"}";
558+
assertEquals(expectedQueryDecorator.apply(expectedQueryStr), serializedQuery);
559+
}
560+
508561
@ParameterizedTest(name = "{0}")
509562
@MethodSource("provideStaticSerializerForMultiRequest")
510563
void serialize_multipleRequests_DiffTypes(String name, Function<GraphQLRequests, String> serializer,

0 commit comments

Comments
 (0)