Search in sources :

Example 1 with Field

use of com.graphql_java_generator.plugin.language.Field in project graphql-maven-plugin-project by graphql-java-generator.

the class GenerateCodeDocumentParser method addFieldAnnotationForClientMode.

/**
 * This method add the needed annotation(s) to the given field. It should be called when the maven plugin is in
 * client mode. This typically add the Jackson annotation, to allow the desialization of the GraphQL server
 * response.
 *
 * @param field
 */
void addFieldAnnotationForClientMode(FieldImpl field) {
    if (configuration.isGenerateJacksonAnnotations()) {
        field.getOwningType().addImport(configuration.getPackageName(), JsonProperty.class.getName());
        field.addAnnotation("@JsonProperty(\"" + field.getName() + "\")");
    }
    if (configuration.isGenerateJacksonAnnotations()) {
        // No json deserialization for input type
        if (!field.getOwningType().isInputType() && (field.getFieldTypeAST().getListDepth() > 0 || field.getType().isCustomScalar())) {
            // Custom Deserializer (for all lists and custom scalars)
            String classSimpleName = // 
            "CustomJacksonDeserializers." + CustomDeserializer.getCustomDeserializerClassSimpleName(field.getFieldTypeAST().getListDepth(), graphqlUtils.getJavaName(field.getType().getName()));
            field.getOwningType().addImport(configuration.getPackageName(), getUtilPackageName() + ".CustomJacksonDeserializers");
            field.getOwningType().addImport(configuration.getPackageName(), JsonDeserialize.class.getName());
            field.addAnnotation(buildJsonDeserializeAnnotation(null, classSimpleName + ".class"));
        }
        // json serialization is only for input types
        if (field.getOwningType().isInputType() && field.getType().isCustomScalar()) {
            // Custom Serializer (only for custom scalars)
            String classSimpleName = // 
            "CustomJacksonSerializers." + CustomSerializer.getCustomSerializerClassSimpleName(field.getFieldTypeAST().getListDepth(), graphqlUtils.getJavaName(field.getType().getName()));
            field.getOwningType().addImport(configuration.getPackageName(), getUtilPackageName() + ".CustomJacksonSerializers");
            field.getOwningType().addImport(configuration.getPackageName(), JsonSerialize.class.getName());
            field.getOwningType().addImport(configuration.getPackageName(), field.getType().getClassFullName());
            field.addAnnotation(buildJsonSerializeAnnotation(classSimpleName + ".class"));
        }
    }
    if (field.getInputParameters().size() > 0) {
        // Let's add the @GraphQLInputParameters annotation
        field.getOwningType().addImport(configuration.getPackageName(), GraphQLInputParameters.class.getName());
        StringBuilder names = new StringBuilder();
        StringBuilder types = new StringBuilder();
        StringBuilder mandatories = new StringBuilder();
        StringBuilder listDepths = new StringBuilder();
        StringBuilder itemsMandatory = new StringBuilder();
        String separator = "";
        for (Field param : field.getInputParameters()) {
            names.append(separator).append('"').append(param.getName()).append('"');
            types.append(separator).append('"').append(param.getGraphQLTypeSimpleName()).append('"');
            mandatories.append(separator).append(param.getFieldTypeAST().isMandatory());
            listDepths.append(separator).append(param.getFieldTypeAST().getListDepth());
            itemsMandatory.append(separator).append(param.getFieldTypeAST().isItemMandatory());
            separator = ", ";
        }
        field.addAnnotation("@GraphQLInputParameters(names = {" + names + "}, types = {" + types + "}, mandatories = {" + mandatories + "}, listDepths = {" + listDepths + "}, itemsMandatory = {" + itemsMandatory + "})");
    }
    addFieldAnnotationForBothClientAndServerMode(field);
}
Also used : JsonSerialize(com.fasterxml.jackson.databind.annotation.JsonSerialize) Field(com.graphql_java_generator.plugin.language.Field) GraphQLField(com.graphql_java_generator.GraphQLField) JsonProperty(com.fasterxml.jackson.annotation.JsonProperty) GraphQLInputParameters(com.graphql_java_generator.annotation.GraphQLInputParameters) JsonDeserialize(com.fasterxml.jackson.databind.annotation.JsonDeserialize)

