use of com.graphql_java_generator.plugin.language.impl.UnionType 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);
}
use of com.graphql_java_generator.plugin.language.impl.UnionType in project graphql-maven-plugin-project by graphql-java-generator.
the class DocumentParser method readUnionType.
/**
* Read an union type from its GraphQL definition
*
* @param node
* @return
*/
UnionType readUnionType(UnionTypeDefinition node) {
UnionType unionType = new UnionType(node.getName(), configuration, this);
unionType.setAppliedDirectives(readAppliedDirectives(node.getDirectives()));
// Let's store its comments
unionType.setComments(node.getComments());
for (graphql.language.Type<?> memberType : node.getMemberTypes()) {
String memberTypeName = (String) graphqlUtils.invokeMethod("getName", memberType);
// We can not use getType yet, as the type list is not filled.
ObjectType type = null;
for (ObjectType ot : objectTypes) {
if (ot.getName().equals(memberTypeName)) {
type = ot;
break;
}
}
if (type == null) {
throw new RuntimeException("Could not find the ObjectType named '" + memberTypeName + "'");
}
type.getMemberOfUnions().add(unionType);
type.getImplementz().add(unionType.getName());
unionType.getMemberTypes().add(type);
}
return unionType;
}
Aggregations