1
1
package club .devcord .devmarkt .requests .query ;
2
2
3
- import club .devcord .devmarkt .env .GlobalEnv ;
3
+ import club .devcord .devmarkt .DevmarktBot ;
4
+ import com .fasterxml .jackson .annotation .JsonInclude ;
4
5
import com .fasterxml .jackson .core .JsonProcessingException ;
5
6
import com .fasterxml .jackson .databind .ObjectMapper ;
7
+ import org .slf4j .Logger ;
8
+ import org .slf4j .LoggerFactory ;
6
9
7
10
import java .io .IOException ;
8
11
import java .net .URI ;
16
19
import java .util .Map ;
17
20
import java .util .concurrent .CompletableFuture ;
18
21
22
+ @ JsonInclude (JsonInclude .Include .NON_EMPTY )
19
23
public record Query (
20
24
String query ,
21
25
Map <String , String > variables
22
26
) {
23
27
28
+ private static final Logger log = LoggerFactory .getLogger (Query .class );
29
+
24
30
public static Query getQuery (String key , Map <String , String > variables ) throws URISyntaxException , IOException {
25
- var queryResourcePath = Query .class .getResource ("graphql. " + key );
31
+ var queryResourcePath = Query .class .getClassLoader (). getResource ("graphql/queries/ " + key + ".graphql" );
26
32
if (queryResourcePath == null ) {
27
33
throw new IllegalArgumentException ("No query found under query: [%s]" .formatted (key ));
28
34
}
29
35
30
- var graphqlQuery = Files .readString (
31
- Paths .get (queryResourcePath .toURI ())
32
- );
36
+ var graphqlQuery = Files .readString (Paths .get (queryResourcePath .toURI ()));
33
37
34
38
return new Query (graphqlQuery , variables );
35
39
}
@@ -41,14 +45,18 @@ public static Query getQuery(String key) throws URISyntaxException, IOException
41
45
public CompletableFuture <QueryResponse > executeQuery (HttpClient client , ObjectMapper mapper ) throws JsonProcessingException , URISyntaxException {
42
46
var queryBody = mapper .writeValueAsString (this );
43
47
44
- var request = HttpRequest .newBuilder ()
45
- .header ("Authorization" , "Self %s" .formatted (GlobalEnv .envOrThrow ("DEVMARKT_BOT_JWT_TOKEN" )))
46
- .uri (new URI (GlobalEnv .envOrThrow ("BACKEND_URL" ) + "/graphql" ))
48
+ var request = HttpRequest .newBuilder (URI .create (DevmarktBot .backendUrl + "/graphql" ))
49
+ .header ("Authorization" , "Self %s" .formatted (DevmarktBot .backendJwtToken ))
50
+ .header ("Content-Type" , "application/json" )
51
+ .header ("Accept" , "application/json" )
47
52
.POST (HttpRequest .BodyPublishers .ofString (queryBody ))
48
53
.build ();
49
54
50
55
return client .sendAsync (request , HttpResponse .BodyHandlers .ofString ())
51
56
.thenApply (HttpResponse ::body )
57
+ .whenComplete (((s , throwable ) ->
58
+ log .debug (s ))
59
+ )
52
60
.thenApply (body -> {
53
61
try {
54
62
return mapper .readValue (body , QueryResponse .class );
0 commit comments