Ability to add custom operation name for request (#682) · Isuru-F/graphql-java-codegen@b6d34be · GitHub
Skip to content

Commit b6d34be

Browse files
robbertnoordzijRobbert Noordzijkobylynskyi
authored
Ability to add custom operation name for request (kobylynskyi#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

4 files changed

Lines changed: 85 additions & 3 deletions

File tree

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

Lines changed: 16 additions & 1 deletion

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

0 commit comments

Comments
 (0)