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/04/23 13:38:22 UTC

[sling-org-apache-sling-graphql-core] branch master updated: SLING-10118 - refactor schema acquisition for clarity

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


The following commit(s) were added to refs/heads/master by this push:
     new 9abddb7  SLING-10118 - refactor schema acquisition for clarity
9abddb7 is described below

commit 9abddb77904a079a5c3c58de0901ac576fb6b53f
Author: Bertrand Delacretaz <bd...@apache.org>
AuthorDate: Fri Apr 23 15:38:09 2021 +0200

    SLING-10118 - refactor schema acquisition for clarity
---
 .../graphql/core/engine/DefaultQueryExecutor.java  | 54 +++++++++++-----------
 1 file changed, 27 insertions(+), 27 deletions(-)

diff --git a/src/main/java/org/apache/sling/graphql/core/engine/DefaultQueryExecutor.java b/src/main/java/org/apache/sling/graphql/core/engine/DefaultQueryExecutor.java
index 3591bca..c4f70e2 100644
--- a/src/main/java/org/apache/sling/graphql/core/engine/DefaultQueryExecutor.java
+++ b/src/main/java/org/apache/sling/graphql/core/engine/DefaultQueryExecutor.java
@@ -127,6 +127,28 @@ public class DefaultQueryExecutor implements QueryExecutor {
         int schemaCacheSize() default 128;
     }
 
+    private class ExecutionContext {
+        final GraphQLSchema schema;
+        final ExecutionInput input;
+
+        ExecutionContext(@NotNull String query, @NotNull Map<String, Object> variables, @NotNull Resource queryResource, @NotNull String[] selectors) 
+        throws ScriptException {
+            final String schemaSdl = prepareSchemaDefinition(schemaProvider, queryResource, selectors);
+            if (schemaSdl == null) {
+                throw new SlingGraphQLException(String.format("Cannot get a schema for resource %s and selectors %s.", queryResource,
+                        Arrays.toString(selectors)));
+            }
+            LOGGER.debug("Resource {} maps to GQL schema {}", queryResource.getPath(), schemaSdl);
+            final TypeDefinitionRegistry typeDefinitionRegistry = getTypeDefinitionRegistry(schemaSdl, queryResource, selectors);
+            schema = buildSchema(typeDefinitionRegistry, queryResource);
+            input = ExecutionInput.newExecutionInput()
+                    .query(query)
+                    .variables(variables)
+                    .build();
+
+        }
+    }
+
     @Activate
     public void activate(Config config) {
         int schemaCacheSize = config.schemaCacheSize();
@@ -141,19 +163,8 @@ public class DefaultQueryExecutor implements QueryExecutor {
     public ValidationResult validate(@NotNull String query, @NotNull Map<String, Object> variables, @NotNull Resource queryResource,
                                      @NotNull String[] selectors) {
         try {
-            String schemaDef = prepareSchemaDefinition(schemaProvider, queryResource, selectors);
-            if (schemaDef == null) {
-                throw new SlingGraphQLException(String.format("Cannot get a schema for resource %s and selectors %s.", queryResource,
-                        Arrays.toString(selectors)));
-            }
-            LOGGER.debug("Resource {} maps to GQL schema {}", queryResource.getPath(), schemaDef);
-            final TypeDefinitionRegistry typeDefinitionRegistry = getTypeDefinitionRegistry(schemaDef, queryResource, selectors);
-            final GraphQLSchema schema = buildSchema(typeDefinitionRegistry, queryResource);
-            ExecutionInput executionInput = ExecutionInput.newExecutionInput()
-                    .query(query)
-                    .variables(variables)
-                    .build();
-            ParseAndValidateResult parseAndValidateResult = ParseAndValidate.parseAndValidate(schema, executionInput);
+            final ExecutionContext ctx = new ExecutionContext(query, variables, queryResource, selectors);
+            ParseAndValidateResult parseAndValidateResult = ParseAndValidate.parseAndValidate(ctx.schema, ctx.input);
             if (!parseAndValidateResult.isFailure()) {
                 return DefaultValidationResult.Builder.newBuilder().withValidFlag(true).build();
             }
@@ -178,24 +189,13 @@ public class DefaultQueryExecutor implements QueryExecutor {
                                                 @NotNull Resource queryResource, @NotNull String[] selectors) {
         String schemaDef = null;
         try {
-            schemaDef = prepareSchemaDefinition(schemaProvider, queryResource, selectors);
-            if (schemaDef == null) {
-                throw new SlingGraphQLException(String.format("Cannot get a schema for resource %s and selectors %s.", queryResource,
-                        Arrays.toString(selectors)));
-            }
-            LOGGER.debug("Resource {} maps to GQL schema {}", queryResource.getPath(), schemaDef);
-            final TypeDefinitionRegistry typeDefinitionRegistry = getTypeDefinitionRegistry(schemaDef, queryResource, selectors);
-            final GraphQLSchema schema = buildSchema(typeDefinitionRegistry, queryResource);
-            final GraphQL graphQL = GraphQL.newGraphQL(schema).build();
+            final ExecutionContext ctx = new ExecutionContext(query, variables, queryResource, selectors);
+            final GraphQL graphQL = GraphQL.newGraphQL(ctx.schema).build();
             if (LOGGER.isDebugEnabled()) {
                 LOGGER.debug("Executing query\n[{}]\nat [{}] with variables [{}]",
                         cleanLog.sanitize(query), queryResource.getPath(), cleanLog.sanitize(variables.toString()));
             }
-            ExecutionInput ei = ExecutionInput.newExecutionInput()
-                    .query(query)
-                    .variables(variables)
-                    .build();
-            final ExecutionResult result = graphQL.execute(ei);
+            final ExecutionResult result = graphQL.execute(ctx.input);
             if (!result.getErrors().isEmpty()) {
                 StringBuilder errors = new StringBuilder();
                 for (GraphQLError error : result.getErrors()) {