Skip to content

Commit 384c4fa

Browse files
committed
update Query + QueryResponse
1 parent 7af0d76 commit 384c4fa

File tree

2 files changed

+22
-33
lines changed

2 files changed

+22
-33
lines changed

bot/src/main/java/club/devcord/devmarkt/requests/query/Query.java

+16-8
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
package club.devcord.devmarkt.requests.query;
22

3-
import club.devcord.devmarkt.env.GlobalEnv;
3+
import club.devcord.devmarkt.DevmarktBot;
4+
import com.fasterxml.jackson.annotation.JsonInclude;
45
import com.fasterxml.jackson.core.JsonProcessingException;
56
import com.fasterxml.jackson.databind.ObjectMapper;
7+
import org.slf4j.Logger;
8+
import org.slf4j.LoggerFactory;
69

710
import java.io.IOException;
811
import java.net.URI;
@@ -16,20 +19,21 @@
1619
import java.util.Map;
1720
import java.util.concurrent.CompletableFuture;
1821

22+
@JsonInclude(JsonInclude.Include.NON_EMPTY)
1923
public record Query(
2024
String query,
2125
Map<String, String> variables
2226
) {
2327

28+
private static final Logger log = LoggerFactory.getLogger(Query.class);
29+
2430
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");
2632
if (queryResourcePath == null) {
2733
throw new IllegalArgumentException("No query found under query: [%s]".formatted(key));
2834
}
2935

30-
var graphqlQuery = Files.readString(
31-
Paths.get(queryResourcePath.toURI())
32-
);
36+
var graphqlQuery = Files.readString(Paths.get(queryResourcePath.toURI()));
3337

3438
return new Query(graphqlQuery, variables);
3539
}
@@ -41,14 +45,18 @@ public static Query getQuery(String key) throws URISyntaxException, IOException
4145
public CompletableFuture<QueryResponse> executeQuery(HttpClient client, ObjectMapper mapper) throws JsonProcessingException, URISyntaxException {
4246
var queryBody = mapper.writeValueAsString(this);
4347

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")
4752
.POST(HttpRequest.BodyPublishers.ofString(queryBody))
4853
.build();
4954

5055
return client.sendAsync(request, HttpResponse.BodyHandlers.ofString())
5156
.thenApply(HttpResponse::body)
57+
.whenComplete(((s, throwable) ->
58+
log.debug(s))
59+
)
5260
.thenApply(body -> {
5361
try {
5462
return mapper.readValue(body, QueryResponse.class);
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,28 @@
11
package club.devcord.devmarkt.requests.query;
22

3+
import com.fasterxml.jackson.annotation.JsonProperty;
34
import com.fasterxml.jackson.core.JsonProcessingException;
5+
import com.fasterxml.jackson.databind.JsonNode;
46
import com.fasterxml.jackson.databind.ObjectMapper;
5-
import org.jetbrains.annotations.Nullable;
6-
7-
import java.io.IOException;
8-
import java.net.URISyntaxException;
9-
import java.net.http.HttpClient;
10-
import java.util.Collections;
117

128
public record QueryResponse(
13-
@Nullable String data,
14-
@Nullable String extensions,
15-
@Nullable String errors
9+
JsonNode data,
10+
String errors
1611
) {
1712

1813
public <T> T mapData(ObjectMapper mapper, Class<T> type) {
1914
try {
20-
return mapper.readValue(this.data, type);
15+
return mapper.treeToValue(data, type);
2116
} catch (JsonProcessingException e) {
2217
throw new RuntimeException(e);
2318
}
2419
}
2520

2621
public boolean isSuccessful() {
27-
return errors == null && (data != null && extensions != null);
22+
return errors == null && (data != null);
2823
}
2924

3025
public boolean isError() {
3126
return !isSuccessful();
3227
}
33-
34-
record User(
35-
String name,
36-
String password
37-
) {
38-
}
39-
40-
public static void main(String[] args) throws URISyntaxException, IOException {
41-
var userResponse = new QueryResponse("user record in json", "kp was", null);
42-
43-
var client = HttpClient.newHttpClient();
44-
45-
var user = Query.getQuery("user").executeQueryMapped(client, User.class);
46-
}
4728
}

0 commit comments

Comments
 (0)