From 7b33ab727db071fbc968248ebe04fedcede7089c Mon Sep 17 00:00:00 2001 From: Igor Dianov Date: Tue, 28 May 2024 14:24:47 -0700 Subject: [PATCH 1/2] Add aggregate query schema field and type descriptions --- .../schema/impl/GraphQLJpaSchemaBuilder.java | 29 +++++++++++++++---- .../GraphQLJpaQueryAggregateTests.java | 2 +- .../jpa/query/example/Application.java | 2 +- 3 files changed, 26 insertions(+), 7 deletions(-) diff --git a/schema/src/main/java/com/introproventures/graphql/jpa/query/schema/impl/GraphQLJpaSchemaBuilder.java b/schema/src/main/java/com/introproventures/graphql/jpa/query/schema/impl/GraphQLJpaSchemaBuilder.java index b058dcee..45de4c63 100644 --- a/schema/src/main/java/com/introproventures/graphql/jpa/query/schema/impl/GraphQLJpaSchemaBuilder.java +++ b/schema/src/main/java/com/introproventures/graphql/jpa/query/schema/impl/GraphQLJpaSchemaBuilder.java @@ -421,7 +421,7 @@ private GraphQLFieldDefinition getAggregateFieldDefinition(EntityType entityT final var selectTypeName = resolveSelectTypeName(entityType); final var aggregateObjectTypeName = selectTypeName.concat("Aggregate"); - var aggregateObjectType = newObject().name(aggregateObjectTypeName); + var aggregateObjectType = newObject().name(aggregateObjectTypeName).description("%s entity aggregate object type".formatted(selectTypeName)); DataFetcher aggregateDataFetcher = environment -> { Map source = environment.getSource(); @@ -431,6 +431,7 @@ private GraphQLFieldDefinition getAggregateFieldDefinition(EntityType entityT var countFieldDefinition = newFieldDefinition() .name("count") + .description("Count the number of records in the database for the %s aggregate".formatted(selectTypeName)) .dataFetcher(aggregateDataFetcher) .type(GraphQLInt); @@ -440,7 +441,9 @@ private GraphQLFieldDefinition getAggregateFieldDefinition(EntityType entityT .filter(it -> EntityIntrospector.introspect(entityType).isNotIgnored(it.getName())) .filter(Attribute::isAssociation) .map(Attribute::getName) - .map(name -> newEnumValueDefinition().name(name).build()) + .map(name -> newEnumValueDefinition().name(name) + .description("%s entity associated %s child entity".formatted(selectTypeName, name)) + .build()) .toList(); var fieldsEnumValueDefinitions = entityType @@ -449,16 +452,20 @@ private GraphQLFieldDefinition getAggregateFieldDefinition(EntityType entityT .filter(it -> EntityIntrospector.introspect(entityType).isNotIgnored(it.getName())) .filter(it -> isBasic(it) || isEmbeddable(it)) .map(Attribute::getName) - .map(name -> newEnumValueDefinition().name(name).build()) + .map(name -> newEnumValueDefinition().name(name) + .description("%s entity %s attribute".formatted(selectTypeName, name)) + .build()) .toList(); if (entityType.getAttributes().stream().anyMatch(Attribute::isAssociation)) { countFieldDefinition.argument( newArgument() .name("of") + .description("Count the number of associated records in the database for the %s aggregate".formatted(selectTypeName)) .type( newEnum() .name(aggregateObjectTypeName.concat("CountOfAssociationsEnum")) + .description("%s entity associated entity name values".formatted(selectTypeName)) .values(associationEnumValueDefinitions) .build() ) @@ -467,21 +474,26 @@ private GraphQLFieldDefinition getAggregateFieldDefinition(EntityType entityT var groupFieldDefinition = newFieldDefinition() .name("group") + .description("Group by %s entity query field aggregated by multiple fields".formatted(selectTypeName)) .dataFetcher(aggregateDataFetcher) .type( new GraphQLList( newObject() .name(aggregateObjectTypeName.concat("GroupBy")) + .description("%s entity group by object type".formatted(selectTypeName)) .field( newFieldDefinition() .name("by") + .description("Group by %s field container used to query aggregate data by one or more fields".formatted(selectTypeName)) .dataFetcher(aggregateDataFetcher) .argument( newArgument() .name("field") + .description("Group by field argument used to specify %s entity field name".formatted(selectTypeName)) .type( newEnum() .name(aggregateObjectTypeName.concat("GroupByFieldsEnum")) + .description("%s entity field name values".formatted(selectTypeName)) .values(fieldsEnumValueDefinitions) .build() ) @@ -515,6 +527,7 @@ private GraphQLFieldDefinition getAggregateFieldDefinition(EntityType entityT aggregateObjectType.field( newFieldDefinition() .name(association.getName()) + .description("Aggregate %s query field definition for the associated %s entity".formatted(selectTypeName, association.getName())) .dataFetcher(aggregateDataFetcher) .type( new GraphQLList( @@ -524,13 +537,16 @@ private GraphQLFieldDefinition getAggregateFieldDefinition(EntityType entityT .concat(capitalize(association.getName())) .concat("GroupByNestedAssociation") ) + .description("Aggregate %s query object type for the associated %s entity".formatted(selectTypeName, association.getName())) .field( newFieldDefinition() .name("by") .dataFetcher(aggregateDataFetcher) + .description("Group by %s attribute field used to query aggregate data by one or more fields".formatted(association.getName())) .argument( newArgument() .name("field") + .description("Group by field argument used to specify associated %s entity field name".formatted(association.getName())) .type( newEnum() .name( @@ -544,7 +560,9 @@ private GraphQLFieldDefinition getAggregateFieldDefinition(EntityType entityT ) .type(JavaScalars.GraphQLObjectScalar) ) - .field(newFieldDefinition().name("count").type(GraphQLInt)) + .field(newFieldDefinition().name("count") + .description("Count the number of records in the database for the %s associated nested aggregate".formatted(association.getName())) + .type(GraphQLInt)) .build() ) ) @@ -554,7 +572,8 @@ private GraphQLFieldDefinition getAggregateFieldDefinition(EntityType entityT aggregateObjectType.field(countFieldDefinition).field(groupFieldDefinition); - var aggregateFieldDefinition = newFieldDefinition().name("aggregate").type(aggregateObjectType); + var aggregateFieldDefinition = newFieldDefinition().name("aggregate") + .description("Aggregate data query field for %s entity".formatted(selectTypeName)).type(aggregateObjectType); return aggregateFieldDefinition.build(); } diff --git a/schema/src/test/java/com/introproventures/graphql/jpa/query/converter/GraphQLJpaQueryAggregateTests.java b/schema/src/test/java/com/introproventures/graphql/jpa/query/converter/GraphQLJpaQueryAggregateTests.java index 8684f534..1d268185 100644 --- a/schema/src/test/java/com/introproventures/graphql/jpa/query/converter/GraphQLJpaQueryAggregateTests.java +++ b/schema/src/test/java/com/introproventures/graphql/jpa/query/converter/GraphQLJpaQueryAggregateTests.java @@ -80,7 +80,7 @@ public Object serialize(final Object input) { .filter(VariableValue.class::isInstance) .map(VariableValue.class::cast) .map(it -> Optional.ofNullable(it.getValue()).orElse(Optional.empty())) - .orElse(input); + .orElseGet(() -> super.serialize(input)); } } ) diff --git a/tests/gatling/src/main/java/com/introproventures/graphql/jpa/query/example/Application.java b/tests/gatling/src/main/java/com/introproventures/graphql/jpa/query/example/Application.java index ba69d37d..03ef5ca9 100644 --- a/tests/gatling/src/main/java/com/introproventures/graphql/jpa/query/example/Application.java +++ b/tests/gatling/src/main/java/com/introproventures/graphql/jpa/query/example/Application.java @@ -64,7 +64,7 @@ public Object serialize(final Object input) { .filter(VariableValue.class::isInstance) .map(VariableValue.class::cast) .map(it -> Optional.ofNullable(it.getValue()).orElse(Optional.empty())) - .orElse(input); + .orElseGet(() -> super.serialize(input)); } } ) From b6e3e0bf024f77be535a093753bb625de0a187b3 Mon Sep 17 00:00:00 2001 From: Igor Dianov Date: Tue, 28 May 2024 14:26:19 -0700 Subject: [PATCH 2/2] Apply Prettier Java formatting --- .../schema/impl/GraphQLJpaSchemaBuilder.java | 85 ++++++++++++++----- 1 file changed, 66 insertions(+), 19 deletions(-) diff --git a/schema/src/main/java/com/introproventures/graphql/jpa/query/schema/impl/GraphQLJpaSchemaBuilder.java b/schema/src/main/java/com/introproventures/graphql/jpa/query/schema/impl/GraphQLJpaSchemaBuilder.java index 45de4c63..dc97c673 100644 --- a/schema/src/main/java/com/introproventures/graphql/jpa/query/schema/impl/GraphQLJpaSchemaBuilder.java +++ b/schema/src/main/java/com/introproventures/graphql/jpa/query/schema/impl/GraphQLJpaSchemaBuilder.java @@ -421,7 +421,9 @@ private GraphQLFieldDefinition getAggregateFieldDefinition(EntityType entityT final var selectTypeName = resolveSelectTypeName(entityType); final var aggregateObjectTypeName = selectTypeName.concat("Aggregate"); - var aggregateObjectType = newObject().name(aggregateObjectTypeName).description("%s entity aggregate object type".formatted(selectTypeName)); + var aggregateObjectType = newObject() + .name(aggregateObjectTypeName) + .description("%s entity aggregate object type".formatted(selectTypeName)); DataFetcher aggregateDataFetcher = environment -> { Map source = environment.getSource(); @@ -441,9 +443,12 @@ private GraphQLFieldDefinition getAggregateFieldDefinition(EntityType entityT .filter(it -> EntityIntrospector.introspect(entityType).isNotIgnored(it.getName())) .filter(Attribute::isAssociation) .map(Attribute::getName) - .map(name -> newEnumValueDefinition().name(name) - .description("%s entity associated %s child entity".formatted(selectTypeName, name)) - .build()) + .map(name -> + newEnumValueDefinition() + .name(name) + .description("%s entity associated %s child entity".formatted(selectTypeName, name)) + .build() + ) .toList(); var fieldsEnumValueDefinitions = entityType @@ -452,16 +457,23 @@ private GraphQLFieldDefinition getAggregateFieldDefinition(EntityType entityT .filter(it -> EntityIntrospector.introspect(entityType).isNotIgnored(it.getName())) .filter(it -> isBasic(it) || isEmbeddable(it)) .map(Attribute::getName) - .map(name -> newEnumValueDefinition().name(name) - .description("%s entity %s attribute".formatted(selectTypeName, name)) - .build()) + .map(name -> + newEnumValueDefinition() + .name(name) + .description("%s entity %s attribute".formatted(selectTypeName, name)) + .build() + ) .toList(); if (entityType.getAttributes().stream().anyMatch(Attribute::isAssociation)) { countFieldDefinition.argument( newArgument() .name("of") - .description("Count the number of associated records in the database for the %s aggregate".formatted(selectTypeName)) + .description( + "Count the number of associated records in the database for the %s aggregate".formatted( + selectTypeName + ) + ) .type( newEnum() .name(aggregateObjectTypeName.concat("CountOfAssociationsEnum")) @@ -484,12 +496,20 @@ private GraphQLFieldDefinition getAggregateFieldDefinition(EntityType entityT .field( newFieldDefinition() .name("by") - .description("Group by %s field container used to query aggregate data by one or more fields".formatted(selectTypeName)) + .description( + "Group by %s field container used to query aggregate data by one or more fields".formatted( + selectTypeName + ) + ) .dataFetcher(aggregateDataFetcher) .argument( newArgument() .name("field") - .description("Group by field argument used to specify %s entity field name".formatted(selectTypeName)) + .description( + "Group by field argument used to specify %s entity field name".formatted( + selectTypeName + ) + ) .type( newEnum() .name(aggregateObjectTypeName.concat("GroupByFieldsEnum")) @@ -527,7 +547,12 @@ private GraphQLFieldDefinition getAggregateFieldDefinition(EntityType entityT aggregateObjectType.field( newFieldDefinition() .name(association.getName()) - .description("Aggregate %s query field definition for the associated %s entity".formatted(selectTypeName, association.getName())) + .description( + "Aggregate %s query field definition for the associated %s entity".formatted( + selectTypeName, + association.getName() + ) + ) .dataFetcher(aggregateDataFetcher) .type( new GraphQLList( @@ -537,16 +562,29 @@ private GraphQLFieldDefinition getAggregateFieldDefinition(EntityType entityT .concat(capitalize(association.getName())) .concat("GroupByNestedAssociation") ) - .description("Aggregate %s query object type for the associated %s entity".formatted(selectTypeName, association.getName())) + .description( + "Aggregate %s query object type for the associated %s entity".formatted( + selectTypeName, + association.getName() + ) + ) .field( newFieldDefinition() .name("by") .dataFetcher(aggregateDataFetcher) - .description("Group by %s attribute field used to query aggregate data by one or more fields".formatted(association.getName())) + .description( + "Group by %s attribute field used to query aggregate data by one or more fields".formatted( + association.getName() + ) + ) .argument( newArgument() .name("field") - .description("Group by field argument used to specify associated %s entity field name".formatted(association.getName())) + .description( + "Group by field argument used to specify associated %s entity field name".formatted( + association.getName() + ) + ) .type( newEnum() .name( @@ -560,9 +598,16 @@ private GraphQLFieldDefinition getAggregateFieldDefinition(EntityType entityT ) .type(JavaScalars.GraphQLObjectScalar) ) - .field(newFieldDefinition().name("count") - .description("Count the number of records in the database for the %s associated nested aggregate".formatted(association.getName())) - .type(GraphQLInt)) + .field( + newFieldDefinition() + .name("count") + .description( + "Count the number of records in the database for the %s associated nested aggregate".formatted( + association.getName() + ) + ) + .type(GraphQLInt) + ) .build() ) ) @@ -572,8 +617,10 @@ private GraphQLFieldDefinition getAggregateFieldDefinition(EntityType entityT aggregateObjectType.field(countFieldDefinition).field(groupFieldDefinition); - var aggregateFieldDefinition = newFieldDefinition().name("aggregate") - .description("Aggregate data query field for %s entity".formatted(selectTypeName)).type(aggregateObjectType); + var aggregateFieldDefinition = newFieldDefinition() + .name("aggregate") + .description("Aggregate data query field for %s entity".formatted(selectTypeName)) + .type(aggregateObjectType); return aggregateFieldDefinition.build(); }