Skip to content

Commit

Permalink
chore: add Relays
Browse files Browse the repository at this point in the history
  • Loading branch information
pipinet committed Oct 9, 2024
1 parent f2db2e5 commit 213bf85
Show file tree
Hide file tree
Showing 16 changed files with 85 additions and 293 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ hibernatePluginVersion=6.6.0.Final
quarkusVersion=3.15.1
junitVersion=5.11.0
jmhVersion=1.37
guavaVersion=33.3.0-jre
guavaVersion=33.3.1-jre
slf4jVersion=2.1.0-alpha1
jandexVersion=3.2.2
hibernateVersion=6.6.0.Final
Expand Down
6 changes: 3 additions & 3 deletions gradle/bom.gradle
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
configurations.all {
// collect { configuration ->
// configuration.exclude group: 'junit', module: 'junit'
// }
collect { configuration ->
configuration.exclude group: 'com.google.code.findbugs', module: 'jsr305'
}
resolutionStrategy {
force "com.fasterxml.jackson.core:jackson-annotations:${jacksonVersion}"
force "com.fasterxml.jackson.core:jackson-core:${jacksonVersion}"
Expand Down
4 changes: 1 addition & 3 deletions graphql/build.gradle
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
dependencies {
api project(":panache")
api "com.google.guava:guava:${guavaVersion}"
api project(":jakarta-data")
api "com.graphql-java:graphql-java:${graphqlJavaVersion}"
api "jakarta.validation:jakarta.validation-api:${jakartaValidationApiVersion}"
api "com.fasterxml.jackson.core:jackson-databind:${jacksonVersion}"
}
13 changes: 7 additions & 6 deletions graphql/src/main/java/com/qwlabs/graphql/Gql.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.qwlabs.lang.F2;
import jakarta.validation.constraints.NotNull;
import lombok.extern.slf4j.Slf4j;

import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Optional;
import java.util.Objects;

@Slf4j
public class Gql {
Expand Down Expand Up @@ -43,11 +42,13 @@ public Gql input(Object input) {

public String build() {
Map<String, Object> result = new LinkedHashMap<>();
F2.ifPresent(operationName, name -> {
result.put("operationName", name);
});
if (Objects.nonNull(operationName)) {
result.put("operationName", operationName);
}
result.put("query", query);
Optional.ofNullable(variables).ifPresent(vars -> result.put("variables", variables));
if (Objects.nonNull(variables)) {
result.put("variables", variables);
}
try {
return initObjectMapper().writeValueAsString(result);
} catch (JsonProcessingException e) {
Expand Down
63 changes: 63 additions & 0 deletions graphql/src/main/java/com/qwlabs/graphql/Relays.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package com.qwlabs.graphql;

import com.qwlabs.jakarta.data.Pages;
import graphql.relay.Connection;
import graphql.relay.ConnectionCursor;
import graphql.relay.DefaultConnection;
import graphql.relay.DefaultConnectionCursor;
import graphql.relay.DefaultEdge;
import graphql.relay.DefaultPageInfo;
import graphql.relay.Edge;
import graphql.relay.PageInfo;
import jakarta.annotation.Nullable;
import jakarta.data.page.Page;
import jakarta.validation.constraints.NotNull;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Objects;
import java.util.function.Function;

public class Relays {

public static <E, P> Connection<P> of(@NotNull Page<E> page, @Nullable Function<E, P> mapper) {
if (Objects.isNull(mapper)) {
return (Connection<P>) of(page);
}
return of(Pages.map(page, mapper));
}

public static <P> Connection<P> of(@NotNull Page<P> page) {
Objects.requireNonNull(page, "page can not be null.");
List<Edge<P>> edges = buildEdges(page);
PageInfo pageInfo = buildPageInfo(page, edges);
return new DefaultConnection<>(edges, pageInfo);
}

private static <P> PageInfo buildPageInfo(Page<P> page, List<Edge<P>> edges) {
ConnectionCursor startCursor = null;
ConnectionCursor endCursor = null;
if (!edges.isEmpty()) {
startCursor = edges.get(0).getCursor();
endCursor = edges.get(edges.size() - 1).getCursor();
}
return new DefaultPageInfo(
startCursor,
endCursor,
page.hasPrevious(),
page.hasNext()
);
}

private static <P> List<Edge<P>> buildEdges(Page<P> page) {
long startAt = (page.pageRequest().page() - 1) * page.pageRequest().size();
List<Edge<P>> edges = new ArrayList<>(page.numberOfElements());
Iterator<P> iterator = page.content().iterator();
while (iterator.hasNext()) {
P node = iterator.next();
edges.add(new DefaultEdge<>(node, new DefaultConnectionCursor(String.valueOf(startAt++))));
}
return edges;
}
}
37 changes: 0 additions & 37 deletions graphql/src/main/java/com/qwlabs/graphql/relay/Connection.java

This file was deleted.

27 changes: 0 additions & 27 deletions graphql/src/main/java/com/qwlabs/graphql/relay/Edge.java

This file was deleted.

This file was deleted.

This file was deleted.

41 changes: 0 additions & 41 deletions graphql/src/main/java/com/qwlabs/graphql/relay/PageInfo.java

This file was deleted.

Loading

0 comments on commit 213bf85

Please sign in to comment.