Example 2 with Field

use of com.graphql_java_generator.plugin.language.Field in project graphql-maven-plugin-project by graphql-java-generator.

the class GenerateCodeDocumentParser method addImportsForOneType.

/**
 * Add all imports that are needed for this type
 *
 * @param type
 */
private void addImportsForOneType(Type type) {
    if (type != null) {
        // Let's loop through all the fields
        for (Field f : type.getFields()) {
            if (f.getFieldTypeAST().getListDepth() > 0) {
                type.addImportForUtilityClasses(getUtilPackageName(), List.class.getName());
            }
            for (Field param : f.getInputParameters()) {
                if (param.getFieldTypeAST().getListDepth() > 0) {
                    type.addImportForUtilityClasses(getUtilPackageName(), List.class.getName());
                }
            }
        // for(inputParameters)
        }
        // for(Fields)
        // Let's add some common imports
        type.addImportForUtilityClasses(getUtilPackageName(), GraphQLField.class.getName());
        type.addImportForUtilityClasses(getUtilPackageName(), GraphQLInputParameters.class.getName());
        // Some imports that are only for utility classes
        type.addImportForUtilityClasses(getUtilPackageName(), RequestType.class.getName());
        switch(configuration.getMode()) {
            case client:
                if (configuration.isGenerateJacksonAnnotations()) {
                    type.addImportForUtilityClasses(getUtilPackageName(), JsonDeserialize.class.getName());
                    type.addImportForUtilityClasses(getUtilPackageName(), JsonProperty.class.getName());
                }
                break;
            case server:
                break;
            default:
                throw new RuntimeException("unexpected plugin mode: " + configuration.getMode().name());
        }
    }
}
Also used : Field(com.graphql_java_generator.plugin.language.Field) GraphQLField(com.graphql_java_generator.GraphQLField) JsonProperty(com.fasterxml.jackson.annotation.JsonProperty) GraphQLInputParameters(com.graphql_java_generator.annotation.GraphQLInputParameters) List(java.util.List) ArrayList(java.util.ArrayList) JsonDeserialize(com.fasterxml.jackson.databind.annotation.JsonDeserialize) GraphQLField(com.graphql_java_generator.GraphQLField) RequestType(com.graphql_java_generator.annotation.RequestType)

Example 3 with Field

use of com.graphql_java_generator.plugin.language.Field in project graphql-maven-plugin-project by graphql-java-generator.

the class GenerateCodeDocumentParser method initRelations.

/**
 * Reads all the GraphQl objects, interfaces, union... that have been read from the GraphQL schema, and list all the
 * relations between Server objects (that is: all objects out of the Query/Mutation/Subscription types and the input
 * types). The found relations are stored, to be reused during the code generation.<BR/>
 * These relations are important for the server mode of the plugin, to generate the proper JPA annotations.
 */
void initRelations() {
    for (ObjectType type : getObjectTypes()) {
        // We initiate the relations only for regular objects (not query/mutation/subscription)
        if (type.getRequestType() == null) {
            if (!type.isInputType()) {
                for (Field field : type.getFields()) {
                    if (field.getType() instanceof ObjectType) {
                        RelationType relType = field.getFieldTypeAST().getListDepth() > 0 ? RelationType.OneToMany : RelationType.ManyToOne;
                        RelationImpl relation = new RelationImpl(type, field, relType);
                        // 
                        ((FieldImpl) field).setRelation(relation);
                        relations.add(relation);
                    }
                // if (instanceof ObjectType)
                }
            // if (!type.isInputType())
            }
        // for (field)
        }
    // if (type.getRequestType()== null)
    }
// for (type)
}
Also used : RelationImpl(com.graphql_java_generator.plugin.language.impl.RelationImpl) ObjectType(com.graphql_java_generator.plugin.language.impl.ObjectType) GraphQLObjectType(com.graphql_java_generator.annotation.GraphQLObjectType) Field(com.graphql_java_generator.plugin.language.Field) GraphQLField(com.graphql_java_generator.GraphQLField) RelationType(com.graphql_java_generator.plugin.language.RelationType) FieldImpl(com.graphql_java_generator.plugin.language.impl.FieldImpl)

