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

danhaywood pushed a commit to branch CAUSEWAY-3676
in repository https://gitbox.apache.org/repos/asf/causeway.git

commit ea7344c373ce291015edaaca9377ea927067ab9d
Author: danhaywood <[email protected]>
AuthorDate: Wed Feb 21 07:41:18 2024 +0000

    CAUSEWAY-3676: wip, more on 'rich' and 'simple' schemas; updates .gql tests
---
 .../core/config/CausewayConfiguration.java         |  5 +++-
 .../graphql/model/domain/GqlvAbstractCustom.java   |  3 ++
 ...pLevelRich.java => GqlvTopLevelRichSchema.java} | 32 +++++++++++-----------
 .../model/toplevel/GqlvTopLevelMutation.java       |  5 ++++
 .../GqlvTopLevelQueryForSimpleAndRich.java         | 17 +++++++++---
 .../viewer/graphql/model/toplevel/RichSchema.java  | 19 -------------
 .../graphql/model/toplevel/SimpleSchema.java       | 19 -------------
 .../integration/GraphQlSourceForCauseway.java      | 32 ++++++++++++----------
 8 files changed, 59 insertions(+), 73 deletions(-)

diff --git 
a/core/config/src/main/java/org/apache/causeway/core/config/CausewayConfiguration.java
 
b/core/config/src/main/java/org/apache/causeway/core/config/CausewayConfiguration.java
index 6084e42e23..2dcc1afc51 100644
--- 
a/core/config/src/main/java/org/apache/causeway/core/config/CausewayConfiguration.java
+++ 
b/core/config/src/main/java/org/apache/causeway/core/config/CausewayConfiguration.java
@@ -2379,12 +2379,15 @@ public class CausewayConfiguration {
                 public boolean isRich() {
                     return this == RICH_ONLY || this == SIMPLE_AND_RICH;
                 }
+                public boolean isSimple() {
+                    return this == SIMPLE_ONLY || this == SIMPLE_AND_RICH;
+                }
             }
 
             /**
              * Which {@link SchemaStyle} to expose.
              */
-            private SchemaStyle schemaStyle = SchemaStyle.RICH_ONLY;
+            private SchemaStyle schemaStyle = SchemaStyle.SIMPLE_AND_RICH;
 
             /**
              * If the {@link #getSchemaStyle()} is set to {@link 
SchemaStyle#SIMPLE_AND_RICH}, defines the name of the
diff --git 
a/viewers/graphql/model/src/main/java/org/apache/causeway/viewer/graphql/model/domain/GqlvAbstractCustom.java
 
b/viewers/graphql/model/src/main/java/org/apache/causeway/viewer/graphql/model/domain/GqlvAbstractCustom.java
index 1d6cee0e5b..8c8375c735 100644
--- 
a/viewers/graphql/model/src/main/java/org/apache/causeway/viewer/graphql/model/domain/GqlvAbstractCustom.java
+++ 
b/viewers/graphql/model/src/main/java/org/apache/causeway/viewer/graphql/model/domain/GqlvAbstractCustom.java
@@ -111,6 +111,9 @@ public abstract class GqlvAbstractCustom extends 
GqlvAbstract implements Parent
         return this.gqlObjectType;
     }
 
+    /**
+     * Implemented by top-level queries only.
+     */
     public void addDataFetchers() {
     }
 
diff --git 
a/viewers/graphql/model/src/main/java/org/apache/causeway/viewer/graphql/model/domain/rich/GqlvTopLevelRich.java
 
b/viewers/graphql/model/src/main/java/org/apache/causeway/viewer/graphql/model/domain/rich/GqlvTopLevelRichSchema.java
similarity index 82%
rename from 
viewers/graphql/model/src/main/java/org/apache/causeway/viewer/graphql/model/domain/rich/GqlvTopLevelRich.java
rename to 
viewers/graphql/model/src/main/java/org/apache/causeway/viewer/graphql/model/domain/rich/GqlvTopLevelRichSchema.java
index 639d3962d8..bc867796b0 100644
--- 
a/viewers/graphql/model/src/main/java/org/apache/causeway/viewer/graphql/model/domain/rich/GqlvTopLevelRich.java
+++ 
b/viewers/graphql/model/src/main/java/org/apache/causeway/viewer/graphql/model/domain/rich/GqlvTopLevelRichSchema.java
@@ -4,7 +4,8 @@ import java.util.ArrayList;
 import java.util.List;
 
 import graphql.schema.DataFetchingEnvironment;
-import graphql.schema.GraphQLObjectType;
+
+import static graphql.schema.GraphQLFieldDefinition.newFieldDefinition;
 
 import org.apache.causeway.core.config.CausewayConfiguration;
 import org.apache.causeway.viewer.graphql.model.context.Context;
@@ -12,7 +13,7 @@ import 
org.apache.causeway.viewer.graphql.model.domain.GqlvAbstractCustom;
 import org.apache.causeway.viewer.graphql.model.domain.GqlvScenario;
 import org.apache.causeway.viewer.graphql.model.domain.Parent;
 
