Search in sources :

Example 1 with GraphQLQuery

use of com.graphql_java_generator.annotation.GraphQLQuery in project graphql-maven-plugin-project by graphql-java-generator.

the class QueryField method isScalar.

/**
 * Indicates whether this field is a scalar or not.
 *
 * @return true if this field is a scalar (custom or not), and false otherwise.
 * @throws GraphQLRequestPreparationException
 */
public boolean isScalar() throws GraphQLRequestPreparationException {
    if (scalar == null) {
        // The scalar value has not yet been calculated.
        // All the generated classes have a GraphQL annotation.
        // If no such annotation, then this type is a scalar.
        GraphQLInputType graphQLInputType = clazz.getAnnotation(GraphQLInputType.class);
        GraphQLInterfaceType graphQLInterfaceType = clazz.getAnnotation(GraphQLInterfaceType.class);
        GraphQLObjectType graphQLObjectType = clazz.getAnnotation(GraphQLObjectType.class);
        GraphQLQuery graphQLQuery = clazz.getAnnotation(GraphQLQuery.class);
        GraphQLUnionType graphQLUnionType = clazz.getAnnotation(GraphQLUnionType.class);
        // If one of these annotations is not null, then it's not a scalar. Otherwise, this type is a scalar.
        scalar = !(graphQLInputType != null || graphQLInterfaceType != null || graphQLObjectType != null || graphQLQuery != null || graphQLUnionType != null);
    }
    return scalar;
}
Also used : GraphQLInputType(com.graphql_java_generator.annotation.GraphQLInputType) GraphQLUnionType(com.graphql_java_generator.annotation.GraphQLUnionType) GraphQLObjectType(com.graphql_java_generator.annotation.GraphQLObjectType) GraphQLInterfaceType(com.graphql_java_generator.annotation.GraphQLInterfaceType) GraphQLQuery(com.graphql_java_generator.annotation.GraphQLQuery)

Example 2 with GraphQLQuery

use of com.graphql_java_generator.annotation.GraphQLQuery 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

GraphQLInputType (com.graphql_java_generator.annotation.GraphQLInputType)2 GraphQLInterfaceType (com.graphql_java_generator.annotation.GraphQLInterfaceType)2 GraphQLObjectType (com.graphql_java_generator.annotation.GraphQLObjectType)2 GraphQLQuery (com.graphql_java_generator.annotation.GraphQLQuery)2 GraphQLUnionType (com.graphql_java_generator.annotation.GraphQLUnionType)2 JsonSubTypes (com.fasterxml.jackson.annotation.JsonSubTypes)1 JsonTypeInfo (com.fasterxml.jackson.annotation.JsonTypeInfo)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