-
Notifications
You must be signed in to change notification settings - Fork 0
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
1 parent
48ac4f4
commit 8ce9f26
Showing
9 changed files
with
154 additions
and
13 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,24 +5,34 @@ | |
import com.netflix.graphql.dgs.InputArgument; | ||
import com.sarya.graphql.service.codegen.types.Customer; | ||
import com.sarya.graphql.service.codegen.types.PaginationInput; | ||
import com.sarya.graphql.service.util.pagination.CursorDecoder; | ||
import com.sarya.graphql.service.util.pagination.CursorEncoder; | ||
import com.sarya.graphql.service.util.pagination.GenericConnection; | ||
import graphql.relay.Connection; | ||
import graphql.relay.SimpleListConnection; | ||
import graphql.schema.DataFetchingEnvironment; | ||
|
||
import java.util.List; | ||
import java.util.UUID; | ||
import lombok.AllArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
|
||
@DgsComponent | ||
@Slf4j | ||
@AllArgsConstructor | ||
public class CustomerResolver { | ||
|
||
private final CursorEncoder cursorEncoder; | ||
private final CursorDecoder cursorDecoder; | ||
|
||
@DgsQuery | ||
Connection<Customer> fetchCustomers( | ||
DataFetchingEnvironment dfe, | ||
@InputArgument PaginationInput paginationInput | ||
) { | ||
log.info("pagination Input: {}", paginationInput); | ||
int offset = cursorDecoder.apply(paginationInput.getAfter(), 0); | ||
var customer = Customer.newBuilder().name("James").customerId(UUID.randomUUID()) | ||
.email("[email protected]").build(); | ||
return new SimpleListConnection<>(List.of(customer)).get(dfe); | ||
return new GenericConnection<>(cursorEncoder, List.of(customer), offset).get(dfe); | ||
} | ||
} |
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
26 changes: 26 additions & 0 deletions
26
src/main/java/com/sarya/graphql/service/util/pagination/CursorDecoder.java
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,26 @@ | ||
package com.sarya.graphql.service.util.pagination; | ||
|
||
import com.google.common.base.Strings; | ||
import java.nio.charset.StandardCharsets; | ||
import java.util.Base64; | ||
import java.util.function.BiFunction; | ||
import org.apache.tomcat.util.codec.binary.StringUtils; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
public class CursorDecoder implements BiFunction<String, Integer, Integer> { | ||
|
||
@Override | ||
public Integer apply(String cursor, Integer defaultValue) { | ||
if (Strings.isNullOrEmpty(cursor)) { | ||
return defaultValue; | ||
} | ||
var hash = Base64.getDecoder().decode(cursor.getBytes(StandardCharsets.UTF_8)); | ||
var cursorLiteral = StringUtils.newStringUtf8(hash); | ||
try { | ||
return (Integer.valueOf(cursorLiteral.substring(5))); | ||
} catch (NumberFormatException nfe) { | ||
throw new RuntimeException("unparsable cursor: {}" + cursor); | ||
} | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
src/main/java/com/sarya/graphql/service/util/pagination/CursorEncoder.java
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,16 @@ | ||
package com.sarya.graphql.service.util.pagination; | ||
|
||
import java.nio.charset.StandardCharsets; | ||
import java.util.Base64; | ||
import java.util.function.Function; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
public class CursorEncoder implements Function<Integer, String> { | ||
|
||
@Override | ||
public String apply(Integer offset) { | ||
byte[] bytes = ("prefix" + offset).getBytes(StandardCharsets.UTF_8); | ||
return Base64.getEncoder().encodeToString(bytes); | ||
} | ||
} |
57 changes: 57 additions & 0 deletions
57
src/main/java/com/sarya/graphql/service/util/pagination/GenericConnection.java
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,57 @@ | ||
package com.sarya.graphql.service.util.pagination; | ||
|
||
import graphql.relay.Connection; | ||
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 graphql.schema.DataFetcher; | ||
import graphql.schema.DataFetchingEnvironment; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import lombok.AllArgsConstructor; | ||
|
||
@AllArgsConstructor | ||
public class GenericConnection<T> implements DataFetcher<Connection<T>> { | ||
|
||
private final CursorEncoder cursorEncoder; | ||
|
||
private final List<T> data; | ||
private final int offset; | ||
|
||
@Override | ||
public Connection<T> get(DataFetchingEnvironment environment) { | ||
List<Edge<T>> edges = buildEdges(); | ||
|
||
if (edges.size() == 0) { | ||
return emptyConnection(); | ||
} | ||
|
||
var firstEdge = edges.get(0); | ||
var lastEdge = edges.get(edges.size() - 1); | ||
|
||
return new DefaultConnection<>( | ||
edges, | ||
new GenericPageInfo(firstEdge.getCursor(), lastEdge.getCursor(), false, false, edges.size()) | ||
); | ||
} | ||
|
||
private Connection<T> emptyConnection() { | ||
PageInfo pageInfo = new DefaultPageInfo(null, null, false, false); | ||
return new DefaultConnection<>(List.of(), pageInfo); | ||
} | ||
|
||
private List<Edge<T>> buildEdges() { | ||
List<Edge<T>> edges = new ArrayList<>(); | ||
int ix = 0; | ||
for (T object : data) { | ||
edges.add( | ||
new DefaultEdge<>(object, new DefaultConnectionCursor(cursorEncoder.apply(offset + ix++))) | ||
); | ||
} | ||
return edges; | ||
} | ||
|
||
} |
22 changes: 22 additions & 0 deletions
22
src/main/java/com/sarya/graphql/service/util/pagination/GenericPageInfo.java
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,22 @@ | ||
package com.sarya.graphql.service.util.pagination; | ||
|
||
import graphql.relay.ConnectionCursor; | ||
import graphql.relay.DefaultPageInfo; | ||
import lombok.ToString; | ||
|
||
@ToString | ||
public class GenericPageInfo extends DefaultPageInfo { | ||
|
||
private final long totalCount; | ||
|
||
public GenericPageInfo( | ||
ConnectionCursor startCursor, | ||
ConnectionCursor endCursor, | ||
boolean hasPreviousPage, | ||
boolean hasNextPage, | ||
long totalCount | ||
) { | ||
super(startCursor, endCursor, hasPreviousPage, hasNextPage); | ||
this.totalCount = totalCount; | ||
} | ||
} |
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