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
The following commit(s) were added to refs/heads/CAUSEWAY-3676 by this push:
new 587ef59c3b CAUSEWAY-3676: wip on addActions, to move into
GqlvObjectSpec (5)
587ef59c3b is described below
commit 587ef59c3b9a1d606996a57b042813b321286871
Author: danhaywood <[email protected]>
AuthorDate: Fri Jan 19 13:21:51 2024 +0000
CAUSEWAY-3676: wip on addActions, to move into GqlvObjectSpec (5)
---
.../graphql/viewer/source/GqlvObjectSpec.java | 43 ++++++++++++++++++++--
.../graphql/viewer/source/ObjectTypeFactory.java | 17 ++++-----
2 files changed, 47 insertions(+), 13 deletions(-)
diff --git
a/incubator/viewers/graphql/viewer/src/main/java/org/apache/causeway/viewer/graphql/viewer/source/GqlvObjectSpec.java
b/incubator/viewers/graphql/viewer/src/main/java/org/apache/causeway/viewer/graphql/viewer/source/GqlvObjectSpec.java
index 94a8ffd11b..7c2be9939c 100644
---
a/incubator/viewers/graphql/viewer/src/main/java/org/apache/causeway/viewer/graphql/viewer/source/GqlvObjectSpec.java
+++
b/incubator/viewers/graphql/viewer/src/main/java/org/apache/causeway/viewer/graphql/viewer/source/GqlvObjectSpec.java
@@ -15,6 +15,7 @@ import lombok.val;
import java.util.ArrayList;
import java.util.List;
+import java.util.Optional;
import java.util.stream.Collectors;
import org.apache.causeway.applib.services.metamodel.BeanSort;
@@ -44,6 +45,10 @@ public class GqlvObjectSpec {
@Getter private final GraphQLObjectType.Builder gqlObjectTypeBuilder;
@Getter private final GraphQLInputObjectType gqlInputObjectType;
+ private String getLogicalTypeName() {
+ return objectSpec.getLogicalTypeName();
+ }
+
public String getLogicalTypeNameSanitized() {
val logicalTypeName = objectSpec.getLogicalTypeName();
return _LogicalTypeName.sanitized(logicalTypeName);
@@ -69,6 +74,11 @@ public class GqlvObjectSpec {
*/
private GraphQLObjectType gqlObjectType;
+ /**
+ * Built lazily using {@link #buildMutatorsTypeIfAny()}
+ */
+ private Optional<GraphQLObjectType> mutatorsTypeIfAny;
+
public GqlvObjectSpec(final ObjectSpecification objectSpec) {
this.objectSpec = objectSpec;
this.gqlObjectTypeBuilder =
newObject().name(getLogicalTypeNameSanitized());
@@ -221,6 +231,9 @@ public class GqlvObjectSpec {
mutatorsTypeFields.add(fieldDefinition);
}
+ boolean hasMutators() {
+ return !mutatorsTypeFields.isEmpty();
+ }
/**
@@ -230,10 +243,9 @@ public class GqlvObjectSpec {
*/
GraphQLObjectType buildGqlObjectType() {
if (gqlObjectType != null) {
- throw new IllegalArgumentException("GqlObjectType has already been
built");
+ throw new IllegalArgumentException(String.format("GqlObjectType
has already been built for %s", getLogicalTypeName()));
}
- gqlObjectType =
getGqlObjectTypeBuilder().name(getLogicalTypeNameSanitized()).build();
- return gqlObjectType;
+ return gqlObjectType =
getGqlObjectTypeBuilder().name(getLogicalTypeNameSanitized()).build();
}
/**
@@ -242,10 +254,33 @@ public class GqlvObjectSpec {
GraphQLObjectType getGqlObjectType() {
if (gqlObjectType == null) {
throw new IllegalStateException(String.format(
- "GraphQLObjectType has not yet been built for %s",
getLogicalTypeNameSanitized()));
+ "GraphQLObjectType has not yet been built for %s",
getLogicalTypeName()));
}
return gqlObjectType;
}
+ /**
+ * @see #buildMutatorsTypeIfAny()
+ */
+ public Optional<GraphQLObjectType> getMutatorsTypeIfAny() {
+ //noinspection OptionalAssignedToNull
+ if (mutatorsTypeIfAny == null) {
+ throw new IllegalArgumentException(String.format("Gql MutatorsType
has not yet been built for %s", getLogicalTypeName()));
+ }
+ return mutatorsTypeIfAny;
+ }
+
+ /**
+ * @see #getMutatorsTypeIfAny()
+ */
+ public Optional<GraphQLObjectType> buildMutatorsTypeIfAny() {
+ //noinspection OptionalAssignedToNull
+ if (mutatorsTypeIfAny != null) {
+ throw new IllegalArgumentException("MutatorsType has already been
built for " + getLogicalTypeName());
+ }
+ return mutatorsTypeIfAny = hasMutators()
+ ? Optional.of(mutatorsTypeBuilder.build())
+ : Optional.empty();
+ }
}
diff --git
a/incubator/viewers/graphql/viewer/src/main/java/org/apache/causeway/viewer/graphql/viewer/source/ObjectTypeFactory.java
b/incubator/viewers/graphql/viewer/src/main/java/org/apache/causeway/viewer/graphql/viewer/source/ObjectTypeFactory.java
index d6412edcca..c3a48442e7 100644
---
a/incubator/viewers/graphql/viewer/src/main/java/org/apache/causeway/viewer/graphql/viewer/source/ObjectTypeFactory.java
+++
b/incubator/viewers/graphql/viewer/src/main/java/org/apache/causeway/viewer/graphql/viewer/source/ObjectTypeFactory.java
@@ -24,6 +24,7 @@ import static graphql.schema.GraphQLNonNull.nonNull;
import static graphql.schema.GraphQLObjectType.newObject;
import java.util.List;
+import java.util.Optional;
import java.util.Set;
import javax.inject.Inject;
@@ -159,13 +160,12 @@ public class ObjectTypeFactory {
MutatorsDataForEntity addActions(final GqlvObjectSpec gqlvObjectSpec) {
gqlvObjectSpec.getObjectSpec().streamActions(ActionScope.PRODUCTION,
MixedIn.INCLUDED)
- .forEach(objectAction ->
- gqlvObjectSpec.addAction(objectAction)
- );
+ .forEach(gqlvObjectSpec::addAction);
- if (!gqlvObjectSpec.mutatorsTypeFields.isEmpty()){
- GraphQLObjectType mutatorsType =
gqlvObjectSpec.mutatorsTypeBuilder.build();
- graphQLTypeRegistry.addTypeIfNotAlreadyPresent(mutatorsType,
gqlvObjectSpec.mutatorsTypeName);
+ Optional<GraphQLObjectType> mutatorsTypeIfAny =
gqlvObjectSpec.buildMutatorsTypeIfAny();
+ return mutatorsTypeIfAny.map(mutatorsType -> {
+
+ graphQLTypeRegistry.addTypeIfNotAlreadyPresent(mutatorsType,
gqlvObjectSpec.getMutatorsTypeName());
GraphQLFieldDefinition gql_mutations = newFieldDefinition()
.name(GQL_MUTATIONS_FIELDNAME)
@@ -196,9 +196,8 @@ public class ObjectTypeFactory {
// return gqlMeta.id();
// }
// });
- }
-
- return null;
+ })
+ .orElse(null);
}