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 5fba37e2890f42542936071bb8d76fa3a4219107 Author: danhaywood <[email protected]> AuthorDate: Fri Jan 19 10:26:47 2024 +0000 CAUSEWAY-3676: refactorings --- ...ayViewerGraphqlTestModuleIntegTestAbstract.java | 8 +- .../test/schema/GqlSchema_print_IntegTest.java | 5 +- .../graphql/viewer/src/main/java/module-info.java | 1 + ...ecutionStrategyResolvingWithinInteraction.java} | 4 +- .../ExecutionGraphQlServiceForCauseway.java} | 4 +- .../GraphQlSourceForCauseway.java | 15 ++- .../viewer/source/{GqlMeta.java => GqlvMeta.java} | 5 +- .../{GqlMutations.java => GqlvMutations.java} | 2 +- .../graphql/viewer/source/GqlvObjectSpec.java | 7 +- .../graphql/viewer/source/ObjectTypeFactory.java | 103 ++++++++++++++------- 10 files changed, 96 insertions(+), 58 deletions(-) diff --git a/incubator/viewers/graphql/test/src/test/java/org/apache/causeway/viewer/graphql/viewer/test/CausewayViewerGraphqlTestModuleIntegTestAbstract.java b/incubator/viewers/graphql/test/src/test/java/org/apache/causeway/viewer/graphql/viewer/test/CausewayViewerGraphqlTestModuleIntegTestAbstract.java index 6f88728dbf..c5c9437e74 100644 --- a/incubator/viewers/graphql/test/src/test/java/org/apache/causeway/viewer/graphql/viewer/test/CausewayViewerGraphqlTestModuleIntegTestAbstract.java +++ b/incubator/viewers/graphql/test/src/test/java/org/apache/causeway/viewer/graphql/viewer/test/CausewayViewerGraphqlTestModuleIntegTestAbstract.java @@ -36,8 +36,8 @@ import org.apache.causeway.commons.internal.resources._Resources; import org.apache.causeway.core.config.environment.CausewaySystemEnvironment; import org.apache.causeway.core.metamodel.specloader.SpecificationLoader; -import org.apache.causeway.viewer.graphql.viewer.source.GraphQlServiceForCauseway; -import org.apache.causeway.viewer.graphql.viewer.source.GraphQlSourceForCauseway; +import org.apache.causeway.viewer.graphql.viewer.integration.ExecutionGraphQlServiceForCauseway; +import org.apache.causeway.viewer.graphql.viewer.integration.GraphQlSourceForCauseway; import org.approvaltests.core.Options; import org.junit.jupiter.api.BeforeEach; @@ -115,7 +115,7 @@ public abstract class CausewayViewerGraphqlTestModuleIntegTestAbstract { @Inject protected SpecificationLoader specificationLoader; @Inject protected TransactionService transactionService; @Inject protected GraphQlSourceForCauseway graphQlSourceForCauseway; - @Inject protected GraphQlServiceForCauseway graphQlServiceForCauseway; + @Inject protected ExecutionGraphQlServiceForCauseway executionGraphQlServiceForCauseway; @LocalServerPort protected int port; @@ -127,7 +127,7 @@ public abstract class CausewayViewerGraphqlTestModuleIntegTestAbstract { assertNotNull(specificationLoader); assertNotNull(transactionService); assertNotNull(graphQlSourceForCauseway); - assertNotNull(graphQlServiceForCauseway); + assertNotNull(executionGraphQlServiceForCauseway); } diff --git a/incubator/viewers/graphql/test/src/test/java/org/apache/causeway/viewer/graphql/viewer/test/schema/GqlSchema_print_IntegTest.java b/incubator/viewers/graphql/test/src/test/java/org/apache/causeway/viewer/graphql/viewer/test/schema/GqlSchema_print_IntegTest.java index 3ceac932e9..561a7b6900 100644 --- a/incubator/viewers/graphql/test/src/test/java/org/apache/causeway/viewer/graphql/viewer/test/schema/GqlSchema_print_IntegTest.java +++ b/incubator/viewers/graphql/test/src/test/java/org/apache/causeway/viewer/graphql/viewer/test/schema/GqlSchema_print_IntegTest.java @@ -29,17 +29,14 @@ import org.apache.causeway.commons.io.TextUtils; import org.apache.causeway.viewer.graphql.viewer.test.CausewayViewerGraphqlTestModuleIntegTestAbstract; -import org.approvaltests.Approvals; import org.approvaltests.core.Options; -import org.approvaltests.reporters.DiffReporter; -import org.approvaltests.reporters.UseReporter; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.springframework.transaction.annotation.Transactional; import org.apache.causeway.core.config.environment.CausewaySystemEnvironment; import org.apache.causeway.core.metamodel.specloader.SpecificationLoader; -import org.apache.causeway.viewer.graphql.viewer.source.GraphQlSourceForCauseway; +import org.apache.causeway.viewer.graphql.viewer.integration.GraphQlSourceForCauseway; import static org.apache.causeway.commons.internal.assertions._Assert.assertEquals; import static org.apache.causeway.commons.internal.assertions._Assert.assertNotNull; diff --git a/incubator/viewers/graphql/viewer/src/main/java/module-info.java b/incubator/viewers/graphql/viewer/src/main/java/module-info.java index a6d381a48e..a1bf0e3120 100644 --- a/incubator/viewers/graphql/viewer/src/main/java/module-info.java +++ b/incubator/viewers/graphql/viewer/src/main/java/module-info.java @@ -1,6 +1,7 @@ module org.apache.causeway.incubator.viewer.graphql.viewer { exports org.apache.causeway.viewer.graphql.viewer; exports org.apache.causeway.viewer.graphql.viewer.source; + exports org.apache.causeway.viewer.graphql.viewer.integration; requires com.fasterxml.jackson.core; requires com.fasterxml.jackson.databind; diff --git a/incubator/viewers/graphql/viewer/src/main/java/org/apache/causeway/viewer/graphql/viewer/source/ExecutionStrategyResolvingWithinInteraction.java b/incubator/viewers/graphql/viewer/src/main/java/org/apache/causeway/viewer/graphql/viewer/integration/AsyncExecutionStrategyResolvingWithinInteraction.java similarity index 92% rename from incubator/viewers/graphql/viewer/src/main/java/org/apache/causeway/viewer/graphql/viewer/source/ExecutionStrategyResolvingWithinInteraction.java rename to incubator/viewers/graphql/viewer/src/main/java/org/apache/causeway/viewer/graphql/viewer/integration/AsyncExecutionStrategyResolvingWithinInteraction.java index d88f289bb3..a94655eb96 100644 --- a/incubator/viewers/graphql/viewer/src/main/java/org/apache/causeway/viewer/graphql/viewer/source/ExecutionStrategyResolvingWithinInteraction.java +++ b/incubator/viewers/graphql/viewer/src/main/java/org/apache/causeway/viewer/graphql/viewer/integration/AsyncExecutionStrategyResolvingWithinInteraction.java @@ -16,7 +16,7 @@ * specific language governing permissions and limitations * under the License. */ -package org.apache.causeway.viewer.graphql.viewer.source; +package org.apache.causeway.viewer.graphql.viewer.integration; import java.util.concurrent.CompletableFuture; @@ -34,7 +34,7 @@ import lombok.RequiredArgsConstructor; @Service @RequiredArgsConstructor(onConstructor_ = {@Inject}) -public class ExecutionStrategyResolvingWithinInteraction extends AsyncExecutionStrategy { +public class AsyncExecutionStrategyResolvingWithinInteraction extends AsyncExecutionStrategy { private final InteractionService interactionService; diff --git a/incubator/viewers/graphql/viewer/src/main/java/org/apache/causeway/viewer/graphql/viewer/source/GraphQlServiceForCauseway.java b/incubator/viewers/graphql/viewer/src/main/java/org/apache/causeway/viewer/graphql/viewer/integration/ExecutionGraphQlServiceForCauseway.java similarity index 93% rename from incubator/viewers/graphql/viewer/src/main/java/org/apache/causeway/viewer/graphql/viewer/source/GraphQlServiceForCauseway.java rename to incubator/viewers/graphql/viewer/src/main/java/org/apache/causeway/viewer/graphql/viewer/integration/ExecutionGraphQlServiceForCauseway.java index 281d2f709f..85e06ba3ee 100644 --- a/incubator/viewers/graphql/viewer/src/main/java/org/apache/causeway/viewer/graphql/viewer/source/GraphQlServiceForCauseway.java +++ b/incubator/viewers/graphql/viewer/src/main/java/org/apache/causeway/viewer/graphql/viewer/integration/ExecutionGraphQlServiceForCauseway.java @@ -16,7 +16,7 @@ * specific language governing permissions and limitations * under the License. */ -package org.apache.causeway.viewer.graphql.viewer.source; +package org.apache.causeway.viewer.graphql.viewer.integration; import javax.inject.Inject; @@ -36,7 +36,7 @@ import reactor.core.publisher.Mono; */ @Service() @RequiredArgsConstructor(onConstructor_ = {@Inject}) -public class GraphQlServiceForCauseway implements ExecutionGraphQlService { +public class ExecutionGraphQlServiceForCauseway implements ExecutionGraphQlService { private final BatchLoaderRegistry batchLoaderRegistry; private final GraphQlSource graphQlSource; diff --git a/incubator/viewers/graphql/viewer/src/main/java/org/apache/causeway/viewer/graphql/viewer/source/GraphQlSourceForCauseway.java b/incubator/viewers/graphql/viewer/src/main/java/org/apache/causeway/viewer/graphql/viewer/integration/GraphQlSourceForCauseway.java similarity index 91% rename from incubator/viewers/graphql/viewer/src/main/java/org/apache/causeway/viewer/graphql/viewer/source/GraphQlSourceForCauseway.java rename to incubator/viewers/graphql/viewer/src/main/java/org/apache/causeway/viewer/graphql/viewer/integration/GraphQlSourceForCauseway.java index 3dc550fa9c..2e697b6a5b 100644 --- a/incubator/viewers/graphql/viewer/src/main/java/org/apache/causeway/viewer/graphql/viewer/source/GraphQlSourceForCauseway.java +++ b/incubator/viewers/graphql/viewer/src/main/java/org/apache/causeway/viewer/graphql/viewer/integration/GraphQlSourceForCauseway.java @@ -16,7 +16,7 @@ * specific language governing permissions and limitations * under the License. */ -package org.apache.causeway.viewer.graphql.viewer.source; +package org.apache.causeway.viewer.graphql.viewer.integration; import static graphql.schema.FieldCoordinates.coordinates; import static graphql.schema.GraphQLFieldDefinition.newFieldDefinition; @@ -33,6 +33,11 @@ import org.apache.causeway.applib.id.HasLogicalType; import org.apache.causeway.core.metamodel.spec.ObjectSpecification; +import org.apache.causeway.viewer.graphql.viewer.integration.AsyncExecutionStrategyResolvingWithinInteraction; + +import org.apache.causeway.viewer.graphql.viewer.source.ObjectTypeFactory; +import org.apache.causeway.viewer.graphql.viewer.source.QueryFieldFactory; + import org.springframework.graphql.execution.GraphQlSource; import org.springframework.stereotype.Service; @@ -44,7 +49,6 @@ import org.apache.causeway.core.metamodel.specloader.SpecificationLoader; import graphql.GraphQL; import graphql.Scalars; -import graphql.execution.instrumentation.tracing.TracingInstrumentation; import graphql.schema.DataFetcher; import graphql.schema.GraphQLCodeRegistry; import graphql.schema.GraphQLObjectType; @@ -61,7 +65,7 @@ public class GraphQlSourceForCauseway implements GraphQlSource { private final SpecificationLoader specificationLoader; private final CausewayConfiguration causewayConfiguration; private final CausewaySystemEnvironment causewaySystemEnvironment; - private final ExecutionStrategyResolvingWithinInteraction executionStrategy; + private final AsyncExecutionStrategyResolvingWithinInteraction executionStrategy; private final ObjectTypeFactory objectTypeFactory; private final QueryFieldFactory queryFieldFactory; @@ -124,7 +128,10 @@ public class GraphQlSourceForCauseway implements GraphQlSource { private void handleObjectSpec( final ObjectSpecification objectSpec, final Set<GraphQLType> graphQLObjectTypes, - final GraphQLObjectType.Builder queryBuilder, final GraphQLCodeRegistry.Builder codeRegistryBuilder) { + final GraphQLObjectType.Builder queryBuilder, + final GraphQLCodeRegistry.Builder codeRegistryBuilder + ) { + switch (objectSpec.getBeanSort()) { case ABSTRACT: diff --git a/incubator/viewers/graphql/viewer/src/main/java/org/apache/causeway/viewer/graphql/viewer/source/GqlMeta.java b/incubator/viewers/graphql/viewer/src/main/java/org/apache/causeway/viewer/graphql/viewer/source/GqlvMeta.java similarity index 96% rename from incubator/viewers/graphql/viewer/src/main/java/org/apache/causeway/viewer/graphql/viewer/source/GqlMeta.java rename to incubator/viewers/graphql/viewer/src/main/java/org/apache/causeway/viewer/graphql/viewer/source/GqlvMeta.java index 4a464fb620..c51c9df2dd 100644 --- a/incubator/viewers/graphql/viewer/src/main/java/org/apache/causeway/viewer/graphql/viewer/source/GqlMeta.java +++ b/incubator/viewers/graphql/viewer/src/main/java/org/apache/causeway/viewer/graphql/viewer/source/GqlvMeta.java @@ -28,8 +28,11 @@ import lombok.Data; import java.util.Objects; import java.util.Optional; +/** + * Metadata for every domain object. + */ @Data -public class GqlMeta { +public class GqlvMeta { private final Bookmark bookmark; private final BookmarkService bookmarkService; diff --git a/incubator/viewers/graphql/viewer/src/main/java/org/apache/causeway/viewer/graphql/viewer/source/GqlMutations.java b/incubator/viewers/graphql/viewer/src/main/java/org/apache/causeway/viewer/graphql/viewer/source/GqlvMutations.java similarity index 97% rename from incubator/viewers/graphql/viewer/src/main/java/org/apache/causeway/viewer/graphql/viewer/source/GqlMutations.java rename to incubator/viewers/graphql/viewer/src/main/java/org/apache/causeway/viewer/graphql/viewer/source/GqlvMutations.java index 27d1cb4e27..49303b7852 100644 --- a/incubator/viewers/graphql/viewer/src/main/java/org/apache/causeway/viewer/graphql/viewer/source/GqlMutations.java +++ b/incubator/viewers/graphql/viewer/src/main/java/org/apache/causeway/viewer/graphql/viewer/source/GqlvMutations.java @@ -26,7 +26,7 @@ import org.apache.causeway.applib.services.bookmark.BookmarkService; import lombok.Data; @Data -public class GqlMutations { +public class GqlvMutations { private final Bookmark bookmark; private final BookmarkService bookmarkService; 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 1f77da06ec..adec308cd5 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 @@ -37,14 +37,9 @@ public class GqlvObjectSpec { } @Getter private final GraphQLObjectType.Builder objectTypeBuilder; - @Getter private final GraphQLObjectType metaType; - static GqlvObjectSpec gqlv(final ObjectSpecification objectSpec) { - return new GqlvObjectSpec(objectSpec); - } - - GqlvObjectSpec(final ObjectSpecification objectSpec) { + public GqlvObjectSpec(final ObjectSpecification objectSpec) { this.objectSpec = objectSpec; this.objectTypeBuilder = newObject().name(getLogicalTypeNameSanitized()); this.metaType = _GraphQLObjectType.create(getLogicalTypeNameSanitized(), getBeanSort()); 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 919fc90df4..4490daf6c9 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 @@ -54,6 +54,7 @@ import graphql.schema.GraphQLFieldDefinition; import graphql.schema.GraphQLInputObjectField; import graphql.schema.GraphQLInputObjectType; import graphql.schema.GraphQLInputType; +import graphql.schema.GraphQLNamedType; import graphql.schema.GraphQLObjectType; import graphql.schema.GraphQLOutputType; import graphql.schema.GraphQLType; @@ -62,10 +63,13 @@ import lombok.AllArgsConstructor; import lombok.Data; import lombok.RequiredArgsConstructor; import lombok.experimental.UtilityClass; +import lombok.extern.java.Log; +import lombok.extern.log4j.Log4j2; import lombok.val; @Component @RequiredArgsConstructor(onConstructor_ = {@Inject}) +@Log4j2 public class ObjectTypeFactory { final static String GQL_INPUTTYPE_PREFIX = "_gql_input__"; @@ -105,13 +109,13 @@ public class ObjectTypeFactory { final Set<GraphQLType> graphQLObjectTypes, final GraphQLCodeRegistry.Builder codeRegistryBuilder) { - val gqlvObjectSpec = GqlvObjectSpec.gqlv(objectSpec); + val gqlvObjectSpec = new GqlvObjectSpec(objectSpec); // create meta field type - BeanSort objectSpecificationBeanSort = objectSpec.getBeanSort(); + BeanSort objectSpecificationBeanSort = gqlvObjectSpec.getBeanSort(); final String logicalTypeNameSanitized = gqlvObjectSpec.getLogicalTypeNameSanitized(); - GraphQLObjectType metaType = _GraphQLObjectType.create(logicalTypeNameSanitized, objectSpecificationBeanSort); + GraphQLObjectType metaType = gqlvObjectSpec.getMetaType(); addTypeIfNotAlreadyPresent(graphQLObjectTypes, metaType, logicalTypeNameSanitized); @@ -128,6 +132,7 @@ public class ObjectTypeFactory { .type(nonNull(Scalars.GraphQLID)) .build()); GraphQLInputType inputType = inputTypeBuilder.build(); + addTypeIfNotAlreadyPresent(graphQLObjectTypes, inputType, inputTypeName); // add fields @@ -166,33 +171,63 @@ public class ObjectTypeFactory { void addTypeIfNotAlreadyPresent( final Set<GraphQLType> graphQLObjectTypes, final GraphQLType typeToAdd, - final String logicalTypeName){ + final String logicalTypeName) { - boolean present; - if (typeToAdd.getClass().isAssignableFrom(GraphQLObjectType.class)){ - GraphQLObjectType typeToAdd1 = (GraphQLObjectType) typeToAdd; - present = graphQLObjectTypes.stream() - .filter(o -> o.getClass().isAssignableFrom(GraphQLObjectType.class)) - .map(GraphQLObjectType.class::cast) - .filter(ot -> ot.getName().equals(typeToAdd1.getName())) - .findFirst().isPresent(); - } else { - // must be input type - GraphQLInputObjectType typeToAdd1 = (GraphQLInputObjectType) typeToAdd; - present = graphQLObjectTypes.stream() - .filter(o -> o.getClass().isAssignableFrom(GraphQLInputObjectType.class)) - .map(GraphQLInputObjectType.class::cast) - .filter(ot -> ot.getName().equals(typeToAdd1.getName())) - .findFirst().isPresent(); + if (typeToAdd instanceof GraphQLObjectType) { + addTypeIfNotAlreadyPresent(graphQLObjectTypes, (GraphQLObjectType) typeToAdd, logicalTypeName); + return; + } + + if (typeToAdd instanceof GraphQLInputObjectType) { + addTypeIfNotAlreadyPresent(graphQLObjectTypes, (GraphQLInputObjectType) typeToAdd, logicalTypeName); + return; } - if (present){ + + // TODO: none of these types yet handled + // GraphQLTypeReference + // GraphQLScalarType + // GraphQLCompositeType + // GraphQLUnionType + // GraphQLEnumType + // GraphQLInterfaceType + // GraphQLList + // GraphQLNonNull + log.warn("GraphQLType {} not yet implemented", typeToAdd.getClass().getName()); + } + + private static void addTypeIfNotAlreadyPresent( + final Set<GraphQLType> graphQLObjectTypes, + final GraphQLInputObjectType typeToAdd, + final String logicalTypeName) { + if (isPresent(graphQLObjectTypes, typeToAdd, GraphQLInputObjectType.class)){ // For now we just log and skip - System.out.println("==== DOUBLE ===="); - System.out.println(logicalTypeName); + log.info("GraphQLInputObjectType for {} already present", logicalTypeName); + return; + } + graphQLObjectTypes.add(typeToAdd); + } - } else { - graphQLObjectTypes.add(typeToAdd); + void addTypeIfNotAlreadyPresent( + final Set<GraphQLType> graphQLObjectTypes, + final GraphQLObjectType typeToAdd, + final String logicalTypeName){ + + if (isPresent(graphQLObjectTypes, typeToAdd, GraphQLObjectType.class)){ + // For now we just log and skip + log.info("GraphQLObjectType for {} already present", logicalTypeName); + return; } + graphQLObjectTypes.add(typeToAdd); + } + + private static boolean isPresent( + final Set<GraphQLType> graphQLObjectTypes, + final GraphQLNamedType typeToAdd, + final Class<? extends GraphQLNamedType> cls) { + return graphQLObjectTypes.stream() + .filter(o -> o.getClass().isAssignableFrom(cls)) + .map(cls::cast) + .anyMatch(ot -> ot.getName().equals(typeToAdd.getName())); } void createAndRegisterDataFetchersForField( @@ -249,7 +284,7 @@ public class ObjectTypeFactory { // // Bookmark bookmark = bookmarkService.bookmarkFor(environment.getSource()).orElse(null); // if (bookmark == null) return null; //TODO: is this correct ? -// return new GqlMutations(bookmark, bookmarkService, mutatorsTypeFields); +// return new GqlvMutations(bookmark, bookmarkService, mutatorsTypeFields); // } // }); // @@ -258,7 +293,7 @@ public class ObjectTypeFactory { // @Override // public Object get(DataFetchingEnvironment environment) throws Exception { // -// GqlMeta gqlMeta = environment.getSource(); +// GqlvMeta gqlMeta = environment.getSource(); // // return gqlMeta.id(); // } @@ -404,24 +439,24 @@ public class ObjectTypeFactory { codeRegistryBuilder.dataFetcher(FieldCoordinates.coordinates(graphQLObjectType, gql_meta), (DataFetcher<Object>) environment -> { return bookmarkService.bookmarkFor(environment.getSource()) - .map(bookmark -> new GqlMeta(bookmark, bookmarkService, objectManager)) + .map(bookmark -> new GqlvMeta(bookmark, bookmarkService, objectManager)) .orElse(null); //TODO: is this correct ? }); codeRegistryBuilder.dataFetcher(FieldCoordinates.coordinates(metaType, Fields.id), (DataFetcher<Object>) environment -> { - GqlMeta gqlMeta = environment.getSource(); - return gqlMeta.id(); + GqlvMeta gqlvMeta = environment.getSource(); + return gqlvMeta.id(); }); codeRegistryBuilder.dataFetcher(FieldCoordinates.coordinates(metaType, Fields.logicalTypeName), (DataFetcher<Object>) environment -> { - GqlMeta gqlMeta = environment.getSource(); - return gqlMeta.logicalTypeName(); + GqlvMeta gqlvMeta = environment.getSource(); + return gqlvMeta.logicalTypeName(); }); if (objectSpecificationBeanSort == BeanSort.ENTITY) { codeRegistryBuilder.dataFetcher(FieldCoordinates.coordinates(metaType, Fields.version), (DataFetcher<Object>) environment -> { - GqlMeta gqlMeta = environment.getSource(); - return gqlMeta.version(); + GqlvMeta gqlvMeta = environment.getSource(); + return gqlvMeta.version(); }); }
