mirror of
https://github.com/HangarMC/Hangar.git
synced 2025-02-23 15:12:52 +08:00
feat(query): allow querying stats
This commit is contained in:
parent
a5083d8219
commit
d4d6005214
@ -40,6 +40,14 @@ public final class QueryHelper {
|
||||
return EMPTY_LIST;
|
||||
}
|
||||
|
||||
public static void selectField(final DataFetchingEnvironment environment, final String tableSuffix, final String qglField, final String dbField, final String resultField) {
|
||||
if (environment.getSelectionSet().contains(qglField)) {
|
||||
final QueryBuilder queryBuilder = getActiveQueryBuilder(environment.getGraphQlContext());
|
||||
final String parentAlias = PrefixUtil.getParentAlias(environment.getExecutionStepInfo(), queryBuilder);
|
||||
queryBuilder.fields.add(STR."\{parentAlias.substring(0, parentAlias.length() - 1)}\{tableSuffix}.\{dbField} AS \{parentAlias}\{resultField}");
|
||||
}
|
||||
}
|
||||
|
||||
public static Object avatarUrl(final DataFetchingEnvironment environment, final FileService fileService, final AvatarService avatarService, final String avatarType) {
|
||||
final String idVar = avatarType.equals(AvatarService.USER) ? "userid" : "projectid";
|
||||
final String idField = avatarType.equals(AvatarService.USER) ? "uuid" : "id";
|
||||
|
@ -12,6 +12,7 @@ import static io.papermc.hangar.components.query.QueryHelper.EMPTY;
|
||||
import static io.papermc.hangar.components.query.QueryHelper.avatarUrl;
|
||||
import static io.papermc.hangar.components.query.QueryHelper.join;
|
||||
import static io.papermc.hangar.components.query.QueryHelper.query;
|
||||
import static io.papermc.hangar.components.query.QueryHelper.selectField;
|
||||
|
||||
@Controller
|
||||
public class QueryMappings {
|
||||
@ -70,15 +71,8 @@ public class QueryMappings {
|
||||
|
||||
@SchemaMapping(typeName = "Project", field = "namespace")
|
||||
public Object projectNamespace(final DataFetchingEnvironment environment) {
|
||||
final QueryBuilder queryBuilder = getActiveQueryBuilder(environment.getGraphQlContext());
|
||||
final String parentAlias = PrefixUtil.getParentAlias(environment.getExecutionStepInfo(), queryBuilder);
|
||||
final String parentTable = PrefixUtil.getParentTable(environment.getExecutionStepInfo(), queryBuilder);
|
||||
if (environment.getSelectionSet().contains("owner")) {
|
||||
queryBuilder.fields.add(STR."\{parentTable}owner_name AS \{parentAlias}namespace_owner");
|
||||
}
|
||||
if (environment.getSelectionSet().contains("slug")) {
|
||||
queryBuilder.fields.add(STR."\{parentTable}slug AS \{parentAlias}namespace_slug");
|
||||
}
|
||||
selectField(environment, "", "owner", "owner_name", "namespace_owner");
|
||||
selectField(environment, "", "slug", "slug", "namespace_slug");
|
||||
return null; // no need to dig deeper
|
||||
}
|
||||
|
||||
@ -90,4 +84,16 @@ public class QueryMappings {
|
||||
join(environment, "project_pages", "homepage", "id", "page_id", parentAlias + "homepage_id.");
|
||||
return EMPTY;
|
||||
}
|
||||
|
||||
@SchemaMapping(typeName = "Project", field = "stats")
|
||||
public Object projectStats(final DataFetchingEnvironment environment) {
|
||||
join(environment, "home_projects", "extra", "id", "id");
|
||||
selectField(environment, "_extra", "stars", "stars", "stats_stars");
|
||||
selectField(environment, "_extra", "watchers", "watchers", "stats_watchers");
|
||||
selectField(environment, "_extra", "views", "views", "stats_views");
|
||||
selectField(environment, "_extra", "downloads", "downloads", "stats_downloads");
|
||||
selectField(environment, "_extra", "recentViews", "recent_views", "stats_recentViews");
|
||||
selectField(environment, "_extra", "recentDownloads", "recent_downloads", "stats_recentDownloads");
|
||||
return null; // no need to dig deeper
|
||||
}
|
||||
}
|
||||
|
@ -47,7 +47,8 @@ type ProjectPage {
|
||||
parent: ProjectPage
|
||||
}
|
||||
|
||||
type ProjectStats {
|
||||
type ProjectStats implements Virtual {
|
||||
_virtual: Boolean
|
||||
views: Int
|
||||
downloads: Int
|
||||
recentViews: Int
|
||||
@ -57,9 +58,9 @@ type ProjectStats {
|
||||
}
|
||||
|
||||
type ProjectNamespace implements Virtual {
|
||||
_virtual: Boolean
|
||||
owner: String
|
||||
slug: String
|
||||
_virtual: Boolean
|
||||
}
|
||||
|
||||
interface Virtual {
|
||||
|
@ -13,6 +13,7 @@ import java.util.List;
|
||||
import java.util.Map;
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.condition.DisabledIfEnvironmentVariable;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
@ -195,7 +196,8 @@ class QueryMergerTest {
|
||||
}
|
||||
|
||||
// TODO solve these by always adding PKs to query on join
|
||||
//@Test
|
||||
@Test
|
||||
@DisabledIfEnvironmentVariable(named = "CI", matches = "true")
|
||||
void mergeNoPrimaryKey() throws JsonProcessingException {
|
||||
final List<Map<String, String>> input = new ArrayList<>();
|
||||
input.add(Map.of("projects_id", "1"));
|
||||
@ -211,7 +213,8 @@ class QueryMergerTest {
|
||||
compare(expected, merger.merge(input));
|
||||
}
|
||||
|
||||
//@Test
|
||||
@Test
|
||||
@DisabledIfEnvironmentVariable(named = "CI", matches = "true")
|
||||
void mergeNoPrimaryKey2() throws JsonProcessingException {
|
||||
final List<Map<String, String>> input = new ArrayList<>();
|
||||
input.add(Map.of("projects_id", "1", "projects_owner_name", "MiniDigger"));
|
||||
@ -227,7 +230,8 @@ class QueryMergerTest {
|
||||
compare(expected, merger.merge(input));
|
||||
}
|
||||
|
||||
//@Test
|
||||
@Test
|
||||
@DisabledIfEnvironmentVariable(named = "CI", matches = "true")
|
||||
void mergeNoPrimaryKey3() throws JsonProcessingException {
|
||||
final List<Map<String, String>> input = new ArrayList<>();
|
||||
input.add(Map.of("projects_name", "Test", "projects_owner_email", "Dum", "projects_owner_id", "1"));
|
||||
@ -243,6 +247,25 @@ class QueryMergerTest {
|
||||
compare(expected, merger.merge(input));
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisabledIfEnvironmentVariable(named = "CI", matches = "true")
|
||||
void mergeEmptyProjects() throws JsonProcessingException {
|
||||
final List<Map<String, String>> input = new ArrayList<>();
|
||||
final Map<String, String> thing = new HashMap<>();
|
||||
thing.put("users_name", "JarScanner");
|
||||
thing.put("users_projects_name", null);
|
||||
thing.put("users_projects_stats_stars", null);
|
||||
input.add(thing);
|
||||
|
||||
final Map<String, Object> expected = Map.of(
|
||||
"users", List.of(Map.of(
|
||||
"projects", List.of(),
|
||||
"name", "JarScanner"
|
||||
)));
|
||||
|
||||
compare(expected, merger.merge(input));
|
||||
}
|
||||
|
||||
@Test
|
||||
void mergeDeep() throws JsonProcessingException {
|
||||
final List<Map<String, String>> input = new ArrayList<>();
|
||||
|
Loading…
Reference in New Issue
Block a user