Skip to content

Commit 56c5872

Browse files
authored
Add aggregate query schema field and type descriptions (#489)
* Add aggregate query schema field and type descriptions * Apply Prettier Java formatting
1 parent d52217c commit 56c5872

File tree

3 files changed

+73
-7
lines changed

3 files changed

+73
-7
lines changed

schema/src/main/java/com/introproventures/graphql/jpa/query/schema/impl/GraphQLJpaSchemaBuilder.java

+71-5
Original file line numberDiff line numberDiff line change
@@ -421,7 +421,9 @@ private GraphQLFieldDefinition getAggregateFieldDefinition(EntityType<?> entityT
421421
final var selectTypeName = resolveSelectTypeName(entityType);
422422
final var aggregateObjectTypeName = selectTypeName.concat("Aggregate");
423423

424-
var aggregateObjectType = newObject().name(aggregateObjectTypeName);
424+
var aggregateObjectType = newObject()
425+
.name(aggregateObjectTypeName)
426+
.description("%s entity aggregate object type".formatted(selectTypeName));
425427

426428
DataFetcher<Object> aggregateDataFetcher = environment -> {
427429
Map<String, Object> source = environment.getSource();
@@ -431,6 +433,7 @@ private GraphQLFieldDefinition getAggregateFieldDefinition(EntityType<?> entityT
431433

432434
var countFieldDefinition = newFieldDefinition()
433435
.name("count")
436+
.description("Count the number of records in the database for the %s aggregate".formatted(selectTypeName))
434437
.dataFetcher(aggregateDataFetcher)
435438
.type(GraphQLInt);
436439

@@ -440,7 +443,12 @@ private GraphQLFieldDefinition getAggregateFieldDefinition(EntityType<?> entityT
440443
.filter(it -> EntityIntrospector.introspect(entityType).isNotIgnored(it.getName()))
441444
.filter(Attribute::isAssociation)
442445
.map(Attribute::getName)
443-
.map(name -> newEnumValueDefinition().name(name).build())
446+
.map(name ->
447+
newEnumValueDefinition()
448+
.name(name)
449+
.description("%s entity associated %s child entity".formatted(selectTypeName, name))
450+
.build()
451+
)
444452
.toList();
445453

446454
var fieldsEnumValueDefinitions = entityType
@@ -449,16 +457,27 @@ private GraphQLFieldDefinition getAggregateFieldDefinition(EntityType<?> entityT
449457
.filter(it -> EntityIntrospector.introspect(entityType).isNotIgnored(it.getName()))
450458
.filter(it -> isBasic(it) || isEmbeddable(it))
451459
.map(Attribute::getName)
452-
.map(name -> newEnumValueDefinition().name(name).build())
460+
.map(name ->
461+
newEnumValueDefinition()
462+
.name(name)
463+
.description("%s entity %s attribute".formatted(selectTypeName, name))
464+
.build()
465+
)
453466
.toList();
454467

455468
if (entityType.getAttributes().stream().anyMatch(Attribute::isAssociation)) {
456469
countFieldDefinition.argument(
457470
newArgument()
458471
.name("of")
472+
.description(
473+
"Count the number of associated records in the database for the %s aggregate".formatted(
474+
selectTypeName
475+
)
476+
)
459477
.type(
460478
newEnum()
461479
.name(aggregateObjectTypeName.concat("CountOfAssociationsEnum"))
480+
.description("%s entity associated entity name values".formatted(selectTypeName))
462481
.values(associationEnumValueDefinitions)
463482
.build()
464483
)
@@ -467,21 +486,34 @@ private GraphQLFieldDefinition getAggregateFieldDefinition(EntityType<?> entityT
467486

468487
var groupFieldDefinition = newFieldDefinition()
469488
.name("group")
489+
.description("Group by %s entity query field aggregated by multiple fields".formatted(selectTypeName))
470490
.dataFetcher(aggregateDataFetcher)
471491
.type(
472492
new GraphQLList(
473493
newObject()
474494
.name(aggregateObjectTypeName.concat("GroupBy"))
495+
.description("%s entity group by object type".formatted(selectTypeName))
475496
.field(
476497
newFieldDefinition()
477498
.name("by")
499+
.description(
500+
"Group by %s field container used to query aggregate data by one or more fields".formatted(
501+
selectTypeName
502+
)
503+
)
478504
.dataFetcher(aggregateDataFetcher)
479505
.argument(
480506
newArgument()
481507
.name("field")
508+
.description(
509+
"Group by field argument used to specify %s entity field name".formatted(
510+
selectTypeName
511+
)
512+
)
482513
.type(
483514
newEnum()
484515
.name(aggregateObjectTypeName.concat("GroupByFieldsEnum"))
516+
.description("%s entity field name values".formatted(selectTypeName))
485517
.values(fieldsEnumValueDefinitions)
486518
.build()
487519
)
@@ -515,6 +547,12 @@ private GraphQLFieldDefinition getAggregateFieldDefinition(EntityType<?> entityT
515547
aggregateObjectType.field(
516548
newFieldDefinition()
517549
.name(association.getName())
550+
.description(
551+
"Aggregate %s query field definition for the associated %s entity".formatted(
552+
selectTypeName,
553+
association.getName()
554+
)
555+
)
518556
.dataFetcher(aggregateDataFetcher)
519557
.type(
520558
new GraphQLList(
@@ -524,13 +562,29 @@ private GraphQLFieldDefinition getAggregateFieldDefinition(EntityType<?> entityT
524562
.concat(capitalize(association.getName()))
525563
.concat("GroupByNestedAssociation")
526564
)
565+
.description(
566+
"Aggregate %s query object type for the associated %s entity".formatted(
567+
selectTypeName,
568+
association.getName()
569+
)
570+
)
527571
.field(
528572
newFieldDefinition()
529573
.name("by")
530574
.dataFetcher(aggregateDataFetcher)
575+
.description(
576+
"Group by %s attribute field used to query aggregate data by one or more fields".formatted(
577+
association.getName()
578+
)
579+
)
531580
.argument(
532581
newArgument()
533582
.name("field")
583+
.description(
584+
"Group by field argument used to specify associated %s entity field name".formatted(
585+
association.getName()
586+
)
587+
)
534588
.type(
535589
newEnum()
536590
.name(
@@ -544,7 +598,16 @@ private GraphQLFieldDefinition getAggregateFieldDefinition(EntityType<?> entityT
544598
)
545599
.type(JavaScalars.GraphQLObjectScalar)
546600
)
547-
.field(newFieldDefinition().name("count").type(GraphQLInt))
601+
.field(
602+
newFieldDefinition()
603+
.name("count")
604+
.description(
605+
"Count the number of records in the database for the %s associated nested aggregate".formatted(
606+
association.getName()
607+
)
608+
)
609+
.type(GraphQLInt)
610+
)
548611
.build()
549612
)
550613
)
@@ -554,7 +617,10 @@ private GraphQLFieldDefinition getAggregateFieldDefinition(EntityType<?> entityT
554617

555618
aggregateObjectType.field(countFieldDefinition).field(groupFieldDefinition);
556619

557-
var aggregateFieldDefinition = newFieldDefinition().name("aggregate").type(aggregateObjectType);
620+
var aggregateFieldDefinition = newFieldDefinition()
621+
.name("aggregate")
622+
.description("Aggregate data query field for %s entity".formatted(selectTypeName))
623+
.type(aggregateObjectType);
558624

559625
return aggregateFieldDefinition.build();
560626
}

schema/src/test/java/com/introproventures/graphql/jpa/query/converter/GraphQLJpaQueryAggregateTests.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ public Object serialize(final Object input) {
8080
.filter(VariableValue.class::isInstance)
8181
.map(VariableValue.class::cast)
8282
.map(it -> Optional.ofNullable(it.getValue()).orElse(Optional.empty()))
83-
.orElse(input);
83+
.orElseGet(() -> super.serialize(input));
8484
}
8585
}
8686
)

tests/gatling/src/main/java/com/introproventures/graphql/jpa/query/example/Application.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public Object serialize(final Object input) {
6464
.filter(VariableValue.class::isInstance)
6565
.map(VariableValue.class::cast)
6666
.map(it -> Optional.ofNullable(it.getValue()).orElse(Optional.empty()))
67-
.orElse(input);
67+
.orElseGet(() -> super.serialize(input));
6868
}
6969
}
7070
)

0 commit comments

Comments
 (0)