Skip to content

Commit 747a1c2

Browse files
author
etienne-sf
committed
Issue #164: descriptions of GraphQL items is now in the generated code
1 parent 560a39d commit 747a1c2

25 files changed

+691
-165
lines changed

CHANGELOG.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ Whether the application uses the _graphql_, the _generateClientCode_ or the _gen
1212

1313
# Not released yet
1414

15-
1615
Both modes:
17-
* Issue #166 : Corrected an issue that prevents to request data when GraphQL field's name are java reserved keywords
16+
* Issue #166: Corrected an issue that prevents to request data when GraphQL field's name are java reserved keywords
17+
* Issue #164: the descriptions from the GraphQL schema is now included in the java comments for objects, fields, union...
1818

1919

2020
# 1.18.8

graphql-maven-plugin-logic/src/main/java/com/graphql_java_generator/plugin/DocumentParser.java

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import com.graphql_java_generator.plugin.conf.GraphQLConfiguration;
2626
import com.graphql_java_generator.plugin.generate_schema.GenerateGraphQLSchemaDocumentParser;
2727
import com.graphql_java_generator.plugin.language.AppliedDirective;
28+
import com.graphql_java_generator.plugin.language.Description;
2829
import com.graphql_java_generator.plugin.language.Directive;
2930
import com.graphql_java_generator.plugin.language.DirectiveLocation;
3031
import com.graphql_java_generator.plugin.language.EnumValue;
@@ -482,9 +483,14 @@ public Directive readDirectiveDefinition(DirectiveDefinition node) {
482483
directive.setArguments(node.getInputValueDefinitions().stream().map(this::readFieldTypeDefinition)
483484
.collect(Collectors.toList()));
484485

485-
// Let's store its comments
486+
// Let's store its comments,
486487
directive.setComments(node.getComments());
487488

489+
// its description,
490+
if (node.getDescription() != null) {
491+
directive.setDescription(getDescription(node.getDescription()));
492+
}
493+
488494
// and all its locations
489495
for (graphql.language.DirectiveLocation dl : node.getDirectiveLocations()) {
490496
DirectiveLocation dirLoc = DirectiveLocation.valueOf(DirectiveLocation.class, dl.getName());
@@ -676,6 +682,11 @@ private ObjectType addObjectTypeDefinition(final ObjectType objectType, ObjectTy
676682
objectType.getComments()
677683
.addAll(node.getComments().stream().map(c -> c.getContent()).collect(Collectors.toList()));
678684

685+
// and its description,
686+
if (node.getDescription() != null) {
687+
objectType.setDescription(getDescription(node.getDescription()));
688+
}
689+
679690
// Let's read all the interfaces this object implements
680691
for (graphql.language.Type type : node.getImplements()) {
681692
if (type instanceof TypeName) {
@@ -710,6 +721,11 @@ ObjectType readInputType(ObjectType objectType, InputObjectTypeDefinition node)
710721
objectType.getComments()
711722
.addAll(node.getComments().stream().map(c -> c.getContent()).collect(Collectors.toList()));
712723

724+
// and its description,
725+
if (node.getDescription() != null) {
726+
objectType.setDescription(getDescription(node.getDescription()));
727+
}
728+
713729
// Let's read all its fields
714730
for (InputValueDefinition def : node.getInputValueDefinitions()) {
715731
FieldImpl field = readFieldTypeDefinition(def);
@@ -742,6 +758,11 @@ InterfaceType readInterfaceType(InterfaceTypeDefinition node) {
742758
// Let's store its comments
743759
interfaceType.setComments(node.getComments());
744760

761+
// and its description,
762+
if (node.getDescription() != null) {
763+
interfaceType.setDescription(getDescription(node.getDescription()));
764+
}
765+
745766
// Let's read all its fields
746767
interfaceType.setFields(node.getFieldDefinitions().stream().map(def -> readField(def, interfaceType))
747768
.collect(Collectors.toList()));
@@ -773,6 +794,11 @@ UnionType readUnionType(UnionType unionType, UnionTypeDefinition node) {
773794
unionType.getComments()
774795
.addAll(node.getComments().stream().map(c -> c.getContent()).collect(Collectors.toList()));
775796

797+
// and its description,
798+
if (node.getDescription() != null) {
799+
unionType.setDescription(getDescription(node.getDescription()));
800+
}
801+
776802
for (graphql.language.Type<?> memberType : node.getMemberTypes()) {
777803
String memberTypeName = (String) graphqlUtils.invokeMethod("getName", memberType);
778804

@@ -834,6 +860,11 @@ CustomScalarType readCustomScalarType(ScalarTypeDefinition node) {
834860
// Let's store its comments
835861
customScalarType.setComments(node.getComments());
836862

863+
// and its description,
864+
if (node.getDescription() != null) {
865+
customScalarType.setDescription(getDescription(node.getDescription()));
866+
}
867+
837868
return customScalarType;
838869
}
839870

@@ -853,6 +884,11 @@ public EnumType readEnumType(EnumType enumType, EnumTypeDefinition node) {
853884
enumType.getComments()
854885
.addAll(node.getComments().stream().map(c -> c.getContent()).collect(Collectors.toList()));
855886

887+
// and its description,
888+
if (node.getDescription() != null) {
889+
enumType.setDescription(getDescription(node.getDescription()));
890+
}
891+
856892
for (EnumValueDefinition enumValDef : node.getEnumValueDefinitions()) {
857893
EnumValue val = EnumValueImpl.builder().name(enumValDef.getName())
858894
.appliedDirectives(readAppliedDirectives(enumValDef.getDirectives())).build();
@@ -883,6 +919,11 @@ Field readField(FieldDefinition fieldDef, Type owningType) {
883919
// Let's store its comments
884920
field.setComments(fieldDef.getComments());
885921

922+
// and its description
923+
if (fieldDef.getDescription() != null) {
924+
field.setDescription(getDescription(fieldDef.getDescription()));
925+
}
926+
886927
return field;
887928
}
888929

@@ -1043,4 +1084,14 @@ protected String getUtilPackageName() {
10431084
return ((GenerateCodeCommonConfiguration) configuration).getPackageName();
10441085
}
10451086

1087+
/**
1088+
* Returns an instance of {@link Description} based on the given String, or null of this string is null
1089+
*
1090+
* @param description
1091+
* @return
1092+
*/
1093+
private Description getDescription(graphql.language.Description description) {
1094+
return (description == null) ? null : new Description(description);
1095+
}
1096+
10461097
}

graphql-maven-plugin-logic/src/main/java/com/graphql_java_generator/plugin/language/DataFetcher.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ default public String getJavaName() {
6969
public DataFetchersDelegate getDataFetcherDelegate();
7070

7171
/**
72-
* Retrieves the origin of this {@link DataFetcher}, that is: the name of the object which contains the field to
72+
* Retrieves the origin of this {@link DataFetcher}, that is: the name of the object which contains the fields to
7373
* fetch.<BR/>
7474
* There are two kinds of {@link DataFetcher}:
7575
* <UL>
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/**
2+
*
3+
*/
4+
package com.graphql_java_generator.plugin.language;
5+
6+
import java.io.BufferedReader;
7+
import java.io.IOException;
8+
import java.io.StringReader;
9+
import java.util.ArrayList;
10+
import java.util.List;
11+
import java.util.stream.Collectors;
12+
13+
/**
14+
* This class matches the description of a GraphQL item, as described in the GraphQL schema. It allows several
15+
* capabilities for use in the template, for code or schema generation
16+
*
17+
* @author etienne-sf
18+
*/
19+
public class Description {
20+
21+
/** The description as it has been read from the source GraphQL schema */
22+
final graphql.language.Description description;
23+
24+
/** The decomposition of the description in lines, without the EOL character(s) */
25+
List<String> lines = null;
26+
27+
public Description(graphql.language.Description description2) {
28+
this.description = description2;
29+
}
30+
31+
/**
32+
* Returns the content of the description
33+
*
34+
* @return
35+
*/
36+
public String getContent() {
37+
return description.getContent();
38+
}
39+
40+
public boolean isMultiLine() {
41+
return description.isMultiLine();
42+
}
43+
44+
/**
45+
* Returns an array of the lines of this description. This array contains at least one item.
46+
*
47+
* @return
48+
*/
49+
public List<String> getLines() {
50+
if (lines == null) {
51+
if (description.isMultiLine()) {
52+
try (BufferedReader sr = new BufferedReader(new StringReader(description.getContent()))) {
53+
lines = sr.lines().collect(Collectors.toList());
54+
} catch (IOException e) {
55+
throw new RuntimeException(e.getMessage(), e);
56+
}
57+
} else {
58+
lines = new ArrayList<String>(1);
59+
lines.add(description.getContent());
60+
}
61+
}
62+
return lines;
63+
}
64+
65+
}

graphql-maven-plugin-logic/src/main/java/com/graphql_java_generator/plugin/language/Directive.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,12 @@ public interface Directive {
1818
/** A directive may have arguments. An argument is actually a field. */
1919
public List<Field> getArguments();
2020

21-
/** Returns the comments that have been found for this object, in the provided GraphQL schema */
21+
/** Returns the comments that have been found before this object, in the provided GraphQL schema */
2222
public List<String> getComments();
2323

24+
/** Returns the description for this object, in the provided GraphQL schema */
25+
public Description getDescription();
26+
2427
/** Returns the list of location that this directive may have */
2528
public List<DirectiveLocation> getDirectiveLocations();
2629

graphql-maven-plugin-logic/src/main/java/com/graphql_java_generator/plugin/language/Field.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,9 +201,12 @@ default public Set<String> getFieldJavaFullClassnamesFromImplementedInterface()
201201
*/
202202
public String getAnnotation();
203203

204-
/** Returns the comments that have been found for this object, in the provided GraphQL schema */
204+
/** Returns the comments that have been found before this object, in the provided GraphQL schema */
205205
public List<String> getComments();
206206

207+
/** Returns the description for this object, in the provided GraphQL schema */
208+
public Description getDescription();
209+
207210
/**
208211
* Convert the given name, which is supposed to be in camel case (for instance: thisIsCamelCase) to a pascal case
209212
* string (for instance: ThisIsCamelCase).

graphql-maven-plugin-logic/src/main/java/com/graphql_java_generator/plugin/language/Type.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,9 +129,12 @@ default public String getTargetFileName(String fileType) {
129129
*/
130130
public void addAnnotation(String annotationToAdd, boolean replace);
131131

132-
/** Returns the comments that have been found for this object, in the provided GraphQL schema */
132+
/** Returns the comments that have been found before this object, in the provided GraphQL schema */
133133
public List<String> getComments();
134134

135+
/** Returns the description for this object, in the provided GraphQL schema */
136+
public Description getDescription();
137+
135138
/**
136139
* The GraphQlType for this type
137140
*

graphql-maven-plugin-logic/src/main/java/com/graphql_java_generator/plugin/language/impl/AbstractType.java

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import com.graphql_java_generator.plugin.conf.CommonConfiguration;
1313
import com.graphql_java_generator.plugin.conf.GraphQLConfiguration;
1414
import com.graphql_java_generator.plugin.language.AppliedDirective;
15+
import com.graphql_java_generator.plugin.language.Description;
1516
import com.graphql_java_generator.plugin.language.Field;
1617
import com.graphql_java_generator.plugin.language.Type;
1718
import com.graphql_java_generator.util.GraphqlUtils;
@@ -53,9 +54,12 @@ public abstract class AbstractType implements Type {
5354
/** All directives that have been defined in the GraphQL schema for this type */
5455
private List<AppliedDirective> appliedDirectives = new ArrayList<>();
5556

56-
/** The comments that have been found for this object, in the provided GraphQL schema */
57+
/** The comments that have been found before this type, in the provided GraphQL schema */
5758
private List<String> comments = new ArrayList<>();
5859

60+
/** The description of this type, in the provided GraphQL schema */
61+
private Description description = null;
62+
5963
/** The GraphQL type for this type */
6064
final private GraphQlType graphQlType;
6165

@@ -89,18 +93,19 @@ public String getClassSimpleName() {
8993
return getJavaName();
9094
}
9195

92-
@Override
93-
public String getJavaName() {
94-
// Optionally add a prefix and/or suffix to the name
95-
String name = Stream.of(getPrefix(), getName(), getSuffix())
96-
.filter(Objects::nonNull).collect(Collectors.joining());
96+
@Override
97+
public String getJavaName() {
98+
// Optionally add a prefix and/or suffix to the name
99+
String name = Stream.of(getPrefix(), getName(), getSuffix()).filter(Objects::nonNull)
100+
.collect(Collectors.joining());
97101

98102
return GraphqlUtils.graphqlUtils.getJavaName(name);
99-
}
103+
}
100104

101105
protected String getPrefix() {
102106
return "";
103107
}
108+
104109
protected String getSuffix() {
105110
return "";
106111
}

graphql-maven-plugin-logic/src/main/java/com/graphql_java_generator/plugin/language/impl/DirectiveImpl.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import java.util.ArrayList;
77
import java.util.List;
88

9+
import com.graphql_java_generator.plugin.language.Description;
910
import com.graphql_java_generator.plugin.language.Directive;
1011
import com.graphql_java_generator.plugin.language.DirectiveLocation;
1112
import com.graphql_java_generator.plugin.language.Field;
@@ -29,9 +30,12 @@ public class DirectiveImpl implements Directive {
2930
/** Returns the list of location that this directive may have */
3031
private List<DirectiveLocation> directiveLocations = new ArrayList<>();
3132

32-
/** The comments that have been found for this object, in the provided GraphQL schema */
33+
/** The comments that have been found before this object, in the provided GraphQL schema */
3334
private List<String> comments = new ArrayList<>();
3435

36+
/** The description of this directive, in the provided GraphQL schema */
37+
private Description description = null;
38+
3539
/**
3640
* True if this directive is a standard GraphQL directive, or if it has been defined in the GraphQL schema. Default
3741
* value is false (non standard)

graphql-maven-plugin-logic/src/main/java/com/graphql_java_generator/plugin/language/impl/FieldImpl.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import com.graphql_java_generator.plugin.DocumentParser;
1010
import com.graphql_java_generator.plugin.generate_code.GenerateCodeDocumentParser;
1111
import com.graphql_java_generator.plugin.language.AppliedDirective;
12+
import com.graphql_java_generator.plugin.language.Description;
1213
import com.graphql_java_generator.plugin.language.Field;
1314
import com.graphql_java_generator.plugin.language.FieldTypeAST;
1415
import com.graphql_java_generator.plugin.language.Relation;
@@ -84,10 +85,13 @@ public class FieldImpl implements Field {
8485
@Builder.Default
8586
private List<AppliedDirective> appliedDirectives = new ArrayList<>();
8687

87-
/** The comments that have been found for this field, in the provided GraphQL schema */
88+
/** The comments that have been found before this field, in the provided GraphQL schema */
8889
@Builder.Default
8990
private List<String> comments = new ArrayList<>();
9091

92+
/** The description of this field, in the provided GraphQL schema */
93+
private Description description;
94+
9195
@Override
9296
public Type getType() {
9397
return documentParser.getType(getGraphQLTypeSimpleName());

0 commit comments

Comments
 (0)