From 747a1c2d97743815b08b023f12c5f83a13444bd4 Mon Sep 17 00:00:00 2001 From: etienne-sf Date: Sat, 3 Dec 2022 14:47:33 +0100 Subject: [PATCH] Issue #164: descriptions of GraphQL items is now in the generated code --- CHANGELOG.md | 4 +- .../plugin/DocumentParser.java | 53 ++++++- .../plugin/language/DataFetcher.java | 2 +- .../plugin/language/Description.java | 65 +++++++++ .../plugin/language/Directive.java | 5 +- .../plugin/language/Field.java | 5 +- .../plugin/language/Type.java | 5 +- .../plugin/language/impl/AbstractType.java | 19 ++- .../plugin/language/impl/DirectiveImpl.java | 6 +- .../plugin/language/impl/FieldImpl.java | 6 +- .../client_query_mutation_executor.vm.java | 60 ++++---- .../client_query_mutation_type.vm.java | 49 ++++++- .../client_subscription_executor.vm.java | 63 +++++---- .../client_subscription_type.vm.java | 51 ++++++- .../resources/templates/enum_type.vm.java | 9 +- .../templates/generateRelaySchema.vm.graphqls | 49 +++++++ .../templates/interface_type.vm.java | 9 +- .../templates/object_content.vm.java | 77 +++++++--- .../resources/templates/object_type.vm.java | 6 +- ...server_GraphQLDataFetchersDelegate.vm.java | 40 ++++++ .../resources/templates/union_type.vm.java | 6 + .../GenerateGraphQLSchemaTest.java | 16 ++- .../plugin/test/helper/DeepComparator.java | 39 ++++- .../test/helper/DeepComparatorTest.java | 79 ++++++++++- .../allGraphQLCases/allGraphQLCases.graphqls | 133 ++++++++++++------ 25 files changed, 691 insertions(+), 165 deletions(-) create mode 100644 graphql-maven-plugin-logic/src/main/java/com/graphql_java_generator/plugin/language/Description.java diff --git a/CHANGELOG.md b/CHANGELOG.md index 99faee6e2a..28d327e14f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,9 +12,9 @@ Whether the application uses the _graphql_, the _generateClientCode_ or the _gen # Not released yet - Both modes: -* Issue #166 : Corrected an issue that prevents to request data when GraphQL field's name are java reserved keywords +* Issue #166: Corrected an issue that prevents to request data when GraphQL field's name are java reserved keywords +* Issue #164: the descriptions from the GraphQL schema is now included in the java comments for objects, fields, union... # 1.18.8 diff --git a/graphql-maven-plugin-logic/src/main/java/com/graphql_java_generator/plugin/DocumentParser.java b/graphql-maven-plugin-logic/src/main/java/com/graphql_java_generator/plugin/DocumentParser.java index 2847c6f967..b421c47893 100644 --- a/graphql-maven-plugin-logic/src/main/java/com/graphql_java_generator/plugin/DocumentParser.java +++ b/graphql-maven-plugin-logic/src/main/java/com/graphql_java_generator/plugin/DocumentParser.java @@ -25,6 +25,7 @@ import com.graphql_java_generator.plugin.conf.GraphQLConfiguration; import com.graphql_java_generator.plugin.generate_schema.GenerateGraphQLSchemaDocumentParser; import com.graphql_java_generator.plugin.language.AppliedDirective; +import com.graphql_java_generator.plugin.language.Description; import com.graphql_java_generator.plugin.language.Directive; import com.graphql_java_generator.plugin.language.DirectiveLocation; import com.graphql_java_generator.plugin.language.EnumValue; @@ -482,9 +483,14 @@ public Directive readDirectiveDefinition(DirectiveDefinition node) { directive.setArguments(node.getInputValueDefinitions().stream().map(this::readFieldTypeDefinition) .collect(Collectors.toList())); - // Let's store its comments + // Let's store its comments, directive.setComments(node.getComments()); + // its description, + if (node.getDescription() != null) { + directive.setDescription(getDescription(node.getDescription())); + } + // and all its locations for (graphql.language.DirectiveLocation dl : node.getDirectiveLocations()) { DirectiveLocation dirLoc = DirectiveLocation.valueOf(DirectiveLocation.class, dl.getName()); @@ -676,6 +682,11 @@ private ObjectType addObjectTypeDefinition(final ObjectType objectType, ObjectTy objectType.getComments() .addAll(node.getComments().stream().map(c -> c.getContent()).collect(Collectors.toList())); + // and its description, + if (node.getDescription() != null) { + objectType.setDescription(getDescription(node.getDescription())); + } + // Let's read all the interfaces this object implements for (graphql.language.Type type : node.getImplements()) { if (type instanceof TypeName) { @@ -710,6 +721,11 @@ ObjectType readInputType(ObjectType objectType, InputObjectTypeDefinition node) objectType.getComments() .addAll(node.getComments().stream().map(c -> c.getContent()).collect(Collectors.toList())); + // and its description, + if (node.getDescription() != null) { + objectType.setDescription(getDescription(node.getDescription())); + } + // Let's read all its fields for (InputValueDefinition def : node.getInputValueDefinitions()) { FieldImpl field = readFieldTypeDefinition(def); @@ -742,6 +758,11 @@ InterfaceType readInterfaceType(InterfaceTypeDefinition node) { // Let's store its comments interfaceType.setComments(node.getComments()); + // and its description, + if (node.getDescription() != null) { + interfaceType.setDescription(getDescription(node.getDescription())); + } + // Let's read all its fields interfaceType.setFields(node.getFieldDefinitions().stream().map(def -> readField(def, interfaceType)) .collect(Collectors.toList())); @@ -773,6 +794,11 @@ UnionType readUnionType(UnionType unionType, UnionTypeDefinition node) { unionType.getComments() .addAll(node.getComments().stream().map(c -> c.getContent()).collect(Collectors.toList())); + // and its description, + if (node.getDescription() != null) { + unionType.setDescription(getDescription(node.getDescription())); + } + for (graphql.language.Type memberType : node.getMemberTypes()) { String memberTypeName = (String) graphqlUtils.invokeMethod("getName", memberType); @@ -834,6 +860,11 @@ CustomScalarType readCustomScalarType(ScalarTypeDefinition node) { // Let's store its comments customScalarType.setComments(node.getComments()); + // and its description, + if (node.getDescription() != null) { + customScalarType.setDescription(getDescription(node.getDescription())); + } + return customScalarType; } @@ -853,6 +884,11 @@ public EnumType readEnumType(EnumType enumType, EnumTypeDefinition node) { enumType.getComments() .addAll(node.getComments().stream().map(c -> c.getContent()).collect(Collectors.toList())); + // and its description, + if (node.getDescription() != null) { + enumType.setDescription(getDescription(node.getDescription())); + } + for (EnumValueDefinition enumValDef : node.getEnumValueDefinitions()) { EnumValue val = EnumValueImpl.builder().name(enumValDef.getName()) .appliedDirectives(readAppliedDirectives(enumValDef.getDirectives())).build(); @@ -883,6 +919,11 @@ Field readField(FieldDefinition fieldDef, Type owningType) { // Let's store its comments field.setComments(fieldDef.getComments()); + // and its description + if (fieldDef.getDescription() != null) { + field.setDescription(getDescription(fieldDef.getDescription())); + } + return field; } @@ -1043,4 +1084,14 @@ protected String getUtilPackageName() { return ((GenerateCodeCommonConfiguration) configuration).getPackageName(); } + /** + * Returns an instance of {@link Description} based on the given String, or null of this string is null + * + * @param description + * @return + */ + private Description getDescription(graphql.language.Description description) { + return (description == null) ? null : new Description(description); + } + } \ No newline at end of file diff --git a/graphql-maven-plugin-logic/src/main/java/com/graphql_java_generator/plugin/language/DataFetcher.java b/graphql-maven-plugin-logic/src/main/java/com/graphql_java_generator/plugin/language/DataFetcher.java index 72ecf9ddb4..a3318c32fc 100644 --- a/graphql-maven-plugin-logic/src/main/java/com/graphql_java_generator/plugin/language/DataFetcher.java +++ b/graphql-maven-plugin-logic/src/main/java/com/graphql_java_generator/plugin/language/DataFetcher.java @@ -69,7 +69,7 @@ default public String getJavaName() { public DataFetchersDelegate getDataFetcherDelegate(); /** - * Retrieves the origin of this {@link DataFetcher}, that is: the name of the object which contains the field to + * Retrieves the origin of this {@link DataFetcher}, that is: the name of the object which contains the fields to * fetch.
* There are two kinds of {@link DataFetcher}: *