Skip to content

Commit 0e695ef

Browse files
authored
Generate Response class having response data with exact type #159 (#161)
* Generate Response class having response data with exact type #159 * Extend GraphQLResultTest
1 parent 8b644f9 commit 0e695ef

File tree

31 files changed

+810
-312
lines changed

31 files changed

+810
-312
lines changed

docs/codegen-options.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,9 @@
2525
| `generateParameterizedFieldsResolvers` | Boolean | True | If true, then generate separate `Resolver` interface for parametrized fields. If false, then add field to the type definition and ignore field parameters. |
2626
| `generateExtensionFieldsResolvers` | Boolean | False | Specifies whether all fields in extensions (`extend type` and `extend interface`) should be present in Resolver interface instead of the type class itself. |
2727
| `subscriptionReturnType` | String | Empty | Return type for subscription methods. For example: `org.reactivestreams.Publisher`, `io.reactivex.Observable`, etc. |
28-
| `generateRequests` | Boolean | False | Specifies whether client-side classes should be generated for each query, mutation and subscription. This includes: `Request` class (contains input data) and `ResponseProjection` class (contains response fields). |
28+
| `generateClient` | Boolean | False | Specifies whether client-side classes should be generated for each query, mutation and subscription. This includes: `Request` classes (contain input data), `ResponseProjection` classes for each type (contain response fields) and `Response` classes (contain response data). |
2929
| `requestSuffix` | String | Request | Sets the suffix for `Request` classes. |
30+
| `responseSuffix` | String | Response | Sets the suffix for `Response` classes. |
3031
| `responseProjectionSuffix` | String | ResponseProjection | Sets the suffix for `ResponseProjection` classes. |
3132
| `parametrizedInputSuffix` | String | ParametrizedInput | Sets the suffix for `ParametrizedInput` classes. |
3233
| `parentInterfaces` | *See [parentInterfaces](#option-parentinterfaces)* | Empty | Block to define parent interfaces for generated interfaces (query / mutation / subscription / type resolver) |

plugins/gradle/example-client/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ task graphqlCodegenProductService(type: GraphQLCodegenGradleTask) {
4949
DateTime: "java.util.Date"
5050
]
5151
modelNameSuffix = "TO"
52-
generateRequests = true
52+
generateClient = true
5353
generateApis = false
5454
}
5555

Lines changed: 39 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,21 @@
11
package io.github.kobylynskyi.order.external;
22

33
import com.kobylynskyi.graphql.codegen.model.graphql.GraphQLRequest;
4-
import com.kobylynskyi.graphql.codegen.model.graphql.GraphQLResult;
54
import io.github.kobylynskyi.order.model.Product;
65
import io.github.kobylynskyi.order.model.UnableToCreateProductException;
76
import io.github.kobylynskyi.order.model.UnableToRetrieveProductException;
87
import io.github.kobylynskyi.order.model.UnableToRetrieveProductsException;
9-
import io.github.kobylynskyi.product.graphql.model.*;
8+
import io.github.kobylynskyi.product.graphql.model.CreateMutationRequest;
9+
import io.github.kobylynskyi.product.graphql.model.CreateMutationResponse;
10+
import io.github.kobylynskyi.product.graphql.model.ProductByIdQueryRequest;
11+
import io.github.kobylynskyi.product.graphql.model.ProductByIdQueryResponse;
12+
import io.github.kobylynskyi.product.graphql.model.ProductInputTO;
13+
import io.github.kobylynskyi.product.graphql.model.ProductResponseProjection;
14+
import io.github.kobylynskyi.product.graphql.model.ProductTO;
15+
import io.github.kobylynskyi.product.graphql.model.ProductsByIdsQueryRequest;
16+
import io.github.kobylynskyi.product.graphql.model.ProductsByIdsQueryResponse;
1017
import org.springframework.beans.factory.annotation.Autowired;
1118
import org.springframework.beans.factory.annotation.Value;
12-
import org.springframework.core.ParameterizedTypeReference;
1319
import org.springframework.http.HttpEntity;
1420
import org.springframework.http.HttpHeaders;
1521
import org.springframework.http.HttpMethod;
@@ -22,7 +28,6 @@
2228
import java.util.Collection;
2329
import java.util.Collections;
2430
import java.util.List;
25-
import java.util.Map;
2631
import java.util.stream.Collectors;
2732

