Skip to content

Commit edfe56a

Browse files
committed
Support simple return type
1 parent a9392b4 commit edfe56a

8 files changed

Lines changed: 72 additions & 6 deletions

File tree

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ class GraphQLResponse {
1111
private Map<String, Object> data;
1212
private List<String> errors;
1313

14-
<T> T getFirstObject(Class<T> type) {
15-
return (T) data.entrySet().stream().findFirst().map(Entry::getValue).orElseThrow();
14+
Object getFirstObject() {
15+
return data.entrySet().stream().findFirst().map(Entry::getValue).orElseThrow();
1616
}
1717

1818
}

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

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
package graphql.kickstart.spring.webclient.boot;
22

3+
import com.fasterxml.jackson.databind.ObjectMapper;
34
import java.io.IOException;
45
import java.io.InputStream;
56
import java.nio.charset.StandardCharsets;
6-
import java.util.HashMap;
77
import java.util.Map;
88
import lombok.RequiredArgsConstructor;
99
import lombok.SneakyThrows;
@@ -20,6 +20,7 @@
2020
class GraphQLWebClientImpl implements GraphQLWebClient {
2121

2222
private final WebClient webClient;
23+
private final ObjectMapper objectMapper;
2324

2425
@Override
2526
public <T> Mono<T> query(String resource, Map<String, Object> variables, Class<T> returnType) {
@@ -34,12 +35,18 @@ public <T> Mono<T> query(String resource, Map<String, Object> variables, Class<T
3435
.bodyValue(request)
3536
.retrieve()
3637
.bodyToMono(GraphQLResponse.class)
37-
.map(it -> it.getFirstObject(returnType));
38+
.map(GraphQLResponse::getFirstObject)
39+
.map(it -> readValue(it, returnType));
40+
}
41+
42+
@SneakyThrows
43+
private <T> T readValue(Object value, Class<T> returnType) {
44+
log.debug("Read value: '{}'", value);
45+
return objectMapper.convertValue(value, returnType);
3846
}
3947

4048
@SneakyThrows
4149
private String loadQuery(String path) {
42-
// Resource resource = resourceLoader.getResource("classpath:" + location);
4350
return loadResource(new ClassPathResource(path));
4451
}
4552

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

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,12 @@
33
import static org.junit.Assert.assertEquals;
44
import static org.junit.Assert.assertNotNull;
55

6+
import com.fasterxml.jackson.databind.ObjectMapper;
7+
import graphql.kickstart.spring.webclient.testapp.Simple;
8+
import java.util.HashMap;
9+
import java.util.Map;
610
import org.junit.jupiter.api.BeforeEach;
11+
import org.junit.jupiter.api.DisplayName;
712
import org.junit.jupiter.api.Test;
813
import org.springframework.boot.test.context.SpringBootTest;
914
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
@@ -24,14 +29,37 @@ void beforeEach() {
2429
WebClient webClient = WebClient.builder()
2530
.baseUrl("http://localhost:" + randomServerPort)
2631
.build();
27-
graphqlClient = new GraphQLWebClientImpl(webClient);
32+
graphqlClient = new GraphQLWebClientImpl(webClient, new ObjectMapper());
2833
}
2934

3035
@Test
36+
@DisplayName("Query test without variables and returning String")
3137
void queryWithoutVariablesSucceeds() {
3238
Mono<String> response = graphqlClient.query("query-test.graphql", null, String.class);
3339
assertNotNull("response should not be null", response);
3440
assertEquals("response should equal 'test'", "test", response.block());
3541
}
3642

43+
@Test
44+
@DisplayName("Query echo with String variable and returning String")
45+
void echoStringSucceeds() {
46+
Map<String, Object> variables = new HashMap<>();
47+
variables.put("value", "echo echo echo");
48+
Mono<String> response = graphqlClient.query("query-echo.graphql", variables, String.class);
49+
assertNotNull("response should not be null", response);
50+
assertEquals("response should equal 'echo echo echo'", "echo echo echo", response.block());
51+
}
52+
53+
@Test
54+
@DisplayName("Query simple return type")
55+
void simpleTypeSucceeds() {
56+
Map<String, Object> variables = new HashMap<>();
57+
variables.put("id", "my-id");
58+
Mono<Simple> response = graphqlClient.query("query-simple.graphql", variables, Simple.class);
59+
assertNotNull("response should not be null", response);
60+
Simple object = response.block();
61+
assertNotNull(object);
62+
assertEquals("response id should equal 'my-id'", "my-id", object.getId());
63+
}
64+
3765
}

graphql-webclient/src/test/java/graphql/kickstart/spring/webclient/testapp/Query.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,8 @@ String echo(String value) {
1414
return value;
1515
}
1616

17+
Simple simple(String id) {
18+
return new Simple(id);
19+
}
20+
1721
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package graphql.kickstart.spring.webclient.testapp;
2+
3+
import lombok.AllArgsConstructor;
4+
import lombok.Data;
5+
import lombok.NoArgsConstructor;
6+
7+
@Data
8+
@NoArgsConstructor
9+
@AllArgsConstructor
10+
public class Simple {
11+
12+
private String id;
13+
14+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
query echo($value: String!) {
2+
echo(value: $value)
3+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
query simple($id: ID!) {
2+
simple(id: $id) {
3+
id
4+
}
5+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11
type Query {
22
test: String!
33
echo(value: String!): String!
4+
simple(id: ID!): Simple!
5+
}
6+
7+
type Simple {
8+
id: ID!
49
}

0 commit comments

Comments
 (0)