Search in sources :

Example 1 with GenerateServerCodeConfiguration

use of com.graphql_java_generator.plugin.conf.GenerateServerCodeConfiguration in project graphql-maven-plugin-project by graphql-java-generator.

the class GenerateCodeDocumentParser method initScalarTypes.

/**
 * This method initializes the {@link #scalarTypes} list. This list depends on the use case
 */
@Override
protected void initScalarTypes(Class<?> notUsed) {
    initConfiguration();
    // Let's load the standard Scalar types
    if (configuration.getMode().equals(PluginMode.server)) {
        try {
            super.initScalarTypes(Class.forName(((GenerateServerCodeConfiguration) configuration).getJavaTypeForIDType()));
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e.getMessage(), e);
        }
    } else {
        // In client mode, ID type is managed as a String
        super.initScalarTypes(String.class);
    }
    // ////////////////////////////////////////////////////////////////////////////////////////
    // Add of all GraphQL custom scalar implementations must be provided by the plugin configuration
    logger.debug("Storing custom scalar's implementations [START]");
    if (configuration.getCustomScalars() != null) {
        for (CustomScalarDefinition customScalarDef : configuration.getCustomScalars()) {
            CustomScalarType type = new CustomScalarType(customScalarDef, configuration, this);
            getCustomScalars().add(type);
            getTypes().put(type.getName(), type);
        }
    }
    logger.debug("Storing custom scalar's implementations [END]");
}
Also used : GenerateServerCodeConfiguration(com.graphql_java_generator.plugin.conf.GenerateServerCodeConfiguration) CustomScalarDefinition(com.graphql_java_generator.plugin.conf.CustomScalarDefinition) CustomScalarType(com.graphql_java_generator.plugin.language.impl.CustomScalarType)

Example 2 with GenerateServerCodeConfiguration

use of com.graphql_java_generator.plugin.conf.GenerateServerCodeConfiguration in project graphql-maven-plugin-project by graphql-java-generator.

the class GenerateCodeJsonSchemaPersonalization method loadGraphQLSchemaPersonalization.

/**
 * Let's load the schema personalization from the configuration json file.
 *
 * @return
 *
 * @throws IOException
 * @throws ProcessingException
 *             When the JSON file is not valid
 * @throws URISyntaxException
 *             If we can't process the JSON URIs. It would be an internal error.
 */
public SchemaPersonalization loadGraphQLSchemaPersonalization() throws IOException, URISyntaxException {
    if (((GenerateServerCodeConfiguration) configuration).getSchemaPersonalizationFile() == null) {
        return null;
    } else {
        // Let's check that the JSON is valid
        JsonValidationService service = JsonValidationService.newInstance();
        // Reads the JSON schema
        // 
        // There is an issue in reading the json schema, when the project is built with Gradle 7
        // It seems to be linked to https://issues.apache.org/jira/browse/JOHNZON-147 (solved in Johnzon 1.1.6, in
        // february 2018)
        // A workaround is to first read the schema in a string, then call the readSchema
        BufferedReader br = new BufferedReader(new InputStreamReader(getClass().getResourceAsStream("/" + JSON_SCHEMA_FILENAME), Charset.forName("UTF-8")));
        String text = br.lines().collect(Collectors.joining("\n"));
        // readSchema should directly read the InputStream
        JsonSchema schema = service.readSchema(new StringReader(text));
        // Problem handler which will print problems found
        ProblemHandler handler = service.createProblemPrinter(this::logParsingError);
        // Reads the JSON instance by javax.json.JsonReader
        nbErrors = 0;
        try (JsonReader reader = service.createReader(new FileInputStream(((GenerateServerCodeConfiguration) configuration).getSchemaPersonalizationFile()), schema, handler)) {
            // JsonValue value =
            reader.readValue();
        // Do something useful here
        }
        if (nbErrors > 0) {
            throw new RuntimeException("The json file '" + ((GenerateServerCodeConfiguration) configuration).getSchemaPersonalizationFile().getAbsolutePath() + "' is invalid. See the logs for details");
        }
        // Let's read the flow definition
        logger.info("Loading file " + ((GenerateServerCodeConfiguration) configuration).getSchemaPersonalizationFile().getAbsolutePath());
        ObjectMapper objectMapper = new ObjectMapper();
        SchemaPersonalization ret;
        try (InputStream isFlowJson = new FileInputStream(((GenerateServerCodeConfiguration) configuration).getSchemaPersonalizationFile())) {
            ret = objectMapper.readValue(isFlowJson, SchemaPersonalization.class);
        }
        return ret;
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) JsonSchema(org.leadpony.justify.api.JsonSchema) FileInputStream(java.io.FileInputStream) ProblemHandler(org.leadpony.justify.api.ProblemHandler) JsonValidationService(org.leadpony.justify.api.JsonValidationService) GenerateServerCodeConfiguration(com.graphql_java_generator.plugin.conf.GenerateServerCodeConfiguration) BufferedReader(java.io.BufferedReader) StringReader(java.io.StringReader) JsonReader(javax.json.JsonReader) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Aggregations

GenerateServerCodeConfiguration (com.graphql_java_generator.plugin.conf.GenerateServerCodeConfiguration)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 CustomScalarDefinition (com.graphql_java_generator.plugin.conf.CustomScalarDefinition)1 CustomScalarType (com.graphql_java_generator.plugin.language.impl.CustomScalarType)1 BufferedReader (java.io.BufferedReader)1 FileInputStream (java.io.FileInputStream)1 InputStream (java.io.InputStream)1 InputStreamReader (java.io.InputStreamReader)1 StringReader (java.io.StringReader)1 JsonReader (javax.json.JsonReader)1 JsonSchema (org.leadpony.justify.api.JsonSchema)1 JsonValidationService (org.leadpony.justify.api.JsonValidationService)1 ProblemHandler (org.leadpony.justify.api.ProblemHandler)1