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 13b9d6724a2d6139ec8277ff6379fb399701e497 Author: danhaywood <[email protected]> AuthorDate: Mon Feb 12 19:25:51 2024 +0000 CAUSEWAY-3676: simplifies constructor of GqlvAbstractCustom --- .../graphql/model/domain/GqlvAbstractCustom.java | 44 +++++++++++++++++++--- .../viewer/graphql/model/domain/GqlvAction.java | 2 +- .../graphql/model/domain/GqlvActionParam.java | 2 +- .../graphql/model/domain/GqlvActionParams.java | 2 +- .../graphql/model/domain/GqlvAssociation.java | 4 +- .../graphql/model/domain/GqlvCollection.java | 2 +- .../graphql/model/domain/GqlvDomainObject.java | 2 +- .../graphql/model/domain/GqlvDomainService.java | 2 +- .../viewer/graphql/model/domain/GqlvMember.java | 4 +- .../viewer/graphql/model/domain/GqlvMeta.java | 2 +- .../viewer/graphql/model/domain/GqlvProperty.java | 2 +- .../viewer/graphql/model/domain/GqlvScenario.java | 2 +- .../graphql/model/domain/GqlvScenarioGiven.java | 4 +- .../model/registry/GraphQLTypeRegistry.java | 11 ++++++ .../model/toplevel/GqlvTopLevelMutation.java | 2 +- .../graphql/model/toplevel/GqlvTopLevelQuery.java | 6 +-- 16 files changed, 66 insertions(+), 27 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 1c4fe194ca..4c275cb813 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 @@ -31,9 +31,12 @@ import lombok.AccessLevel; import lombok.Getter; import lombok.val; +import java.util.Optional; + public abstract class GqlvAbstractCustom extends GqlvAbstract implements GqlvHolder { protected final GraphQLObjectType.Builder gqlObjectTypeBuilder; + private final String typeName; @Getter(AccessLevel.PROTECTED) private GraphQLObjectType gqlObjectType; @@ -42,22 +45,45 @@ public abstract class GqlvAbstractCustom extends GqlvAbstract implements GqlvHol final GraphQLObjectType.Builder gqlObjectTypeBuilder, final Context context) { super(context); + typeName = null; // TODO - remove this constructor this.gqlObjectTypeBuilder = gqlObjectTypeBuilder; } - protected final GraphQLFieldDefinition addChildField(GraphQLFieldDefinition childField) { - if (this.gqlObjectType != null) { - throw new IllegalStateException("GqlObjectType has already been created"); + protected GqlvAbstractCustom( + final String typeName, + final Context context) { + super(context); + this.typeName = typeName; + Optional<GraphQLObjectType> typeIfAny = + context.graphQLTypeRegistry.lookup(typeName, GraphQLObjectType.class); + if(typeIfAny.isPresent()) { + this.gqlObjectType = typeIfAny.get(); + this.gqlObjectTypeBuilder = null; + } else { + this.gqlObjectTypeBuilder = newObject().name(typeName); + } + } + + public boolean isBuilt() { + return gqlObjectType != null; + } + + protected final void addChildField(GraphQLFieldDefinition childField) { + if (isBuilt()) { + return; } if (childField != null) { gqlObjectTypeBuilder.field(childField); } - return childField; } protected void buildObjectTypeAndField(String fieldName) { + if (isBuilt()) { + return; + } + val graphQLObjectType = buildObjectType(); setField(newFieldDefinition() @@ -67,12 +93,18 @@ public abstract class GqlvAbstractCustom extends GqlvAbstract implements GqlvHol } protected final GraphQLObjectType buildObjectType() { - this.gqlObjectType = gqlObjectTypeBuilder.build(); - context.graphQLTypeRegistry.addTypeIfNotAlreadyPresent(this.gqlObjectType); + if (!isBuilt()) { + this.gqlObjectType = gqlObjectTypeBuilder.build(); + context.graphQLTypeRegistry.addTypeIfNotAlreadyPresent(this.gqlObjectType); + } return this.gqlObjectType; } public final FieldCoordinates coordinatesFor(final GraphQLFieldDefinition field) { + if (gqlObjectType == null) { + throw new IllegalStateException( + String.format("GQL Object Type for '%s' not yet built", typeName)); + } return FieldCoordinates.coordinates(gqlObjectType, field); } diff --git a/viewers/graphql/model/src/main/java/org/apache/causeway/viewer/graphql/model/domain/GqlvAction.java b/viewers/graphql/model/src/main/java/org/apache/causeway/viewer/graphql/model/domain/GqlvAction.java index 1839ea2f5b..73f38544ea 100644 --- a/viewers/graphql/model/src/main/java/org/apache/causeway/viewer/graphql/model/domain/GqlvAction.java +++ b/viewers/graphql/model/src/main/java/org/apache/causeway/viewer/graphql/model/domain/GqlvAction.java @@ -73,7 +73,7 @@ public class GqlvAction final Holder holder, final ObjectAction objectAction, final Context context) { - super(holder, objectAction, newObject().name(TypeNames.actionTypeNameFor(holder.getObjectSpecification(), objectAction)), context); + super(holder, objectAction, TypeNames.actionTypeNameFor(holder.getObjectSpecification(), objectAction), context); this.hidden = new GqlvMemberHidden<>(this, context); addChildField(hidden.getField()); diff --git a/viewers/graphql/model/src/main/java/org/apache/causeway/viewer/graphql/model/domain/GqlvActionParam.java b/viewers/graphql/model/src/main/java/org/apache/causeway/viewer/graphql/model/domain/GqlvActionParam.java index 6a0cc01fc5..9b6ca82501 100644 --- a/viewers/graphql/model/src/main/java/org/apache/causeway/viewer/graphql/model/domain/GqlvActionParam.java +++ b/viewers/graphql/model/src/main/java/org/apache/causeway/viewer/graphql/model/domain/GqlvActionParam.java @@ -75,7 +75,7 @@ public class GqlvActionParam final ObjectActionParameter objectActionParameter, final Context context, final int paramNum) { - super(newObject().name(TypeNames.actionParamTypeNameFor(holder.getObjectSpecification(), objectActionParameter)), context); + super(TypeNames.actionParamTypeNameFor(holder.getObjectSpecification(), objectActionParameter), context); this.holder = holder; this.objectActionParameter = objectActionParameter; this.paramNum = paramNum; diff --git a/viewers/graphql/model/src/main/java/org/apache/causeway/viewer/graphql/model/domain/GqlvActionParams.java b/viewers/graphql/model/src/main/java/org/apache/causeway/viewer/graphql/model/domain/GqlvActionParams.java index 51cf89b4ab..03b95dbbef 100644 --- a/viewers/graphql/model/src/main/java/org/apache/causeway/viewer/graphql/model/domain/GqlvActionParams.java +++ b/viewers/graphql/model/src/main/java/org/apache/causeway/viewer/graphql/model/domain/GqlvActionParams.java @@ -55,7 +55,7 @@ public class GqlvActionParams public GqlvActionParams( final Holder holder, final Context context) { - super(newObject().name(TypeNames.actionParamsTypeNameFor(holder.getObjectSpecification(), holder.getObjectAction())), context); + super(TypeNames.actionParamsTypeNameFor(holder.getObjectSpecification(), holder.getObjectAction()), context); this.holder = holder; val idx = new AtomicInteger(0); diff --git a/viewers/graphql/model/src/main/java/org/apache/causeway/viewer/graphql/model/domain/GqlvAssociation.java b/viewers/graphql/model/src/main/java/org/apache/causeway/viewer/graphql/model/domain/GqlvAssociation.java index f867a633d7..c34c3e7edb 100644 --- a/viewers/graphql/model/src/main/java/org/apache/causeway/viewer/graphql/model/domain/GqlvAssociation.java +++ b/viewers/graphql/model/src/main/java/org/apache/causeway/viewer/graphql/model/domain/GqlvAssociation.java @@ -29,9 +29,9 @@ public abstract class GqlvAssociation<T extends ObjectAssociation, H extends Gql public GqlvAssociation( final H holder, final T objectAssociation, - final GraphQLObjectType.Builder gqlObjectTypeBuilder, + final String typeName, final Context context) { - super(holder, objectAssociation, gqlObjectTypeBuilder, context); + super(holder, objectAssociation, typeName, context); } diff --git a/viewers/graphql/model/src/main/java/org/apache/causeway/viewer/graphql/model/domain/GqlvCollection.java b/viewers/graphql/model/src/main/java/org/apache/causeway/viewer/graphql/model/domain/GqlvCollection.java index 174997254c..f5c0ef75b2 100644 --- a/viewers/graphql/model/src/main/java/org/apache/causeway/viewer/graphql/model/domain/GqlvCollection.java +++ b/viewers/graphql/model/src/main/java/org/apache/causeway/viewer/graphql/model/domain/GqlvCollection.java @@ -40,7 +40,7 @@ public class GqlvCollection final Holder holder, final OneToManyAssociation oneToManyAssociation, final Context context) { - super(holder, oneToManyAssociation, newObject().name(TypeNames.collectionTypeNameFor(holder.getObjectSpecification(), oneToManyAssociation)), context); + super(holder, oneToManyAssociation, TypeNames.collectionTypeNameFor(holder.getObjectSpecification(), oneToManyAssociation), context); this.hidden = new GqlvMemberHidden<>(this, context); addChildField(hidden.getField()); 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 51ad86ed82..cc21f2b654 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 @@ -71,7 +71,7 @@ public class GqlvDomainObject final GqlvDomainObject.Holder holder, final ObjectSpecification objectSpecification, final Context context) { - super(newObject().name(TypeNames.objectTypeNameFor(objectSpecification)), context); + super(TypeNames.objectTypeNameFor(objectSpecification), context); this.holder = holder; this.objectSpecification = objectSpecification; 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 b01a6d597d..64fb0d6aa3 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 @@ -59,7 +59,7 @@ public class GqlvDomainService final ObjectSpecification objectSpecification, final Object servicePojo, final Context context) { - super(newObject().name(TypeNames.objectTypeNameFor(objectSpecification)), context); + super(TypeNames.objectTypeNameFor(objectSpecification), context); this.holder = holder; this.objectSpecification = objectSpecification; this.servicePojo = servicePojo; diff --git a/viewers/graphql/model/src/main/java/org/apache/causeway/viewer/graphql/model/domain/GqlvMember.java b/viewers/graphql/model/src/main/java/org/apache/causeway/viewer/graphql/model/domain/GqlvMember.java index 94574512c2..8f903c39f9 100644 --- a/viewers/graphql/model/src/main/java/org/apache/causeway/viewer/graphql/model/domain/GqlvMember.java +++ b/viewers/graphql/model/src/main/java/org/apache/causeway/viewer/graphql/model/domain/GqlvMember.java @@ -37,10 +37,10 @@ public abstract class GqlvMember<T extends ObjectMember, H extends GqlvMember.Ho public GqlvMember( final H holder, final T objectMember, - final GraphQLObjectType.Builder gqlObjectTypeBuilder, + final String typeName, final Context context ) { - super(gqlObjectTypeBuilder, context); + super(typeName, context); this.holder = holder; this.objectMember = objectMember; } diff --git a/viewers/graphql/model/src/main/java/org/apache/causeway/viewer/graphql/model/domain/GqlvMeta.java b/viewers/graphql/model/src/main/java/org/apache/causeway/viewer/graphql/model/domain/GqlvMeta.java index 695a3d9c58..c9ded6804d 100644 --- a/viewers/graphql/model/src/main/java/org/apache/causeway/viewer/graphql/model/domain/GqlvMeta.java +++ b/viewers/graphql/model/src/main/java/org/apache/causeway/viewer/graphql/model/domain/GqlvMeta.java @@ -52,7 +52,7 @@ public class GqlvMeta extends GqlvAbstractCustom { final Holder holder, final Context context ) { - super(newObject().name(TypeNames.metaTypeNameFor(holder.getObjectSpecification())), context); + super(TypeNames.metaTypeNameFor(holder.getObjectSpecification()), context); this.holder = holder; diff --git a/viewers/graphql/model/src/main/java/org/apache/causeway/viewer/graphql/model/domain/GqlvProperty.java b/viewers/graphql/model/src/main/java/org/apache/causeway/viewer/graphql/model/domain/GqlvProperty.java index dec22de65d..5482c699ec 100644 --- a/viewers/graphql/model/src/main/java/org/apache/causeway/viewer/graphql/model/domain/GqlvProperty.java +++ b/viewers/graphql/model/src/main/java/org/apache/causeway/viewer/graphql/model/domain/GqlvProperty.java @@ -64,7 +64,7 @@ public class GqlvProperty final Holder holder, final OneToOneAssociation oneToOneAssociation, final Context context) { - super(holder, oneToOneAssociation, newObject().name(TypeNames.propertyTypeNameFor(holder.getObjectSpecification(), oneToOneAssociation)), context); + super(holder, oneToOneAssociation, TypeNames.propertyTypeNameFor(holder.getObjectSpecification(), oneToOneAssociation), context); this.hidden = new GqlvMemberHidden<>(this, context); addChildField(hidden.getField()); diff --git a/viewers/graphql/model/src/main/java/org/apache/causeway/viewer/graphql/model/domain/GqlvScenario.java b/viewers/graphql/model/src/main/java/org/apache/causeway/viewer/graphql/model/domain/GqlvScenario.java index a52d1400f3..1ede77d259 100644 --- a/viewers/graphql/model/src/main/java/org/apache/causeway/viewer/graphql/model/domain/GqlvScenario.java +++ b/viewers/graphql/model/src/main/java/org/apache/causeway/viewer/graphql/model/domain/GqlvScenario.java @@ -46,7 +46,7 @@ public class GqlvScenario public GqlvScenario( final GqlvScenario.Holder holder, final Context context) { - super(newObject().name("Scenario"), context); + super("Scenario", context); this.holder = holder; this.scenarioPojo = context.serviceRegistry.lookupService(Scenario.class).orElseThrow(); 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 f054030c97..34e8e33134 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 @@ -13,8 +13,6 @@ public class GqlvScenarioGiven extends GqlvAbstractCustom implements GqlvDomainService.Holder, GqlvDomainObject.Holder { - private static final String OBJECT_TYPE_NAME = "Given"; - private final Holder holder; private final List<GqlvDomainService> domainServices = new ArrayList<>(); @@ -23,7 +21,7 @@ public class GqlvScenarioGiven public GqlvScenarioGiven( final GqlvScenarioGiven.Holder holder, final Context context) { - super(newObject().name(OBJECT_TYPE_NAME), context); + super("Given", context); this.holder = holder; diff --git a/viewers/graphql/model/src/main/java/org/apache/causeway/viewer/graphql/model/registry/GraphQLTypeRegistry.java b/viewers/graphql/model/src/main/java/org/apache/causeway/viewer/graphql/model/registry/GraphQLTypeRegistry.java index bca95d0431..8fe4284670 100644 --- a/viewers/graphql/model/src/main/java/org/apache/causeway/viewer/graphql/model/registry/GraphQLTypeRegistry.java +++ b/viewers/graphql/model/src/main/java/org/apache/causeway/viewer/graphql/model/registry/GraphQLTypeRegistry.java @@ -20,6 +20,7 @@ package org.apache.causeway.viewer.graphql.model.registry; import java.util.Collections; import java.util.HashSet; +import java.util.Optional; import java.util.Set; import graphql.schema.GraphQLInputObjectType; @@ -110,6 +111,16 @@ public class GraphQLTypeRegistry { .anyMatch(ot -> ot.getName().equals(typeToAdd.getName())); } + public <T extends GraphQLNamedType> Optional<T> lookup( + final String typeName, + final Class<T> cls) { + return graphQLTypes.stream() + .filter(o -> o.getClass().isAssignableFrom(cls)) + .map(cls::cast) + .filter(ot -> ot.getName().equals(typeName)) + .findFirst(); + } + private void add(GraphQLType typeToAdd) { graphQLTypes.add(typeToAdd); 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 365f327096..6a8af3dce7 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 @@ -27,7 +27,7 @@ public class GqlvTopLevelMutation private final List<GqlvMutationForProperty> properties = new ArrayList<>(); public GqlvTopLevelMutation(final Context context) { - super(newObject().name("Mutation"), context); + super("Mutation", context); val objectSpecifications = context.objectSpecifications(); 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 237f5d399b..1f75f41fa2 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 @@ -17,15 +17,13 @@ public class GqlvTopLevelQuery extends GqlvAbstractCustom implements GqlvDomainService.Holder, GqlvDomainObject.Holder, GqlvScenario.Holder { - private static final String OBJECT_TYPE_NAME = "Query"; - private final List<GqlvDomainService> domainServices = new ArrayList<>(); private final List<GqlvDomainObject> domainObjects = new ArrayList<>(); // private final GqlvScenario scenario; public GqlvTopLevelQuery(final Context context) { - super(newObject().name(OBJECT_TYPE_NAME), context); + super("Query", context); context.objectSpecifications().forEach(objectSpec -> { switch (objectSpec.getBeanSort()) { @@ -60,7 +58,7 @@ public class GqlvTopLevelQuery } // scenario = new GqlvScenario(this, context); -// addField(scenario.getField()); +// addChildField(scenario.getField()); buildObjectType(); }
