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 2020/04/28 11:33:02 UTC

[sling-whiteboard] branch master updated: Fix output format to adhere to GraphQL spec

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-whiteboard.git


The following commit(s) were added to refs/heads/master by this push:
     new 61a9c57  Fix output format to adhere to GraphQL spec
     new 9e45e85  Merge pull request #56 from stefangrimm/output-format-fix
61a9c57 is described below

commit 61a9c57d0f2a825e5fd617500d143e846f0e14b9
Author: Stefan Grimm <sg...@adobe.com>
AuthorDate: Tue Apr 28 10:37:39 2020 +0200

    Fix output format to adhere to GraphQL spec
    
    - Use "data" top-level container
    - Correct error handling for query issues
---
 .../scripting/gql/engine/GraphQLScriptEngine.java      |  5 +----
 .../sling/scripting/graphql/it/BasicContentIT.java     |  6 +++---
 .../sling/scripting/graphql/it/GraphQLServletIT.java   | 18 +++++++++---------
 3 files changed, 13 insertions(+), 16 deletions(-)

diff --git a/graphql-scripting/src/main/java/org/apache/sling/scripting/gql/engine/GraphQLScriptEngine.java b/graphql-scripting/src/main/java/org/apache/sling/scripting/gql/engine/GraphQLScriptEngine.java
index 2a6c07f..297e41b 100644
--- a/graphql-scripting/src/main/java/org/apache/sling/scripting/gql/engine/GraphQLScriptEngine.java
+++ b/graphql-scripting/src/main/java/org/apache/sling/scripting/gql/engine/GraphQLScriptEngine.java
@@ -75,10 +75,7 @@ public class GraphQLScriptEngine extends AbstractScriptEngine {
     }
 
     public static void sendJSON(PrintWriter out, ExecutionResult result) throws ScriptException {
-        if (!result.getErrors().isEmpty()) {
-            throw new ScriptException(("GraphQL query failed:" + result.getErrors()));
-        }
-        final Object data = result.getData();
+        final Object data = result.toSpecification();
         if (data == null) {
             throw new ScriptException("No data");
         }
diff --git a/graphql-scripting/src/test/java/org/apache/sling/scripting/graphql/it/BasicContentIT.java b/graphql-scripting/src/test/java/org/apache/sling/scripting/graphql/it/BasicContentIT.java
index a652340..ea00a31 100644
--- a/graphql-scripting/src/test/java/org/apache/sling/scripting/graphql/it/BasicContentIT.java
+++ b/graphql-scripting/src/test/java/org/apache/sling/scripting/graphql/it/BasicContentIT.java
@@ -58,8 +58,8 @@ public class BasicContentIT extends GraphQLScriptingTestSupport {
     public void testJsonContent() throws Exception {
         final String path = "/graphql/one";
         final String json = getContent(path + ".json");
-        assertThat(json, hasJsonPath("$.currentResource"));
-        assertThat(json, hasJsonPath("$.currentResource.path", equalTo("/content/graphql/one")));
-        assertThat(json, hasJsonPath("$.currentResource.resourceType", equalTo("graphql/test/one")));
+        assertThat(json, hasJsonPath("$.data.currentResource"));
+        assertThat(json, hasJsonPath("$.data.currentResource.path", equalTo("/content/graphql/one")));
+        assertThat(json, hasJsonPath("$.data.currentResource.resourceType", equalTo("graphql/test/one")));
     }
 }
diff --git a/graphql-scripting/src/test/java/org/apache/sling/scripting/graphql/it/GraphQLServletIT.java b/graphql-scripting/src/test/java/org/apache/sling/scripting/graphql/it/GraphQLServletIT.java
index 41d8057..62fa9e4 100644
--- a/graphql-scripting/src/test/java/org/apache/sling/scripting/graphql/it/GraphQLServletIT.java
+++ b/graphql-scripting/src/test/java/org/apache/sling/scripting/graphql/it/GraphQLServletIT.java
@@ -72,25 +72,25 @@ public class GraphQLServletIT extends GraphQLScriptingTestSupport {
     @Test
     public void testGqlExt() throws Exception {
         final String json = getContent("/graphql/two.gql", "query", "{ currentResource { resourceType name } }");
-        assertThat(json, hasJsonPath("$.currentResource.resourceType", equalTo("graphql/test/two")));
-        assertThat(json, hasJsonPath("$.currentResource.name", equalTo("two")));
-        assertThat(json, hasNoJsonPath("$.currentResource.path"));
+        assertThat(json, hasJsonPath("$.data.currentResource.resourceType", equalTo("graphql/test/two")));
+        assertThat(json, hasJsonPath("$.data.currentResource.name", equalTo("two")));
+        assertThat(json, hasNoJsonPath("$.data.currentResource.path"));
     }
 
     @Test
     public void testGqlExtWithPost() throws Exception {
         final String json = getContentWithPost("/graphql/two.gql", "{ currentResource { resourceType name } }", null);
-        assertThat(json, hasJsonPath("$.currentResource.resourceType", equalTo("graphql/test/two")));
-        assertThat(json, hasJsonPath("$.currentResource.name", equalTo("two")));
-        assertThat(json, hasNoJsonPath("$.currentResource.path"));
+        assertThat(json, hasJsonPath("$.data.currentResource.resourceType", equalTo("graphql/test/two")));
+        assertThat(json, hasJsonPath("$.data.currentResource.name", equalTo("two")));
+        assertThat(json, hasNoJsonPath("$.data.currentResource.path"));
     }
 
     @Test
     public void testOtherExt() throws Exception {
         final String json = getContent("/graphql/two.testing.otherExt", "query", "{ currentResource { path name } }");
-        assertThat(json, hasJsonPath("$.currentResource.path", equalTo("/content/graphql/two")));
-        assertThat(json, hasJsonPath("$.currentResource.name", equalTo("two")));
-        assertThat(json, hasNoJsonPath("$.currentResource.resourceType"));
+        assertThat(json, hasJsonPath("$.data.currentResource.path", equalTo("/content/graphql/two")));
+        assertThat(json, hasJsonPath("$.data.currentResource.name", equalTo("two")));
+        assertThat(json, hasNoJsonPath("$.data.currentResource.resourceType"));
         executeRequest("GET", "/graphql/two.otherExt", null, 404);
     }