Skip to content

Commit 3b43078

Browse files
robbertnoordzijRobbert Noordzij
andauthored
Add directive support for types #660 (#665)
Co-authored-by: Robbert Noordzij <robbert@robbertnoordzij.nl>
1 parent e15ac84 commit 3b43078

File tree

4 files changed

+217
-1
lines changed

4 files changed

+217
-1
lines changed

src/main/java/com/kobylynskyi/graphql/codegen/mapper/GraphQLTypeMapper.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,7 @@ default List<String> getAnnotations(MappingContext mappingContext, Type<?> type,
272272
default List<String> getAnnotations(MappingContext mappingContext, ExtendedDefinition<?, ?> extendedDefinition) {
273273
NamedNode def = extendedDefinition != null ? extendedDefinition.getDefinition() : null;
274274
return getAnnotations(mappingContext, extendedDefinition.getName(), extendedDefinition.getName(), null,
275-
Collections.emptyList(), false, def);
275+
extendedDefinition.getDirectives(), false, def);
276276
}
277277

278278
default List<String> getAnnotations(MappingContext mappingContext, String name) {

src/main/java/com/kobylynskyi/graphql/codegen/model/definitions/ExtendedDefinition.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,26 @@ public List<String> getDirectiveNames() {
115115
return directives;
116116
}
117117

118+
/**
119+
* Return all directives for this definition
120+
*
121+
* @return list of directive names
122+
*/
123+
public List<Directive> getDirectives() {
124+
List<Directive> directives = new ArrayList<>();
125+
if (this.definition instanceof DirectivesContainer) {
126+
List<Directive> definitionDirectives = ((DirectivesContainer<?>) this.definition).getDirectives();
127+
if (!Utils.isEmpty(definitionDirectives)) {
128+
directives.addAll(definitionDirectives);
129+
}
130+
this.extensions.stream().filter(Objects::nonNull)
131+
.map(DirectivesContainer.class::cast)
132+
.map(DirectivesContainer::getDirectives).filter(Objects::nonNull)
133+
.forEach(ds -> ds.forEach(d -> directives.add(((Directive) d))));
134+
}
135+
return directives;
136+
}
137+
118138
public T getDefinition() {
119139
return definition;
120140
}

src/test/java/com/kobylynskyi/graphql/codegen/GraphQLCodegenAnnotationsTest.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,7 @@ void generate_Directives() throws Exception {
209209
"int={{int}}, " +
210210
"n={{n?toString}})"));
211211
directiveAnnotationsMapping.put("valid", singletonList("@javax.validation.Valid"));
212+
directiveAnnotationsMapping.put("customResolver", singletonList("@com.example.CustomAnnotation"));
212213
mappingConfig.setDirectiveAnnotationsMapping(directiveAnnotationsMapping);
213214

