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 3fa2708789517c80795a2ceb14abba7ec5d4c719
Author: danhaywood <[email protected]>
AuthorDate: Mon Feb 12 19:37:31 2024 +0000

    CAUSEWAY-3676: reintroduces factory method for GqlvDomainObject and 
...Service
---
 .../graphql/model/domain/GqlvAbstractCustom.java   |  8 +++----
 .../graphql/model/domain/GqlvDomainObject.java     | 13 +++++++++-
 .../graphql/model/domain/GqlvDomainService.java    | 28 +++++++++++-----------
 .../graphql/model/domain/GqlvScenarioGiven.java    |  4 ++--
 .../graphql/model/toplevel/GqlvTopLevelQuery.java  |  4 ++--
 5 files changed, 33 insertions(+), 24 deletions(-)

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 4c275cb813..32434ed56c 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
@@ -80,15 +80,13 @@ public abstract class GqlvAbstractCustom extends 
GqlvAbstract implements GqlvHol
     }
 
     protected void buildObjectTypeAndField(String fieldName) {
-        if (isBuilt()) {
-            return;
+        if (!isBuilt()) {
+            buildObjectType();
         }
 
-        val graphQLObjectType = buildObjectType();
-
         setField(newFieldDefinition()
                 .name(fieldName)
-                .type(graphQLObjectType)
+                .type(getGqlObjectType())
                 .build());
     }
 
diff --git 
a/viewers/graphql/model/src/main/java/org/apache/causeway/viewer/graphql/model/domain/GqlvDomainObject.java
 
b/viewers/graphql/model/src/main/java/org/apache/causeway/viewer/graphql/model/domain/GqlvDomainObject.java
index cc21f2b654..ab1c1fc88e 100644
--- 
a/viewers/graphql/model/src/main/java/org/apache/causeway/viewer/graphql/model/domain/GqlvDomainObject.java
+++ 
b/viewers/graphql/model/src/main/java/org/apache/causeway/viewer/graphql/model/domain/GqlvDomainObject.java
@@ -18,9 +18,11 @@
  */
 package org.apache.causeway.viewer.graphql.model.domain;
 
+import java.util.LinkedHashMap;
 import java.util.Map;
 import java.util.SortedMap;
 import java.util.TreeMap;
+import java.util.function.Function;
 
 import graphql.Scalars;
 import graphql.schema.DataFetcher;
@@ -67,9 +69,18 @@ public class GqlvDomainObject
 
     @Getter private final GraphQLInputObjectType gqlInputObjectType;
 
+    private final static Map<ObjectSpecification, GqlvDomainObject> 
domainObjectBySpec = new LinkedHashMap<>();
+
+    public static GqlvDomainObject of(
+            final ObjectSpecification objectSpecification,
+            final Holder holder,
+            final Context context) {
+        return domainObjectBySpec.computeIfAbsent(objectSpecification, spec -> 
new GqlvDomainObject(spec, holder, context));
+    }
+
     public GqlvDomainObject(
-            final GqlvDomainObject.Holder holder,
             final ObjectSpecification objectSpecification,
+            final Holder holder,
             final Context context) {
         super(TypeNames.objectTypeNameFor(objectSpecification), context);
 
diff --git 
a/viewers/graphql/model/src/main/java/org/apache/causeway/viewer/graphql/model/domain/GqlvDomainService.java
 
b/viewers/graphql/model/src/main/java/org/apache/causeway/viewer/graphql/model/domain/GqlvDomainService.java
index 64fb0d6aa3..aac70cdaed 100644
--- 
a/viewers/graphql/model/src/main/java/org/apache/causeway/viewer/graphql/model/domain/GqlvDomainService.java
+++ 
b/viewers/graphql/model/src/main/java/org/apache/causeway/viewer/graphql/model/domain/GqlvDomainService.java
@@ -20,9 +20,9 @@ package org.apache.causeway.viewer.graphql.model.domain;
 
 import java.util.LinkedHashMap;
 import java.util.Map;
+import java.util.function.Function;
 
 import graphql.schema.DataFetcher;
-import graphql.schema.GraphQLFieldDefinition;
 
 import static graphql.schema.FieldCoordinates.coordinates;
 import static graphql.schema.GraphQLFieldDefinition.newFieldDefinition;
@@ -48,18 +48,25 @@ public class GqlvDomainService
     @Getter private final ObjectSpecification objectSpecification;
     @Getter private final Object servicePojo;
 
-    String getLogicalTypeName() {
-        return objectSpecification.getLogicalTypeName();
-    }
-
     private final Map<String, GqlvAction> actions = new LinkedHashMap<>();
 
+    private final static Map<ObjectSpecification, GqlvDomainService> 
domainServiceBySpec = new LinkedHashMap<>();
+
+    public static GqlvDomainService of(
+            final ObjectSpecification objectSpecification,
+            final Holder holder,
+            final Object servicePojo,
+            final Context context) {
+        return domainServiceBySpec.computeIfAbsent(objectSpecification, spec 
-> new GqlvDomainService(spec, holder, servicePojo, context));
+    }
+
     public GqlvDomainService(
-            final GqlvDomainService.Holder holder,
             final ObjectSpecification objectSpecification,
+            final Holder holder,
             final Object servicePojo,
             final Context context) {
         super(TypeNames.objectTypeNameFor(objectSpecification), context);
+
         this.holder = holder;
         this.objectSpecification = objectSpecification;
         this.servicePojo = servicePojo;
@@ -86,18 +93,11 @@ public class GqlvDomainService
 
     private void addAction(final ObjectAction objectAction) {
         GqlvAction gqlvAction = new GqlvAction(this, objectAction, context);
-        addField(gqlvAction.getField());
+        addChildField(gqlvAction.getField());
         actions.put(objectAction.getId(), gqlvAction);
     }
 
 
-    private GraphQLFieldDefinition addField(GraphQLFieldDefinition field) {
-        if (field != null) {
-            gqlObjectTypeBuilder.field(field);
-        }
-        return field;
-    }
-
     public void addDataFetchers() {
         context.codeRegistryBuilder.dataFetcher(
                 holder.coordinatesFor(getField()),
diff --git 
a/viewers/graphql/model/src/main/java/org/apache/causeway/viewer/graphql/model/domain/GqlvScenarioGiven.java
 
b/viewers/graphql/model/src/main/java/org/apache/causeway/viewer/graphql/model/domain/GqlvScenarioGiven.java
index 34e8e33134..7c3c5c80d2 100644
--- 
a/viewers/graphql/model/src/main/java/org/apache/causeway/viewer/graphql/model/domain/GqlvScenarioGiven.java
+++ 
b/viewers/graphql/model/src/main/java/org/apache/causeway/viewer/graphql/model/domain/GqlvScenarioGiven.java
@@ -32,7 +32,7 @@ public class GqlvScenarioGiven
                 case VIEW_MODEL: // @DomainObject(nature=VIEW_MODEL)
                 case ENTITY:     // @DomainObject(nature=ENTITY)
 
-                    domainObjects.add(new GqlvDomainObject(this, objectSpec, 
context));
+                    domainObjects.add(new GqlvDomainObject(objectSpec, this, 
context));
 
                     break;
             }
@@ -42,7 +42,7 @@ public class GqlvScenarioGiven
             if (Objects.requireNonNull(objectSpec.getBeanSort()) == 
BeanSort.MANAGED_BEAN_CONTRIBUTING) { // @DomainService
                 
context.serviceRegistry.lookupBeanById(objectSpec.getLogicalTypeName())
                         .ifPresent(servicePojo -> {
-                            GqlvDomainService gqlvDomainService = new 
GqlvDomainService(this, objectSpec, servicePojo, context);
+                            GqlvDomainService gqlvDomainService = 
GqlvDomainService.of(objectSpec, this, servicePojo, context);
                             addChildField(gqlvDomainService.getField());
                             domainServices.add(gqlvDomainService);
                         });
diff --git 
a/viewers/graphql/model/src/main/java/org/apache/causeway/viewer/graphql/model/toplevel/GqlvTopLevelQuery.java
 
b/viewers/graphql/model/src/main/java/org/apache/causeway/viewer/graphql/model/toplevel/GqlvTopLevelQuery.java
index 1f75f41fa2..b3bfdcac82 100644
--- 
a/viewers/graphql/model/src/main/java/org/apache/causeway/viewer/graphql/model/toplevel/GqlvTopLevelQuery.java
+++ 
b/viewers/graphql/model/src/main/java/org/apache/causeway/viewer/graphql/model/toplevel/GqlvTopLevelQuery.java
@@ -32,7 +32,7 @@ public class GqlvTopLevelQuery
                 case VIEW_MODEL: // @DomainObject(nature=VIEW_MODEL)
                 case ENTITY:     // @DomainObject(nature=ENTITY)
 
-                    domainObjects.add(new GqlvDomainObject(this, objectSpec, 
context));
+                    domainObjects.add(new GqlvDomainObject(objectSpec, this, 
context));
 
                     break;
             }
@@ -44,7 +44,7 @@ public class GqlvTopLevelQuery
                 case MANAGED_BEAN_CONTRIBUTING: // @DomainService
                     
context.serviceRegistry.lookupBeanById(objectSpec.getLogicalTypeName())
                             .ifPresent(servicePojo -> {
-                                GqlvDomainService gqlvDomainService = new 
GqlvDomainService(this, objectSpec, servicePojo, context);
+                                GqlvDomainService gqlvDomainService = 
GqlvDomainService.of(objectSpec, this, servicePojo, context);
                                 addChildField(gqlvDomainService.getField());
                                 domainServices.add(gqlvDomainService);
                             });

Reply via email to