Skip to content

Commit 1b6ace0

Browse files
authored
Merge pull request #13 from 1096kc/Feature/custom-resultMapping-and-errorHandling
Change the post method return type to GraphQLResponse
2 parents 79dff05 + f4e68d1 commit 1b6ace0

File tree

4 files changed

+18
-9
lines changed

4 files changed

+18
-9
lines changed

graphql-webclient/src/main/java/graphql/kickstart/spring/webclient/boot/GraphQLResponse.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import lombok.Data;
77

88
@Data
9-
class GraphQLResponse {
9+
public class GraphQLResponse {
1010

1111
private Map<String, Object> data;
1212
private List<GraphQLError> errors;

graphql-webclient/src/main/java/graphql/kickstart/spring/webclient/boot/GraphQLWebClient.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ static GraphQLWebClient newInstance(WebClient webClient, ObjectMapper objectMapp
1616

1717
<T> Mono<T> post(String resource, Map<String, Object> variables, Class<T> returnType);
1818

19-
<T> Mono<T> post(GraphQLRequest<T> request);
19+
Mono<GraphQLResponse> post(GraphQLRequest<?> request);
2020

2121
<T> Flux<T> flux(String resource, Class<T> returnType);
2222

graphql-webclient/src/main/java/graphql/kickstart/spring/webclient/boot/GraphQLWebClientImpl.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,13 @@ private <T> T readValue(Object value, Class<T> returnType) {
3535
}
3636

3737
@Override
38-
public <T> Mono<T> post(GraphQLRequest<T> request) {
39-
return execute(request).map(it -> readValue(it, request.getReturnType()));
38+
public Mono<GraphQLResponse> post(GraphQLRequest<?> request) {
39+
WebClient.RequestBodySpec spec = webClient.post()
40+
.contentType(MediaType.APPLICATION_JSON);
41+
request.getHeaders().forEach((header, values) -> spec.header(header, values.toArray(new String[0])));
42+
return spec.bodyValue(request.getRequestBody())
43+
.retrieve()
44+
.bodyToMono(GraphQLResponse.class);
4045
}
4146

4247
@Override

graphql-webclient/src/test/java/graphql/kickstart/spring/webclient/boot/GraphQLWebClientTest.java

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -72,11 +72,12 @@ void simpleTypeAsRequestSucceeds() {
7272
.resource("query-simple.graphql")
7373
.variables(Map.of("id", "my-id"))
7474
.build();
75-
Mono<Simple> response = graphqlClient.post(request);
75+
Mono<GraphQLResponse> response = graphqlClient.post(request);
7676
assertNotNull("response should not be null", response);
77-
Simple object = response.block();
77+
GraphQLResponse object = response.block();
7878
assertNotNull(object);
79-
assertEquals("response id should equal 'my-id'", "my-id", object.getId());
79+
Map<String,Object> simple = (Map<String, Object>) object.getData().get("simple");
80+
assertEquals("response id should equal 'my-id'", "my-id", simple.get("id"));
8081
}
8182

8283
@Test
@@ -107,9 +108,12 @@ void headerIsAdded() {
107108
.variables(Map.of("name", "my-custom-header"))
108109
.header("my-custom-header", "my-custom-header-value")
109110
.build();
110-
Mono<String> response = graphqlClient.post(request);
111+
Mono<GraphQLResponse> response = graphqlClient.post(request);
111112
assertNotNull("response should not be null", response);
112-
assertEquals("response should equal 'my-custom-header-value'", "my-custom-header-value", response.block());
113+
assertNotNull("response should not be null", response);
114+
GraphQLResponse object = response.block();
115+
assertNotNull(object);
116+
assertEquals("response should equal 'my-custom-header-value'", "my-custom-header-value", object.getData().get("header"));
113117
}
114118

115119
}

0 commit comments

Comments
 (0)