-public class GqlvTopLevelRich
+public class GqlvTopLevelRichSchema
         extends GqlvAbstractCustom
         implements Parent {
 
@@ -23,8 +24,8 @@ public class GqlvTopLevelRich
 
     private final GqlvScenario scenario;
 
-    public GqlvTopLevelRich(final Context context) {
-        super("Query", context);
+    public GqlvTopLevelRichSchema(final Context context) {
+        super("RichSchema", context);
 
         graphqlConfiguration = 
context.causewayConfiguration.getViewer().getGraphql();
 
@@ -61,26 +62,25 @@ public class GqlvTopLevelRich
         }
 
         buildObjectType();
-    }
 
-    /**
-     * Never used.
-     *
-     * @param environment
-     * @return
-     */
-    @Override
-    protected Object fetchData(DataFetchingEnvironment environment) {
-        return null;
+        // the field is used if the schemaStyle is 'SIMPLE_AND_RICH', but is 
ignored/unused otherwise
+        setField(newFieldDefinition()
+                .name(graphqlConfiguration.getTopLevelFieldNameForRich())
+                .type(getGqlObjectType())
+                .build());
     }
 
     @Override
-    public GraphQLObjectType getGqlObjectType() {
-        return super.getGqlObjectType();
+    protected Object fetchData(DataFetchingEnvironment environment) {
+        return environment;
     }
 
     public void addDataFetchers() {
+        addDataFetchersForChildren();
+    }
 
+    @Override
+    protected void addDataFetchersForChildren() {
         domainServices.forEach(domainService -> {
             boolean actionsAdded = domainService.hasActions();
             if (actionsAdded) {
diff --git 
a/viewers/graphql/model/src/main/java/org/apache/causeway/viewer/graphql/model/toplevel/GqlvTopLevelMutation.java
 
b/viewers/graphql/model/src/main/java/org/apache/causeway/viewer/graphql/model/toplevel/GqlvTopLevelMutation.java
index 53d89711ff..563d26d663 100644
--- 
a/viewers/graphql/model/src/main/java/org/apache/causeway/viewer/graphql/model/toplevel/GqlvTopLevelMutation.java
+++ 
b/viewers/graphql/model/src/main/java/org/apache/causeway/viewer/graphql/model/toplevel/GqlvTopLevelMutation.java
@@ -79,6 +79,11 @@ public class GqlvTopLevelMutation
     }
 
     public void addDataFetchers() {
+        addDataFetchersForChildren();
+    }
+
+    @Override
+    protected void addDataFetchersForChildren() {
         actions.forEach(gqlvMutationForAction -> 
gqlvMutationForAction.addDataFetcher(this));
         properties.forEach(gqlvMutationForProperty -> 
gqlvMutationForProperty.addDataFetcher(this));
     }
diff --git 
a/viewers/graphql/model/src/main/java/org/apache/causeway/viewer/graphql/model/toplevel/GqlvTopLevelQueryForSimpleAndRich.java
 
b/viewers/graphql/model/src/main/java/org/apache/causeway/viewer/graphql/model/toplevel/GqlvTopLevelQueryForSimpleAndRich.java
index af2f6c1e8c..052e97b206 100644
--- 
a/viewers/graphql/model/src/main/java/org/apache/causeway/viewer/graphql/model/toplevel/GqlvTopLevelQueryForSimpleAndRich.java
+++ 
b/viewers/graphql/model/src/main/java/org/apache/causeway/viewer/graphql/model/toplevel/GqlvTopLevelQueryForSimpleAndRich.java
@@ -4,21 +4,30 @@ import graphql.schema.DataFetchingEnvironment;
 
 import org.apache.causeway.viewer.graphql.model.context.Context;
 import org.apache.causeway.viewer.graphql.model.domain.GqlvAbstractCustom;
+import 
org.apache.causeway.viewer.graphql.model.domain.rich.GqlvTopLevelRichSchema;
 
 public class GqlvTopLevelQueryForSimpleAndRich extends GqlvAbstractCustom {
 
-    private final RichSchema richSchema;
-    private final SimpleSchema simpleSchema;
+    private final GqlvTopLevelRichSchema richSchema;
 
     public GqlvTopLevelQueryForSimpleAndRich(final Context context) {
         super("SimpleAndRich", context);
 
-        addChildFieldFor(richSchema = new RichSchema(context));
-        addChildFieldFor(simpleSchema = new SimpleSchema(context));
+        addChildFieldFor(richSchema = new GqlvTopLevelRichSchema(context));
 
         buildObjectType();
     }
 
+    @Override
+    public void addDataFetchers() {
+        addDataFetchersForChildren();
+    }
+
+    @Override
+    protected void addDataFetchersForChildren() {
+        richSchema.addDataFetcher(this);
+    }
+
     @Override
     protected Object fetchData(DataFetchingEnvironment environment) {
         return environment;
diff --git 
a/viewers/graphql/model/src/main/java/org/apache/causeway/viewer/graphql/model/toplevel/RichSchema.java
 
b/viewers/graphql/model/src/main/java/org/apache/causeway/viewer/graphql/model/toplevel/RichSchema.java
deleted file mode 100644
index 39b6fd0a91..0000000000
--- 
a/viewers/graphql/model/src/main/java/org/apache/causeway/viewer/graphql/model/toplevel/RichSchema.java
+++ /dev/null
@@ -1,19 +0,0 @@
-package org.apache.causeway.viewer.graphql.model.toplevel;
-
-import graphql.schema.DataFetchingEnvironment;
-
-import org.apache.causeway.viewer.graphql.model.context.Context;
-import org.apache.causeway.viewer.graphql.model.domain.GqlvAbstractCustom;
-
-public class RichSchema extends GqlvAbstractCustom {
-
-    protected RichSchema(final Context context) {
-        super("rich", context);
-
-    }
-
-    @Override
-    protected Object fetchData(DataFetchingEnvironment environment) {
-        return environment;
-    }
-}
diff --git 
a/viewers/graphql/model/src/main/java/org/apache/causeway/viewer/graphql/model/toplevel/SimpleSchema.java
 
b/viewers/graphql/model/src/main/java/org/apache/causeway/viewer/graphql/model/toplevel/SimpleSchema.java
deleted file mode 100644
index 9557001d5a..0000000000
--- 
a/viewers/graphql/model/src/main/java/org/apache/causeway/viewer/graphql/model/toplevel/SimpleSchema.java
+++ /dev/null
@@ -1,19 +0,0 @@
-package org.apache.causeway.viewer.graphql.model.toplevel;
-
-import graphql.schema.DataFetchingEnvironment;
-
-import org.apache.causeway.viewer.graphql.model.context.Context;
-import org.apache.causeway.viewer.graphql.model.domain.GqlvAbstractCustom;
-
-public class SimpleSchema extends GqlvAbstractCustom {
-
-    protected SimpleSchema(final Context context) {
-        super("simple", context);
-
-    }
-
-    @Override
-    protected Object fetchData(DataFetchingEnvironment environment) {
-        return environment;
-    }
-}
diff --git 
a/viewers/graphql/viewer/src/main/java/org/apache/causeway/viewer/graphql/viewer/integration/GraphQlSourceForCauseway.java
 
b/viewers/graphql/viewer/src/main/java/org/apache/causeway/viewer/graphql/viewer/integration/GraphQlSourceForCauseway.java
index bb314e0351..bf0b606d6c 100644
--- 
a/viewers/graphql/viewer/src/main/java/org/apache/causeway/viewer/graphql/viewer/integration/GraphQlSourceForCauseway.java
+++ 
b/viewers/graphql/viewer/src/main/java/org/apache/causeway/viewer/graphql/viewer/integration/GraphQlSourceForCauseway.java
@@ -38,7 +38,7 @@ import 
org.apache.causeway.core.metamodel.specloader.SpecificationLoader;
 import org.apache.causeway.viewer.graphql.model.context.Context;
 import org.apache.causeway.viewer.graphql.model.registry.GraphQLTypeRegistry;
 import org.apache.causeway.viewer.graphql.model.toplevel.GqlvTopLevelMutation;
-import org.apache.causeway.viewer.graphql.model.domain.rich.GqlvTopLevelRich;
+import 
org.apache.causeway.viewer.graphql.model.domain.rich.GqlvTopLevelRichSchema;
 
 import lombok.val;
 
@@ -102,19 +102,7 @@ public class GraphQlSourceForCauseway implements 
GraphQlSource {
             throw new IllegalStateException("Metamodel is not fully 
introspected");
         }
 
-        GqlvAbstractCustom topLevelQuery;
-        switch (graphqlConfiguration.getSchemaStyle()) {
-            case SIMPLE_ONLY:
-                throw new IllegalStateException("SIMPLE_ONLY not yet 
supported");
-            case RICH_ONLY:
-                topLevelQuery = new GqlvTopLevelRich(context);
-                break;
-            case SIMPLE_AND_RICH:
-            default:
-                topLevelQuery = new GqlvTopLevelQueryForSimpleAndRich(context);
-                break;
-        }
-
+        val topLevelQuery = 
determineTopLevelQueryFrom(graphqlConfiguration.getSchemaStyle());
         val topLevelMutation = new GqlvTopLevelMutation(context);
 
         topLevelQuery.addDataFetchers();
@@ -132,5 +120,21 @@ public class GraphQlSourceForCauseway implements 
GraphQlSource {
                 .build();
     }
 
+    private GqlvAbstractCustom determineTopLevelQueryFrom(
+            final CausewayConfiguration.Viewer.Graphql.SchemaStyle 
schemaStyle) {
+        switch (schemaStyle) {
+            case SIMPLE_ONLY:
+                throw new IllegalStateException("SIMPLE_ONLY not yet 
supported");
+            case RICH_ONLY:
+                return new GqlvTopLevelRichSchema(context);
+            case SIMPLE_AND_RICH:
+                return new GqlvTopLevelQueryForSimpleAndRich(context);
+            default:
+                // shouldn't happen
+                throw new IllegalStateException(String.format(
+                        "Configured SchemaStyle '%s' not recognised", 
schemaStyle));
+        }
+    }
+
 
 }

Reply via email to