You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by bd...@apache.org on 2021/05/12 10:37:37 UTC

[sling-org-apache-sling-graphql-core] branch SLING-10309/experiment updated (14f155d -> 672eb71)

This is an automated email from the ASF dual-hosted git repository.

bdelacretaz pushed a change to branch SLING-10309/experiment
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-graphql-core.git.


    from 14f155d  SLING-10309 - nullable annotations and checks, maximum limit
     new 7846654  SLING-10309 - add comments on public APIs
     new 672eb71  SLING-10309 - update README

The 2 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:
 README.md                                          | 10 ++++++++
 .../core/helpers/pagination/GenericConnection.java | 27 +++++++++++++++-------
 .../core/pagination/GenericConnectionTest.java     |  6 +++++
 3 files changed, 35 insertions(+), 8 deletions(-)

[sling-org-apache-sling-graphql-core] 02/02: SLING-10309 - update README

Posted by bd...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

bdelacretaz pushed a commit to branch SLING-10309/experiment
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-graphql-core.git

commit 672eb71fa8877d76808f9188d00d4a991498871c
Author: Bertrand Delacretaz <bd...@apache.org>
AuthorDate: Wed May 12 12:37:23 2021 +0200

    SLING-10309 - update README
---
 README.md | 10 ++++++++++
 1 file changed, 10 insertions(+)

diff --git a/README.md b/README.md
index 07ea4fc..a5b0e50 100644
--- a/README.md
+++ b/README.md
@@ -151,6 +151,16 @@ which have Java package names that start with `org.apache.sling`.
 The `<options>` and `<source>` arguments of the directive can be used by the
 `SlingTypeResolver` services to influence their behavior.
 
+## Result Set Pagination
+
+The [GenericConnection](./src/main/java/org/apache/sling/graphql/core/helpers/pagination/GenericConnection.java) class provides support
+for paginated results, following the [Relay Cursor Connections](https://relay.dev/graphql/connections.htm) specification.
+
+With this utility class, you just need to supply an `Iterator` on your data, a function to generate a string that represents the cursor
+for a given object, and optional parameters to control the page start and length.
+
+The [GenericConnectionTest](./src/test/java/org/apache/sling/graphql/core/pagination/GenericConnectionTest.java) class has concrete examples of that.
+
 ## Caching: Persisted queries API
 
 No matter how you decide to create your Sling GraphQL endpoints, you have the option to allow GraphQL clients to use persisted queries.

[sling-org-apache-sling-graphql-core] 01/02: SLING-10309 - add comments on public APIs

Posted by bd...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

bdelacretaz pushed a commit to branch SLING-10309/experiment
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-graphql-core.git

commit 784665416b65ed97ca06eda70e99a5a2ec07223d
Author: Bertrand Delacretaz <bd...@apache.org>
AuthorDate: Wed May 12 12:31:55 2021 +0200

    SLING-10309 - add comments on public APIs
---
 .../core/helpers/pagination/GenericConnection.java | 27 +++++++++++++++-------
 .../core/pagination/GenericConnectionTest.java     |  6 +++++
 2 files changed, 25 insertions(+), 8 deletions(-)

diff --git a/src/main/java/org/apache/sling/graphql/core/helpers/pagination/GenericConnection.java b/src/main/java/org/apache/sling/graphql/core/helpers/pagination/GenericConnection.java
index 163079a..cd4c4c1 100644
--- a/src/main/java/org/apache/sling/graphql/core/helpers/pagination/GenericConnection.java
+++ b/src/main/java/org/apache/sling/graphql/core/helpers/pagination/GenericConnection.java
@@ -34,6 +34,10 @@ import org.osgi.annotation.versioning.ConsumerType;
 
 /** As per https://relay.dev/graphql/connections.htm a "connection"
  *  is a page of results for a paginated query.
+ * 
+ *  Use the {@link #Builder} class to build a Connection that outputs
+ *  the supplied data, optionally sliced based on a "start after" cursor
+ *  and a limit on the number of items output.
 */
 @ConsumerType
 public class GenericConnection<T> implements Connection<T>, PageInfo {
@@ -54,14 +58,7 @@ public class GenericConnection<T> implements Connection<T>, PageInfo {
     private Boolean hasNextPage;
     private int limit = DEFAULT_LIMIT;
 
-    /** Build a Connection that will output the supplied data, optionally skipping items
-     *  at the beginning and considering a set maximum of items.
-     * 
-     *  @param dataIterator the connection's data - must include the item that startAfter points to
-     *  @param cursorStringProvider extracts a String from an object of type T to create a Cursor
-     *  @param startAfter if not null, data up to and including the item which has this cursor is ignored
-     *  @param maxItemsReturned at most this many items are considered
-    */
+    /** The Builder must be used to construct this */
     private GenericConnection(@NotNull Iterator<T> dataIterator, @NotNull Function<T, String> cursorStringProvider) {
         checkNotNull(dataIterator, "Data iterator");
         checkNotNull(cursorStringProvider, "Cursor string provider function");
@@ -177,10 +174,20 @@ public class GenericConnection<T> implements Connection<T>, PageInfo {
     public static class Builder<T> {
         private final GenericConnection<T> connection;
 
+        /** Builder for a Connection that will output the supplied data, optionally skipping items
+         *  at the beginning and considering a set maximum of items.
+         * 
+         *  @param dataIterator the connection's data - must include the item that startAfter points to if that
+         *      Cursor is set, but can contain less items that set by the "limit" parameter.          
+         *  @param cursorStringProvider extracts a String from an object of type T to create a Cursor
+        */
         public Builder(Iterator<T> dataIterator, Function<T, String> cursorStringProvider) {
             connection = new GenericConnection<>(dataIterator, cursorStringProvider);
         }
 
+        /** Set a limit on the number of items returned by the connection.
+         * @param limit must be <= MAX_LIMIT
+         */
         public Builder<T> withLimit(int limit) {
             if(limit < 0) {
                 limit = 0;
@@ -192,6 +199,9 @@ public class GenericConnection<T> implements Connection<T>, PageInfo {
             return this;
         }
 
+        /** If set, the connection will skip to the first item after
+         *  this Cursor.
+         */
         public Builder<T> withStartAfter(@Nullable Cursor c) {
             connection.startAfter = c;
             return this;
@@ -213,6 +223,7 @@ public class GenericConnection<T> implements Connection<T>, PageInfo {
             return this;
         }
 
+        /** Build the Connection - can only be called once */
         public Connection<T> build() {
             connection.initialize();
             return connection;
diff --git a/src/test/java/org/apache/sling/graphql/core/pagination/GenericConnectionTest.java b/src/test/java/org/apache/sling/graphql/core/pagination/GenericConnectionTest.java
index f7aa8bd..3a289d1 100644
--- a/src/test/java/org/apache/sling/graphql/core/pagination/GenericConnectionTest.java
+++ b/src/test/java/org/apache/sling/graphql/core/pagination/GenericConnectionTest.java
@@ -69,6 +69,12 @@ public class GenericConnectionTest {
     }
 
     @Test
+    public void noArguments() {
+        final Connection<Integer> c = new GenericConnection.Builder<>(data.iterator(), cursorize).build();
+        assertValues(c, 1, 5, false, false);
+    }
+
+    @Test
     public void minimalArguments() {
         final Connection<Integer> c = new GenericConnection.Builder<>(data.iterator(), cursorize)
             .withLimit(2)