2833
@Service
@@ -38,44 +43,41 @@ public class ProductServiceGraphQLClient {
3843
private String productUrl;
3944

4045
public Product getProduct(String productId) throws UnableToRetrieveProductException {
41-
ProductByIdQueryRequest getProductRequest = new ProductByIdQueryRequest();
42-
getProductRequest.setId(productId);
43-
GraphQLRequest request = new GraphQLRequest(getProductRequest,
44-
new ProductResponseProjection()
45-
.id()
46-
.title()
47-
.price());
46+
ProductByIdQueryRequest productByIdQueryRequest = new ProductByIdQueryRequest();
47+
productByIdQueryRequest.setId(productId);
48+
ProductResponseProjection responseProjection = new ProductResponseProjection()
49+
.id()
50+
.title()
51+
.price();
52+
GraphQLRequest graphQLRequest = new GraphQLRequest(productByIdQueryRequest, responseProjection);
4853

49-
GraphQLResult<Map<String, ProductTO>> result = restTemplate.exchange(URI.create(productUrl),
54+
ProductByIdQueryResponse result = restTemplate.exchange(URI.create(productUrl),
5055
HttpMethod.POST,
51-
httpEntity(request),
52-
new ParameterizedTypeReference<GraphQLResult<Map<String, ProductTO>>>() {})
53-
.getBody();
56+
httpEntity(graphQLRequest),
57+
ProductByIdQueryResponse.class).getBody();
5458
if (result.hasErrors()) {
5559
throw new UnableToRetrieveProductException(productId, result.getErrors().get(0).getMessage());
5660
}
57-
return productMapper.map(result.getData().get(getProductRequest.getOperationName()));
61+
return productMapper.map(result.productById());
5862
}
5963

6064
public List<Product> getProducts(Collection<String> productIds) throws UnableToRetrieveProductsException {
6165
ProductsByIdsQueryRequest getProductRequest = new ProductsByIdsQueryRequest();
6266
getProductRequest.setIds(productIds);
63-
GraphQLRequest request = new GraphQLRequest(getProductRequest,
64-
new ProductResponseProjection()
65-
.id()
66-
.title()
67-
.price());
67+
ProductResponseProjection responseProjection = new ProductResponseProjection()
68+
.id()
69+
.title()
70+
.price();
71+
GraphQLRequest request = new GraphQLRequest(getProductRequest, responseProjection);
6872

69-
GraphQLResult<Map<String, List<ProductTO>>> result = restTemplate.exchange(URI.create(productUrl),
73+
ProductsByIdsQueryResponse result = restTemplate.exchange(URI.create(productUrl),
7074
HttpMethod.POST,
7175
httpEntity(request),
72-
new ParameterizedTypeReference<GraphQLResult<Map<String, List<ProductTO>>>>() {})
73-
.getBody();
76+
ProductsByIdsQueryResponse.class).getBody();
7477
if (result.hasErrors()) {
7578
throw new UnableToRetrieveProductsException(productIds, result.getErrors().get(0).getMessage());
7679
}
77-
List<ProductTO> productTOs = result.getData().get(getProductRequest.getOperationName());
78-
return productTOs.stream().map(productMapper::map).collect(Collectors.toList());
80+
return result.productsByIds().stream().map(productMapper::map).collect(Collectors.toList());
7981
}
8082

8183
public Product createProduct(String productTitle, String productSku, BigDecimal productPrice) throws UnableToCreateProductException {
@@ -85,29 +87,28 @@ public Product createProduct(String productTitle, String productSku, BigDecimal
8587
.setPrice(productPrice.toString())
8688
.setSku(productSku)
8789
.build());
88-
GraphQLRequest request = new GraphQLRequest(createProductRequest,
89-
new ProductResponseProjection()
90-
.id()
91-
.title()
92-
.price()
93-
.sku());
90+
ProductResponseProjection responseProjection = new ProductResponseProjection()
91+
.id()
92+
.title()
93+
.price()
94+
.sku();
95+
GraphQLRequest request = new GraphQLRequest(createProductRequest, responseProjection);
9496

95-
GraphQLResult<Map<String, ProductTO>> result = restTemplate.exchange(URI.create(productUrl),
97+
CreateMutationResponse result = restTemplate.exchange(URI.create(productUrl),
9698
HttpMethod.POST,
9799
httpEntity(request),
98-
new ParameterizedTypeReference<GraphQLResult<Map<String, ProductTO>>>() {})
99-
.getBody();
100+
CreateMutationResponse.class).getBody();
100101
if (result.hasErrors()) {
101102
throw new UnableToCreateProductException(result.getErrors().get(0).getMessage());
102103
}
103-
ProductTO productTO = result.getData().get(createProductRequest.getOperationName());
104+
ProductTO productTO = result.create();
104105
return productMapper.map(productTO);
105106
}
106107

107-
private static HttpEntity<String> httpEntity(Object request) {
108+
private static HttpEntity<String> httpEntity(GraphQLRequest request) {
108109
HttpHeaders headers = new HttpHeaders();
109110
headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));
110111
headers.setContentType(MediaType.APPLICATION_JSON);
111-
return new HttpEntity<>(request.toString(), headers);
112+
return new HttpEntity<>(request.toHttpJsonBody(), headers);
112113
}
113114
}

