Skip to content

Commit 58f1061

Browse files
Add resolver argument annotation on parameterized field resolvers (#1044)
* Add resolver argument annotation test assertion Signed-off-by: Clément BUCHART <[email protected]> --------- Signed-off-by: Clément BUCHART <[email protected]>
1 parent 0c3674b commit 58f1061

File tree

4 files changed

+41
-3
lines changed

4 files changed

+41
-3
lines changed

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import java.util.Collections;
2121
import java.util.List;
2222
import java.util.Map;
23+
import java.util.stream.Collectors;
2324

2425
import static com.kobylynskyi.graphql.codegen.mapper.GraphQLTypeMapper.getDirectives;
2526
import static com.kobylynskyi.graphql.codegen.mapper.GraphQLTypeMapper.getMandatoryType;
@@ -123,9 +124,12 @@ public List<String> getAnnotations(MappingContext mappingContext, String graphQL
123124
}
124125
}
125126
// 6. Add annotations for resolver arguments
126-
if (!Utils.isEmpty(mappingContext.getResolverArgumentAnnotations())
127-
&& mappingContext.getOperationsName().contains(parentTypeName)) {
128-
annotations.addAll(mappingContext.getResolverArgumentAnnotations());
127+
if (!Utils.isEmpty(mappingContext.getResolverArgumentAnnotations())) {
128+
if (mappingContext.getOperationsName().contains(parentTypeName)
129+
|| (def instanceof InputValueDefinition
130+
&& !mappingContext.getInputsName().contains(parentTypeName))) {
131+
annotations.addAll(mappingContext.getResolverArgumentAnnotations());
132+
}
129133
}
130134
// 7. Add annotations for parametrized resolvers
131135
if (!Utils.isEmpty(mappingContext.getParametrizedResolverAnnotations())

src/main/java/com/kobylynskyi/graphql/codegen/model/MappingContext.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ public class MappingContext implements GraphQLCodegenConfiguration {
3131
private final Set<String> interfacesName;
3232
private final Set<String> unionsName;
3333
private final Set<String> operationsName;
34+
private final Set<String> inputsName;
3435
private final Map<String, Set<String>> interfaceChildren;
3536
private final GeneratedInformation generatedInformation;
3637
private final DataModelMapperFactory dataModelMapperFactory;
@@ -49,6 +50,8 @@ private MappingContext(File outputDirectory,
4950
this.typesUnionsInterfacesNames = document.getTypesUnionsInterfacesNames();
5051
this.interfacesName = document.getInterfacesNames();
5152
this.unionsName = document.getUnionsNames();
53+
this.inputsName = document.getInputDefinitions().stream().map(ExtendedDefinition::getName)
54+
.collect(Collectors.toSet());
5255
this.interfaceChildren = document.getInterfaceChildren();
5356
this.generatedInformation = generatedInformation;
5457
this.operationsName = document.getOperationsNames();
@@ -366,6 +369,10 @@ public Set<String> getOperationsName() {
366369
return operationsName;
367370
}
368371

372+
public Set<String> getInputsName() {
373+
return inputsName;
374+
}
375+
369376
public Map<String, Set<String>> getInterfaceChildren() {
370377
return interfaceChildren;
371378
}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,7 @@ void generate_CustomAnnotationMappings_Multiple() throws Exception {
200200
@Test
201201
void generate_ResolverArgumentAnnotations() throws Exception {
202202
mappingConfig.setGenerateDataFetchingEnvironmentArgumentInApis(true);
203+
mappingConfig.setGenerateParameterizedFieldsResolvers(true);
203204
mappingConfig.setResolverArgumentAnnotations(singleton(
204205
"@org.springframework.graphql.data.method.annotation.Argument"));
205206

@@ -212,6 +213,9 @@ void generate_ResolverArgumentAnnotations() throws Exception {
212213
assertSameTrimmedContent(
213214
new File("src/test/resources/expected-classes/annotation/QueryResolver_ArgumentAnnotations.java.txt"),
214215
getFileByName(files, "QueryResolver.java"));
216+
assertSameTrimmedContent(new File("src/test/resources/expected-classes/annotation/" +
217+
"EventPropertyResolver_ArgumentAnnotations.java.txt"),
218+
getFileByName(files, "EventPropertyResolver.java"));
215219
}
216220

217221
@Test
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.kobylynskyi.graphql.test1;
2+
3+
4+
/**
5+
* Resolver for EventProperty
6+
*/
7+
@javax.annotation.Generated(
8+
value = "com.kobylynskyi.graphql.codegen.GraphQLCodegen",
9+
date = "2020-12-31T23:59:59-0500"
10+
)
11+
public interface EventPropertyResolver {
12+
13+
/**
14+
* Properties
15+
*/
16+
java.util.List<EventProperty> child(EventProperty eventProperty, @org.springframework.graphql.data.method.annotation.Argument Integer first, @org.springframework.graphql.data.method.annotation.Argument Integer last, graphql.schema.DataFetchingEnvironment env) throws Exception;
17+
18+
/**
19+
* Parent event of the property
20+
*/
21+
Event parent(EventProperty eventProperty, @org.springframework.graphql.data.method.annotation.Argument EventStatus withStatus, @org.springframework.graphql.data.method.annotation.Argument String createdAfter, graphql.schema.DataFetchingEnvironment env) throws Exception;
22+
23+
}

0 commit comments

Comments
 (0)