Search in sources :

Example 1 with RequestType

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);
}
Also used : UnionType(com.graphql_java_generator.plugin.language.impl.UnionType) GraphQLUnionType(com.graphql_java_generator.annotation.GraphQLUnionType) ObjectType(com.graphql_java_generator.plugin.language.impl.ObjectType) GraphQLObjectType(com.graphql_java_generator.annotation.GraphQLObjectType) GraphQLInterfaceType(com.graphql_java_generator.annotation.GraphQLInterfaceType) InterfaceType(com.graphql_java_generator.plugin.language.impl.InterfaceType) JsonSubTypes(com.fasterxml.jackson.annotation.JsonSubTypes) UnionType(com.graphql_java_generator.plugin.language.impl.UnionType) GraphQlType(com.graphql_java_generator.plugin.language.Type.GraphQlType) GraphQLInterfaceType(com.graphql_java_generator.annotation.GraphQLInterfaceType) RequestType(com.graphql_java_generator.annotation.RequestType) GraphQLInputType(com.graphql_java_generator.annotation.GraphQLInputType) RelationType(com.graphql_java_generator.plugin.language.RelationType) GraphQLUnionType(com.graphql_java_generator.annotation.GraphQLUnionType) AbstractType(com.graphql_java_generator.plugin.language.impl.AbstractType) InterfaceType(com.graphql_java_generator.plugin.language.impl.InterfaceType) Type(com.graphql_java_generator.plugin.language.Type) ScalarType(com.graphql_java_generator.plugin.language.impl.ScalarType) ObjectType(com.graphql_java_generator.plugin.language.impl.ObjectType) EnumType(com.graphql_java_generator.plugin.language.impl.EnumType) GraphQLObjectType(com.graphql_java_generator.annotation.GraphQLObjectType) CustomScalarType(com.graphql_java_generator.plugin.language.impl.CustomScalarType) JsonTypeInfo(com.fasterxml.jackson.annotation.JsonTypeInfo) Id(javax.persistence.Id) GraphQLQuery(com.graphql_java_generator.annotation.GraphQLQuery) RequestType(com.graphql_java_generator.annotation.RequestType)

Aggregations

JsonSubTypes (com.fasterxml.jackson.annotation.JsonSubTypes)1 JsonTypeInfo (com.fasterxml.jackson.annotation.JsonTypeInfo)1 GraphQLInputType (com.graphql_java_generator.annotation.GraphQLInputType)1 GraphQLInterfaceType (com.graphql_java_generator.annotation.GraphQLInterfaceType)1 GraphQLObjectType (com.graphql_java_generator.annotation.GraphQLObjectType)1 GraphQLQuery (com.graphql_java_generator.annotation.GraphQLQuery)1 GraphQLUnionType (com.graphql_java_generator.annotation.GraphQLUnionType)1 RequestType (com.graphql_java_generator.annotation.RequestType)1 RelationType (com.graphql_java_generator.plugin.language.RelationType)1 Type (com.graphql_java_generator.plugin.language.Type)1 GraphQlType (com.graphql_java_generator.plugin.language.Type.GraphQlType)1 AbstractType (com.graphql_java_generator.plugin.language.impl.AbstractType)1 CustomScalarType (com.graphql_java_generator.plugin.language.impl.CustomScalarType)1 EnumType (com.graphql_java_generator.plugin.language.impl.EnumType)1 InterfaceType (com.graphql_java_generator.plugin.language.impl.InterfaceType)1 ObjectType (com.graphql_java_generator.plugin.language.impl.ObjectType)1 ScalarType (com.graphql_java_generator.plugin.language.impl.ScalarType)1 UnionType (com.graphql_java_generator.plugin.language.impl.UnionType)1 Id (javax.persistence.Id)1