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/17 16:02:03 UTC

[sling-org-apache-sling-graphql-core] branch master updated (a3e75ae -> dee6f62)

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

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


    from a3e75ae  SLING-10309 - GraphQL results pagination
     new 3d570da  SLING-10309 - README tweaks for pagination and built-in directives
     new dee6f62  SLING-10309 - tweaks and cleanup

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                                          | 110 +++++++++++++++++----
 .../sling/graphql/api/pagination/Cursor.java       |   1 +
 .../sling/graphql/api/pagination/CursorTest.java   |   1 -
 src/test/resources/failing-fetcher-schema.txt      |   2 +
 .../apps/graphql/test/two/GQLschema.jsp            |   4 +-
 .../apps/graphql/test/two/testing.GQLschema.jsp    |   4 +-
 6 files changed, 100 insertions(+), 22 deletions(-)

[sling-org-apache-sling-graphql-core] 02/02: SLING-10309 - tweaks and cleanup

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

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

commit dee6f62a86964855fc2c3072d553be1f7e393287
Author: Bertrand Delacretaz <bd...@apache.org>
AuthorDate: Mon May 17 17:59:51 2021 +0200

    SLING-10309 - tweaks and cleanup
---
 src/main/java/org/apache/sling/graphql/api/pagination/Cursor.java     | 1 +
 src/test/java/org/apache/sling/graphql/api/pagination/CursorTest.java | 1 -
 src/test/resources/failing-fetcher-schema.txt                         | 2 ++
 .../resources/initial-content/apps/graphql/test/two/GQLschema.jsp     | 4 +++-
 .../initial-content/apps/graphql/test/two/testing.GQLschema.jsp       | 4 +++-
 5 files changed, 9 insertions(+), 3 deletions(-)

