Search in sources :

Example 1 with GraphQLInputParameters

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

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

the class InputParameter method parseInputParameterValue.

/**
 * Parse a value read for an input parameter, within the query
 *
 * @param owningClass
 * @param fieldName
 * @param parameterName
 * @param parameterValue
 * @return
 * @throws GraphQLRequestPreparationException
 */
private static Object parseInputParameterValue(String schema, Class<?> owningClass, String fieldName, String parameterName, String parameterValue) throws GraphQLRequestPreparationException {
    Field field = graphqlUtils.getDeclaredField(owningClass, graphqlUtils.getJavaName(fieldName), true);
    GraphQLInputParameters graphQLInputParameters = field.getDeclaredAnnotation(GraphQLInputParameters.class);
    if (graphQLInputParameters == null) {
        throw new GraphQLRequestPreparationException("[Internal error] The field '" + fieldName + "' is lacking the GraphQLInputParameters annotation");
    }
    for (int i = 0; i < graphQLInputParameters.names().length; i += 1) {
        if (graphQLInputParameters.names()[i].equals(parameterName)) {
            // We've found the parameterType. Let's get its value.
            try {
                return parseValueForInputParameter(schema, parameterValue, graphQLInputParameters.types()[i], owningClass.getPackage().getName());
            } catch (Exception e) {
                throw new GraphQLRequestPreparationException("Could not read the value for the parameter '" + parameterName + "' of the field '" + fieldName + "' of the type '" + owningClass.getName() + "'", e);
            }
        }
    }
    // Too bad...
    throw new GraphQLRequestPreparationException("[Internal error] Can't find the type for the parameter '" + parameterName + "' of the field '" + fieldName + "'");
}
Also used : Field(java.lang.reflect.Field) GraphQLInputParameters(com.graphql_java_generator.annotation.GraphQLInputParameters) GraphQLRequestPreparationException(com.graphql_java_generator.exception.GraphQLRequestPreparationException) GraphQLRequestExecutionException(com.graphql_java_generator.exception.GraphQLRequestExecutionException) GraphQLRequestPreparationException(com.graphql_java_generator.exception.GraphQLRequestPreparationException)

Aggregations

GraphQLInputParameters (com.graphql_java_generator.annotation.GraphQLInputParameters)2 JsonProperty (com.fasterxml.jackson.annotation.JsonProperty)1 JsonDeserialize (com.fasterxml.jackson.databind.annotation.JsonDeserialize)1 JsonSerialize (com.fasterxml.jackson.databind.annotation.JsonSerialize)1 GraphQLField (com.graphql_java_generator.GraphQLField)1 GraphQLRequestExecutionException (com.graphql_java_generator.exception.GraphQLRequestExecutionException)1 GraphQLRequestPreparationException (com.graphql_java_generator.exception.GraphQLRequestPreparationException)1 Field (com.graphql_java_generator.plugin.language.Field)1 Field (java.lang.reflect.Field)1