plugins/gradle/graphql-java-codegen-gradle-plugin/src/main/java/io/github/kobylynskyi/graphql/codegen/gradle/GraphQLCodegenGradleTask.java

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,16 @@ public class GraphQLCodegenGradleTask extends DefaultTask implements GraphQLCode
4848
private Boolean generateDataFetchingEnvironmentArgumentInApis = MappingConfigConstants.DEFAULT_GENERATE_DATA_FETCHING_ENV;
4949
private Set<String> fieldsWithResolvers = new HashSet<>();
5050
private Set<String> fieldsWithoutResolvers = new HashSet<>();
51+
52+
53+
/**
54+
* @deprecated Not intended for use and will be removed in the next version.
55+
* Please use: {@link #generateClient}
56+
*/
5157
private Boolean generateRequests;
58+
private Boolean generateClient;
5259
private String requestSuffix;
60+
private String responseSuffix;
5361
private String responseProjectionSuffix;
5462
private String parametrizedInputSuffix;
5563
private final ParentInterfacesConfig parentInterfaces = new ParentInterfacesConfig();
@@ -82,8 +90,9 @@ public void generate() throws Exception {
8290
mappingConfig.setGenerateDataFetchingEnvironmentArgumentInApis(generateDataFetchingEnvironmentArgumentInApis);
8391
mappingConfig.setFieldsWithResolvers(fieldsWithResolvers);
8492
mappingConfig.setFieldsWithoutResolvers(fieldsWithoutResolvers);
85-
mappingConfig.setGenerateRequests(generateRequests);
93+
mappingConfig.setGenerateClient(Boolean.TRUE.equals(generateClient) || Boolean.TRUE.equals(generateRequests)); // FIXME after removing generateRequests
8694
mappingConfig.setRequestSuffix(requestSuffix);
95+
mappingConfig.setResponseSuffix(responseSuffix);
8796
mappingConfig.setResponseProjectionSuffix(responseProjectionSuffix);
8897
mappingConfig.setParametrizedInputSuffix(parametrizedInputSuffix);
8998
mappingConfig.setResolverParentInterface(getResolverParentInterface());
@@ -377,17 +386,36 @@ public void setFieldsWithoutResolvers(Set<String> fieldsWithoutResolvers) {
377386
this.fieldsWithoutResolvers = fieldsWithoutResolvers;
378387
}
379388

389+
/**
390+
* @deprecated Not intended for use and will be removed in the next version.
391+
* Please use: {@link #generateClient}
392+
*/
380393
@Input
381394
@Optional
382-
@Override
395+
@Deprecated
383396
public Boolean getGenerateRequests() {
384397
return generateRequests;
385398
}
386399

400+
/**
401+
* @deprecated Not intended for use and will be removed in the next version.
402+
* Please use: {@link #generateClient}
403+
*/
387404
public void setGenerateRequests(Boolean generateRequests) {
388405
this.generateRequests = generateRequests;
389406
}
390407

408+
@Input
409+
@Optional
410+
@Override
411+
public Boolean getGenerateClient() {
412+
return generateClient;
413+
}
414+
415+
public void setGenerateClient(Boolean generateClient) {
416+
this.generateClient = generateClient;
417+
}
418+
391419
@Input
392420
@Optional
393421
@Override
@@ -399,6 +427,17 @@ public void setRequestSuffix(String requestSuffix) {
399427
this.requestSuffix = requestSuffix;
400428
}
401429

430+
@Input
431+
@Optional
432+
@Override
433+
public String getResponseSuffix() {
434+
return responseSuffix;
435+
}
436+
437+
public void setResponseSuffix(String responseSuffix) {
438+
this.responseSuffix = responseSuffix;
439+
}
440+
402441
@Input
403442
@Optional
404443
@Override

plugins/maven/example-client/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
<DateTime>java.util.Date</DateTime>
3333
</customTypesMapping>
3434
<modelNameSuffix>TO</modelNameSuffix>
35-
<generateRequests>true</generateRequests>
35+
<generateClient>true</generateClient>
3636
<generateApis>false</generateApis>
3737
</configuration>
3838
</execution>

0 commit comments

Comments
 (0)