Skip to content

Commit

Permalink
feat(query): allow querying stats
Browse files Browse the repository at this point in the history
  • Loading branch information
MiniDigger committed Nov 3, 2023
1 parent 18128f5 commit 34f6bef
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,14 @@ public static List<Object> join(final DataFetchingEnvironment environment, final
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";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
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 {
Expand Down Expand Up @@ -70,15 +71,8 @@ public Object userUrl(final DataFetchingEnvironment environment) {

@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
}

Expand All @@ -90,4 +84,16 @@ public Object projectHomepage(final DataFetchingEnvironment environment) {
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
}
}
7 changes: 4 additions & 3 deletions backend/src/main/resources/graphql/schema.graphqls
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ type ProjectPage {
parent: ProjectPage
}

type ProjectStats {
type ProjectStats implements Virtual {
_virtual: Boolean
views: Int
downloads: Int
recentViews: Int
Expand All @@ -56,10 +57,10 @@ type ProjectStats {
watchers: Int
}

type ProjectNamespace implements Virtual{
type ProjectNamespace implements Virtual {
_virtual: Boolean
owner: String
slug: String
_virtual: Boolean
}

interface Virtual {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
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.*;

Expand Down Expand Up @@ -195,7 +196,8 @@ void mergeNoPrimaryKeyNamespace() throws JsonProcessingException {
}

// 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"));
Expand All @@ -211,7 +213,8 @@ void mergeNoPrimaryKey() throws JsonProcessingException {
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"));
Expand All @@ -227,7 +230,8 @@ void mergeNoPrimaryKey2() throws JsonProcessingException {
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"));
Expand All @@ -243,6 +247,25 @@ void mergeNoPrimaryKey3() throws JsonProcessingException {
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<>();
Expand Down

0 comments on commit 34f6bef

Please sign in to comment.