6
6
import com .example .demo .gql .client .PostProjection ;
7
7
import com .example .demo .gql .types .Post ;
8
8
import com .jayway .jsonpath .TypeRef ;
9
- import com .netflix .graphql .dgs .client .*;
9
+ import com .netflix .graphql .dgs .client .CustomGraphQLClient ;
10
+ import com .netflix .graphql .dgs .client .GraphQLClient ;
11
+ import com .netflix .graphql .dgs .client .GraphQLResponse ;
12
+ import com .netflix .graphql .dgs .client .HttpResponse ;
10
13
import com .netflix .graphql .dgs .client .codegen .GraphQLQueryRequest ;
11
14
import lombok .extern .slf4j .Slf4j ;
15
+ import org .intellij .lang .annotations .Language ;
12
16
import org .springframework .beans .factory .annotation .Value ;
13
17
import org .springframework .boot .ApplicationArguments ;
14
18
import org .springframework .boot .ApplicationRunner ;
@@ -44,20 +48,20 @@ private static HttpResponse execute(String url, Map<String, ? extends List<Strin
44
48
* To use RestTemplate, the requestHeaders need to be transformed into Spring's HttpHeaders.
45
49
*/
46
50
HttpHeaders requestHeaders = new HttpHeaders ();
47
- headers . forEach ( requestHeaders :: put );
51
+ requestHeaders . putAll ( headers );
48
52
49
53
/**
50
54
* Use RestTemplate to call the GraphQL service.
51
55
* The response type should simply be String, because the parsing will be done by the GraphQLClient.
52
56
*/
53
57
var dgsRestTemplate = new RestTemplate ();
54
- ResponseEntity <String > exchange = dgsRestTemplate .exchange (url , HttpMethod .POST , new HttpEntity (body , requestHeaders ), String .class );
58
+ ResponseEntity <String > exchange = dgsRestTemplate .exchange (url , HttpMethod .POST , new HttpEntity <> (body , requestHeaders ), String .class );
55
59
56
60
/**
57
61
* Return a HttpResponse, which contains the HTTP status code and response body (as a String).
58
62
* The way to get these depend on the HTTP client.
59
63
*/
60
- return new HttpResponse (exchange .getStatusCodeValue (), exchange .getBody ());
64
+ return new HttpResponse (exchange .getStatusCode (). value (), exchange .getBody ());
61
65
}
62
66
63
67
@@ -87,7 +91,7 @@ public void run(ApplicationArguments args) throws Exception {
87
91
GraphQLQueryRequest graphQLQueryRequest =
88
92
new GraphQLQueryRequest (
89
93
new AllPostsGraphQLQuery (),
90
- new AllPostsProjectionRoot <AuthorProjection <?,?>,PostProjection <?,?>>()
94
+ new AllPostsProjectionRoot <AuthorProjection <?, ?>, PostProjection <?, ?>>()
91
95
.id ()
92
96
.title ()
93
97
.content ()
@@ -98,11 +102,14 @@ public void run(ApplicationArguments args) throws Exception {
98
102
.createdAt ()
99
103
);
100
104
101
- String query = graphQLQueryRequest .serialize ();
105
+ @ Language ("graphql" ) String query = graphQLQueryRequest .serialize ();
106
+ log .info ("query string: {}" , query );
107
+
102
108
GraphQLClient client = new CustomGraphQLClient (url , DemoApplication ::execute );
103
- GraphQLResponse response = client .executeQuery (query , new HashMap <>() );
109
+ GraphQLResponse response = client .executeQuery (query , new HashMap <>());
104
110
105
- var data = response .extractValueAsObject ("allPosts" , new TypeRef <List <Post >>() { });
111
+ var data = response .extractValueAsObject ("allPosts" , new TypeRef <List <Post >>() {
112
+ });
106
113
log .info ("fetched all posts from client: {}" , data );
107
114
}
108
115
}
0 commit comments