diff --git a/src/main/java/org/apache/sling/graphql/api/pagination/Cursor.java b/src/main/java/org/apache/sling/graphql/api/pagination/Cursor.java
index 7fe0308..a0f07b4 100644
--- a/src/main/java/org/apache/sling/graphql/api/pagination/Cursor.java
+++ b/src/main/java/org/apache/sling/graphql/api/pagination/Cursor.java
@@ -74,6 +74,7 @@ public class Cursor {
      * @param encoded the encoded value from which to generate a new cursor
      * @return a new cursor, if one can be generated; {@code null} otherwise
      */
+    @Nullable
     public static Cursor fromEncodedString(@Nullable String encoded) {
         if(encoded == null) {
             return null;
diff --git a/src/test/java/org/apache/sling/graphql/api/pagination/CursorTest.java b/src/test/java/org/apache/sling/graphql/api/pagination/CursorTest.java
index 68e16be..4aeb425 100644
--- a/src/test/java/org/apache/sling/graphql/api/pagination/CursorTest.java
+++ b/src/test/java/org/apache/sling/graphql/api/pagination/CursorTest.java
@@ -26,7 +26,6 @@ import static org.junit.Assert.assertNull;
 import java.util.UUID;
 
 import org.apache.sling.graphql.api.SlingGraphQLException;
-import org.apache.sling.graphql.api.pagination.Cursor;
 import org.junit.Test;
 
 public class CursorTest {
diff --git a/src/test/resources/failing-fetcher-schema.txt b/src/test/resources/failing-fetcher-schema.txt
index a559d0f..5bf03ce 100644
--- a/src/test/resources/failing-fetcher-schema.txt
+++ b/src/test/resources/failing-fetcher-schema.txt
@@ -16,6 +16,8 @@
 # * under the License.
 
 # This directive maps fields to our Sling data fetchers
+# It is not needed anymore since SLING-10375, but still supported
+# for backwards compatiblity with existing schemas.
 directive @fetcher(
     name : String,
     options : String = "",
diff --git a/src/test/resources/initial-content/apps/graphql/test/two/GQLschema.jsp b/src/test/resources/initial-content/apps/graphql/test/two/GQLschema.jsp
index ec3fb81..fae8501 100644
--- a/src/test/resources/initial-content/apps/graphql/test/two/GQLschema.jsp
+++ b/src/test/resources/initial-content/apps/graphql/test/two/GQLschema.jsp
@@ -18,6 +18,8 @@
 --%>
 
 # This directive maps fields to our Sling data fetchers
+# It is not needed anymore since SLING-10375, but still supported
+# for backwards compatiblity with existing schemas.
 directive @fetcher(
     name : String,
     options : String = "",
@@ -32,4 +34,4 @@ type SlingResource {
   path: String
   resourceType: String
   name: String
-}
\ No newline at end of file
+}
diff --git a/src/test/resources/initial-content/apps/graphql/test/two/testing.GQLschema.jsp b/src/test/resources/initial-content/apps/graphql/test/two/testing.GQLschema.jsp
index b0cf1da..ec31f69 100644
--- a/src/test/resources/initial-content/apps/graphql/test/two/testing.GQLschema.jsp
+++ b/src/test/resources/initial-content/apps/graphql/test/two/testing.GQLschema.jsp
@@ -18,6 +18,8 @@
 --%>
 
 # This directive maps fields to our Sling data fetchers
+# It is not needed anymore since SLING-10375, but still supported
+# for backwards compatiblity with existing schemas.
 directive @fetcher(
     name : String,
     options : String = "",
@@ -30,4 +32,4 @@ type Query {
 
 type TestData {
   farenheit: Int @fetcher(name:"test/pipe" options:"farenheit")
-}
\ No newline at end of file
+}

[sling-org-apache-sling-graphql-core] 01/02: SLING-10309 - README tweaks for pagination and built-in directives

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

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

commit 3d570da635fcd57f35d5d15d3c67696e14a0c53c
Author: Bertrand Delacretaz <bd...@apache.org>
AuthorDate: Mon May 17 17:59:28 2021 +0200

    SLING-10309 - README tweaks for pagination and built-in directives
---
 README.md | 110 +++++++++++++++++++++++++++++++++++++++++++++++++++-----------
 1 file changed, 91 insertions(+), 19 deletions(-)

diff --git a/README.md b/README.md
index d80f8a2..f14fa53 100644
--- a/README.md
+++ b/README.md
@@ -84,12 +84,15 @@ schemas dynamically, taking request selectors into account.
 Unless you have specific needs not covered by this mechanism, there's no need to implement your
 own `SchemaProvider` services.
 
-## Built-in directives
+## Built-in GraphQL Schema Directives
 
-Version 0.0.10 of the Apache Sling GraphQL Core introduces the concept of built-in directives. The `@fetcher` and
-`@resolver` directives were supported before as well as part of the schema definition. However, starting from version
-0.0.10 their schema definition is redundant, since all schemas will now be automatically extended to provide support
-for the built-in directives.
+Since version 0.0.10 of this module, a number of GraphQL schema directives are built-in to support specific
+features. As of that version, the `@fetcher`, `@resolver` and `@connection` directives described below
+can be used directly, without having to declare them explicitly in the schema with the `directive`
+statement that was required before.
+
+Declaring these directives explicitly is still supported for backwards compatibility with existing
+schemas, but not needed anymore.
 
 ### SlingDataFetcher selection using the `@fetcher` directive
 
@@ -102,11 +105,12 @@ The following built-in `@fetcher` directive is defined by this module:
         source : String = ""
     ) on FIELD_DEFINITION
 
-A field using the built-in `@fetcher` directive allows selecting a specific `SlingDataFetcher` service to return the appropriate data.
+It allows for selecting a specific `SlingDataFetcher` service to return the appropriate data, as in the
+examples below.
 
 Fileds which do not have such a directive will be retrieved using the default data fetcher.
 
-Here's a simple example, the test code has more:
+Here are a few examples, the test code has more of them:
 
     type Query {
       withTestingSelector : TestData @fetcher(name:"test/pipe")
@@ -163,7 +167,7 @@ directive has the following definition:
       for: String!
     ) on FIELD_DEFINITION
 
-With this in mind, your schema that supports pagination can look like:
+To allow schemas to be ehanced with pagination support, like in this example:
 
     type Query {
         paginatedHumans (after : String, limit : Int) : HumanConnection @connection(for: "Human") @fetcher(name:"humans/connection")
@@ -175,6 +179,28 @@ With this in mind, your schema that supports pagination can look like:
         address: String
     }
 
+Using this directive as in the above example adds the following types to the schema to provide paginated
+output that follows the Relay spec:
+
+    type PageInfo {
+        startCursor : String
+        endCursor : String
+        hasPreviousPage : Boolean
+        hasNextPage : Boolean
+    }
+
+    type HumanEdge {
+        cursor: String
+        node: Human
+    }
+
+    type HumanConnection {
+        edges : [HumanEdge]
+        pageInfo : PageInfo
+    }
+
+### How to implement a SlingDataFetcher that provides a paginated result set
+
 The [GenericConnection](./src/main/java/org/apache/sling/graphql/core/helpers/pagination/GenericConnection.java) class,
 together with the [`org.apache.sling.graphql.api.pagination`](./src/main/java/org/apache/sling/graphql/api/pagination) API
 provide support for paginated results. With this utility class, you just need to supply an `Iterator` on your data, a
@@ -183,19 +209,65 @@ page start and length.
 
 The [QueryDataFetcherComponent](./src/test/java/org/apache/sling/graphql/core/mocks/QueryDataFetcherComponent.java) provides a usage example: 
 
-    // fake test data simulating a query
-    final List<Resource> data = new ArrayList<>();
-    data.add(env.getCurrentResource());
-    data.add(env.getCurrentResource().getParent());
-    data.add(env.getCurrentResource().getParent().getParent());
+    @Override
+    public Object get(SlingDataFetcherEnvironment env) throws Exception {
+      // fake test data simulating a query
+      final List<Resource> data = new ArrayList<>();
+      data.add(env.getCurrentResource());
+      data.add(env.getCurrentResource().getParent());
+      data.add(env.getCurrentResource().getParent().getParent());
+
+      // Define how to build a unique cursor that points to one of our data objects
+      final Function<Resource, String> cursorStringProvider = r -> r.getPath();
+
+      // return a GenericConnection that the library will introspect and serialize
+      return new GenericConnection.Builder<>(data.iterator(), cursorStringProvider)
+        .withLimit(5)
+        .build();
+    }
 
-    // how to build a unique cursor that points to one of our data objects
-    final Function<Resource, String> cursorStringProvider = r -> r.getPath();
+The above data fetcher code produces the following output, with the `GenericConnection` helper taking
+care of the pagination logic and of generating the required data:
+
+    {
+      "data": {
+        "oneSchemaQuery": {
+          "pageInfo": {
+            "startCursor": "L2NvbnRlbnQvZ3JhcGhxbC9vbmU=",
+            "endCursor": "L2NvbnRlbnQ=",
+            "hasPreviousPage": false,
+            "hasNextPage": false
+          },
+          "edges": [
+            {
+              "cursor": "L2NvbnRlbnQvZ3JhcGhxbC9vbmU=",
+              "node": {
+                "path": "/content/graphql/one",
+                "resourceType": "graphql/test/one"
+              }
+            },
+            {
+              "cursor": "L2NvbnRlbnQvZ3JhcGhxbA==",
+              "node": {
+                "path": "/content/graphql",
+                "resourceType": "graphql/test/root"
+              }
+            },
+            {
+              "cursor": "L2NvbnRlbnQ=",
+              "node": {
+                "path": "/content",
+                "resourceType": "sling:OrderedFolder"
+              }
+            }
+          ]
+        }
+      }
+    }
 
-    // return a GenericConnection that the library will introspect and serialize
-    return new GenericConnection.Builder<>(data.iterator(), cursorStringProvider)
-      withLimit(5)
-      .build();
+Usage of this `GenericConnection` helper is optional, although recommended for ease of use and consistency. As long
+as the `SlingDataFetcher` provides a result that implements the [`org.apache.sling.graphql.api.pagination.Connection`](./src/main/java/org/apache/sling/graphql/api/pagination/Connection.java),
+the output will be according to the Relay spec.
 
 ## Caching: Persisted queries API