未验证 提交 3b430786 编写于 作者: R Robbert Noordzij 提交者: GitHub

Add directive support for types #660 (#665)

Co-authored-by: NRobbert Noordzij <robbert@robbertnoordzij.nl>
上级 e15ac840
......@@ -272,7 +272,7 @@ public interface GraphQLTypeMapper {
default List<String> getAnnotations(MappingContext mappingContext, ExtendedDefinition<?, ?> extendedDefinition) {
NamedNode def = extendedDefinition != null ? extendedDefinition.getDefinition() : null;
return getAnnotations(mappingContext, extendedDefinition.getName(), extendedDefinition.getName(), null,
Collections.emptyList(), false, def);
extendedDefinition.getDirectives(), false, def);
}
default List<String> getAnnotations(MappingContext mappingContext, String name) {
......
......@@ -115,6 +115,26 @@ public abstract class ExtendedDefinition<T extends NamedNode<T>, E extends T> {
return directives;
}
/**
* Return all directives for this definition
*
* @return list of directive names
*/
public List<Directive> getDirectives() {
List<Directive> directives = new ArrayList<>();
if (this.definition instanceof DirectivesContainer) {
List<Directive> definitionDirectives = ((DirectivesContainer<?>) this.definition).getDirectives();
if (!Utils.isEmpty(definitionDirectives)) {
directives.addAll(definitionDirectives);
}
this.extensions.stream().filter(Objects::nonNull)
.map(DirectivesContainer.class::cast)
.map(DirectivesContainer::getDirectives).filter(Objects::nonNull)
.forEach(ds -> ds.forEach(d -> directives.add(((Directive) d))));
}
return directives;
}
public T getDefinition() {
return definition;
}
......
......@@ -209,6 +209,7 @@ class GraphQLCodegenAnnotationsTest {
"int={{int}}, " +
"n={{n?toString}})"));
directiveAnnotationsMapping.put("valid", singletonList("@javax.validation.Valid"));
directiveAnnotationsMapping.put("customResolver", singletonList("@com.example.CustomAnnotation"));
mappingConfig.setDirectiveAnnotationsMapping(directiveAnnotationsMapping);
new JavaGraphQLCodegen(singletonList("src/test/resources/schemas/test.graphqls"),
......@@ -221,6 +222,9 @@ class GraphQLCodegenAnnotationsTest {
assertSameTrimmedContent(
new File("src/test/resources/expected-classes/annotation/MutationResolver.java.txt"),
getFileByName(files, "MutationResolver.java"));
assertSameTrimmedContent(
new File("src/test/resources/expected-classes/annotation/EventProperty.java.txt"),
getFileByName(files, "EventProperty.java"));
}
}
package com.kobylynskyi.graphql.test1;
/**
* An event property have all possible types
*/
@javax.annotation.Generated(
value = "com.kobylynskyi.graphql.codegen.GraphQLCodegen",
date = "2020-12-31T23:59:59-0500"
)
@com.example.CustomAnnotation
public class EventProperty implements java.io.Serializable {
private Double floatVal;
private Boolean booleanVal;
private int intVal;
private java.util.List<Integer> intVals;
private String stringVal;
private java.util.List<EventProperty> child;
private Event parent;
public EventProperty() {
}
public EventProperty(Double floatVal, Boolean booleanVal, int intVal, java.util.List<Integer> intVals, String stringVal, java.util.List<EventProperty> child, Event parent) {
this.floatVal = floatVal;
this.booleanVal = booleanVal;
this.intVal = intVal;
this.intVals = intVals;
this.stringVal = stringVal;
this.child = child;
this.parent = parent;
}
/**
* Float property
* with multiline comment
*/
public Double getFloatVal() {
return floatVal;
}
/**
* Float property
* with multiline comment
*/
public void setFloatVal(Double floatVal) {
this.floatVal = floatVal;
}
public Boolean getBooleanVal() {
return booleanVal;
}
public void setBooleanVal(Boolean booleanVal) {
this.booleanVal = booleanVal;
}
public int getIntVal() {
return intVal;
}
public void setIntVal(int intVal) {
this.intVal = intVal;
}
/**
* primitive should not be generated
*/
public java.util.List<Integer> getIntVals() {
return intVals;
}
/**
* primitive should not be generated
*/
public void setIntVals(java.util.List<Integer> intVals) {
this.intVals = intVals;
}
/**
* String comment
*/
public String getStringVal() {
return stringVal;
}
/**
* String comment
*/
public void setStringVal(String stringVal) {
this.stringVal = stringVal;
}
/**
* Properties
*/
public java.util.List<EventProperty> getChild() {
return child;
}
/**
* Properties
*/
public void setChild(java.util.List<EventProperty> child) {
this.child = child;
}
/**
* Parent event of the property
*/
public Event getParent() {
return parent;
}
/**
* Parent event of the property
*/
public void setParent(Event parent) {
this.parent = parent;
}
public static EventProperty.Builder builder() {
return new EventProperty.Builder();
}
public static class Builder {
private Double floatVal;
private Boolean booleanVal;
private int intVal;
private java.util.List<Integer> intVals;
private String stringVal;
private java.util.List<EventProperty> child;
private Event parent;
public Builder() {
}
/**
* Float property
* with multiline comment
*/
public Builder setFloatVal(Double floatVal) {
this.floatVal = floatVal;
return this;
}
public Builder setBooleanVal(Boolean booleanVal) {
this.booleanVal = booleanVal;
return this;
}
public Builder setIntVal(int intVal) {
this.intVal = intVal;
return this;
}
/**
* primitive should not be generated
*/
public Builder setIntVals(java.util.List<Integer> intVals) {
this.intVals = intVals;
return this;
}
/**
* String comment
*/
public Builder setStringVal(String stringVal) {
this.stringVal = stringVal;
return this;
}
/**
* Properties
*/
public Builder setChild(java.util.List<EventProperty> child) {
this.child = child;
return this;
}
/**
* Parent event of the property
*/
public Builder setParent(Event parent) {
this.parent = parent;
return this;
}
public EventProperty build() {
return new EventProperty(floatVal, booleanVal, intVal, intVals, stringVal, child, parent);
}
}
}
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册