-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
pipinet
committed
Oct 9, 2024
1 parent
f2db2e5
commit 213bf85
Showing
16 changed files
with
85 additions
and
293 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
37
graphql/src/main/java/com/qwlabs/graphql/relay/Connection.java
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
45 changes: 0 additions & 45 deletions
45
graphql/src/main/java/com/qwlabs/graphql/relay/InvalidCursorException.java
This file was deleted.
Oops, something went wrong.
45 changes: 0 additions & 45 deletions
45
graphql/src/main/java/com/qwlabs/graphql/relay/InvalidPageSizeException.java
This file was deleted.
Oops, something went wrong.
41 changes: 0 additions & 41 deletions
41
graphql/src/main/java/com/qwlabs/graphql/relay/PageInfo.java
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.