214215
new JavaGraphQLCodegen(singletonList("src/test/resources/schemas/test.graphqls"),
@@ -221,6 +222,9 @@ void generate_Directives() throws Exception {
221222
assertSameTrimmedContent(
222223
new File("src/test/resources/expected-classes/annotation/MutationResolver.java.txt"),
223224
getFileByName(files, "MutationResolver.java"));
225+
assertSameTrimmedContent(
226+
new File("src/test/resources/expected-classes/annotation/EventProperty.java.txt"),
227+
getFileByName(files, "EventProperty.java"));
224228
}
225229

226230
}
Lines changed: 192 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,192 @@
1+
package com.kobylynskyi.graphql.test1;
2+
3+
4+
/**
5+
* An event property have all possible types
6+
*/
7+
@javax.annotation.Generated(
8+
value = "com.kobylynskyi.graphql.codegen.GraphQLCodegen",
9+
date = "2020-12-31T23:59:59-0500"
10+
)
11+
@com.example.CustomAnnotation
12+
public class EventProperty implements java.io.Serializable {
13+
14+
private Double floatVal;
15+
private Boolean booleanVal;
16+
private int intVal;
17+
private java.util.List<Integer> intVals;
18+
private String stringVal;
19+
private java.util.List<EventProperty> child;
20+
private Event parent;
21+
22+
public EventProperty() {
23+
}
24+
25+
public EventProperty(Double floatVal, Boolean booleanVal, int intVal, java.util.List<Integer> intVals, String stringVal, java.util.List<EventProperty> child, Event parent) {
26+
this.floatVal = floatVal;
27+
this.booleanVal = booleanVal;
28+
this.intVal = intVal;
29+
this.intVals = intVals;
30+
this.stringVal = stringVal;
31+
this.child = child;
32+
this.parent = parent;
33+
}
34+
35+
/**
36+
* Float property
37+
* with multiline comment
38+
*/
39+
public Double getFloatVal() {
40+
return floatVal;
41+
}
42+
/**
43+
* Float property
44+
* with multiline comment
45+
*/
46+
public void setFloatVal(Double floatVal) {
47+
this.floatVal = floatVal;
48+
}
49+
50+
public Boolean getBooleanVal() {
51+
return booleanVal;
52+
}
53+
public void setBooleanVal(Boolean booleanVal) {
54+
this.booleanVal = booleanVal;
55+
}
56+
57+
public int getIntVal() {
58+
return intVal;
59+
}
60+
public void setIntVal(int intVal) {
61+
this.intVal = intVal;
62+
}
63+
64+
/**
65+
* primitive should not be generated
66+
*/
67+
public java.util.List<Integer> getIntVals() {
68+
return intVals;
69+
}
70+
/**
71+
* primitive should not be generated
72+
*/
73+
public void setIntVals(java.util.List<Integer> intVals) {
74+
this.intVals = intVals;
75+
}
76+
77+
/**
78+
* String comment
79+
*/
80+
public String getStringVal() {
81+
return stringVal;
82+
}
83+
/**
84+
* String comment
85+
*/
86+
public void setStringVal(String stringVal) {
87+
this.stringVal = stringVal;
88+
}
89+
90+
/**
91+
* Properties
92+
*/
93+
public java.util.List<EventProperty> getChild() {
94+
return child;
95+
}
96+
/**
97+
* Properties
98+
*/
99+
public void setChild(java.util.List<EventProperty> child) {
100+
this.child = child;
101+
}
102+
103+
/**
104+
* Parent event of the property
105+
*/
106+
public Event getParent() {
107+
return parent;
108+
}
109+
/**
110+
* Parent event of the property
111+
*/
112+
public void setParent(Event parent) {
113+
this.parent = parent;
114+
}
115+
116+
117+
118+
public static EventProperty.Builder builder() {
119+
return new EventProperty.Builder();
120+
}
121+
122+
public static class Builder {
123+
124+
private Double floatVal;
125+
private Boolean booleanVal;
126+
private int intVal;
127+
private java.util.List<Integer> intVals;
128+
private String stringVal;
129+
private java.util.List<EventProperty> child;
130+
private Event parent;
131+
132+
public Builder() {
133+
}
134+
135+
/**
136+
* Float property
137+
* with multiline comment
138+
*/
139+
public Builder setFloatVal(Double floatVal) {
140+
this.floatVal = floatVal;
141+
return this;
142+
}
143+
144+
public Builder setBooleanVal(Boolean booleanVal) {
145+
this.booleanVal = booleanVal;
146+
return this;
147+
}
148+
149+
public Builder setIntVal(int intVal) {
150+
this.intVal = intVal;
151+
return this;
152+
}
153+
154+
/**
155+
* primitive should not be generated
156+
*/
157+
public Builder setIntVals(java.util.List<Integer> intVals) {
158+
this.intVals = intVals;
159+
return this;
160+
}
161+
162+
/**
163+
* String comment
164+
*/
165+
public Builder setStringVal(String stringVal) {
166+
this.stringVal = stringVal;
167+
return this;
168+
}
169+
170+
/**
171+
* Properties
172+
*/
173+
public Builder setChild(java.util.List<EventProperty> child) {
174+
this.child = child;
175+
return this;
176+
}
177+
178+
/**
179+
* Parent event of the property
180+
*/
181+
public Builder setParent(Event parent) {
182+
this.parent = parent;
183+
return this;
184+
}
185+
186+
187+
public EventProperty build() {
188+
return new EventProperty(floatVal, booleanVal, intVal, intVals, stringVal, child, parent);
189+
}
190+
191+
}
192+
}

0 commit comments

Comments
 (0)