use of com.graphql_java_generator.annotation.RequestType in project graphql-maven-plugin-project by graphql-java-generator.
the class GenerateCodeDocumentParser method addTypeAnnotationForClientMode.
/**
* This method add the needed annotation(s) to the given type, when in client mode
*
* @param type
*/
void addTypeAnnotationForClientMode(Type type) {
if (type instanceof InterfaceType || type instanceof UnionType) {
if (configuration.isGenerateJacksonAnnotations()) {
type.addImport(configuration.getPackageName(), JsonTypeInfo.class.getName());
type.addImport(configuration.getPackageName(), JsonTypeInfo.Id.class.getName());
type.addAnnotation("@JsonTypeInfo(use = Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = \"__typename\", visible = true)");
// jsonSubTypes annotation looks like this:
// @JsonSubTypes({ @Type(value = Droid.class, name = "Droid"), @Type(value = Human.class, name =
// "Human") })
StringBuffer jsonSubTypes = new StringBuffer();
type.addImport(configuration.getPackageName(), JsonSubTypes.class.getName());
type.addImport(configuration.getPackageName(), JsonSubTypes.Type.class.getName());
jsonSubTypes.append("@JsonSubTypes({");
boolean addSeparator = false;
List<ObjectType> types;
if (type instanceof InterfaceType)
types = ((InterfaceType) type).getImplementingTypes();
else
types = ((UnionType) type).getMemberTypes();
for (ObjectType t : types) {
// No separator for the first iteration
if (addSeparator)
jsonSubTypes.append(",");
else
addSeparator = true;
jsonSubTypes.append(" @Type(value = ").append(t.getName()).append(".class, name = \"").append(t.getName()).append("\")");
}
jsonSubTypes.append(" })");
type.addAnnotation(jsonSubTypes.toString());
}
}
// query/mutation/subscription
if (type instanceof ObjectType && ((ObjectType) type).getRequestType() != null) {
type.addImport(configuration.getPackageName(), GraphQLQuery.class.getName());
type.addImport(configuration.getPackageName(), RequestType.class.getName());
type.addAnnotation("@GraphQLQuery(name = \"" + type.getName() + "\", type = RequestType." + ((ObjectType) type).getRequestType() + ")");
}
// Let's add the annotations, that are common to both the client and the server mode
addTypeAnnotationForBothClientAndServerMode(type);
}
Aggregations