Example 4 with Field

use of com.graphql_java_generator.plugin.language.Field in project graphql-maven-plugin-project by graphql-java-generator.

the class AddRelayConnectionsTest method test_getFieldInheritedFrom_interfaceThatImplementsInterface.

@Test
@Execution(ExecutionMode.CONCURRENT)
void test_getFieldInheritedFrom_interfaceThatImplementsInterface() throws IOException {
    // Preparation
    loadSpringContext(AllGraphQLCasesRelayConnection_Client_SpringConfiguration.class, "test_getFieldInheritedFrom_interfaceThatImplementsInterface", true);
    Field f = getField("AllFieldCasesInterface", "id");
    // Go, go, go
    List<Field> fields = addRelayConnections.getFieldInheritedFrom(f);
    // Verification
    assertEquals(1, fields.size());
    assertEquals("id", fields.get(0).getName());
    assertEquals("WithID", fields.get(0).getOwningType().getName());
}
Also used : Field(com.graphql_java_generator.plugin.language.Field) Execution(org.junit.jupiter.api.parallel.Execution) Test(org.junit.jupiter.api.Test)

Example 5 with Field

use of com.graphql_java_generator.plugin.language.Field in project graphql-maven-plugin-project by graphql-java-generator.

the class ObjectType method toString.

@Override
public String toString() {
    StringBuilder sb = new StringBuilder();
    boolean addSeparator;
    sb.append(getClass().getSimpleName() + " {name:").append(getName());
    sb.append(", fields:{");
    addSeparator = false;
    for (Field f : getFields()) {
        if (addSeparator)
            sb.append(",");
        else
            addSeparator = true;
        sb.append(f.toString());
    }
    sb.append("}");
    if (getImplementz().size() > 0) {
        sb.append(", implements ");
        sb.append(String.join(",", getImplementz()));
    }
    if (getComments() == null) {
        sb.append(", comments=null");
    } else if (getComments().size() > 0) {
        sb.append(", comments=empty");
    } else {
        sb.append(", comments \"");
        sb.append(String.join("\\n", getComments()));
        sb.append("\"");
    }
    return sb.toString();
}
Also used : Field(com.graphql_java_generator.plugin.language.Field)

Aggregations

Field (com.graphql_java_generator.plugin.language.Field)20 ObjectType (com.graphql_java_generator.plugin.language.impl.ObjectType)12 Type (com.graphql_java_generator.plugin.language.Type)9 InterfaceType (com.graphql_java_generator.plugin.language.impl.InterfaceType)8 FieldImpl (com.graphql_java_generator.plugin.language.impl.FieldImpl)5 GraphQLField (com.graphql_java_generator.GraphQLField)4 ObjectField (graphql.language.ObjectField)4 FieldTypeAST (com.graphql_java_generator.plugin.language.FieldTypeAST)3 RelationType (com.graphql_java_generator.plugin.language.RelationType)3 EnumType (com.graphql_java_generator.plugin.language.impl.EnumType)3 ArrayList (java.util.ArrayList)3 Test (org.junit.jupiter.api.Test)3 JsonProperty (com.fasterxml.jackson.annotation.JsonProperty)2 JsonDeserialize (com.fasterxml.jackson.databind.annotation.JsonDeserialize)2 GraphQLInputParameters (com.graphql_java_generator.annotation.GraphQLInputParameters)2 Execution (org.junit.jupiter.api.parallel.Execution)2 JsonSerialize (com.fasterxml.jackson.databind.annotation.JsonSerialize)1 GraphQLObjectType (com.graphql_java_generator.annotation.GraphQLObjectType)1 RequestType (com.graphql_java_generator.annotation.RequestType)1 AppliedDirective (com.graphql_java_generator.plugin.language.AppliedDirective)1