Search in sources :

Example 1 with UnionType

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);
}
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)

Example 2 with UnionType

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;
}
Also used : UnionType(com.graphql_java_generator.plugin.language.impl.UnionType) ObjectType(com.graphql_java_generator.plugin.language.impl.ObjectType)

Aggregations

ObjectType (com.graphql_java_generator.plugin.language.impl.ObjectType)2 UnionType (com.graphql_java_generator.plugin.language.impl.UnionType)2 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 ScalarType (com.graphql_java_generator.plugin.language.impl.ScalarType)1 Id (javax.persistence.Id)1