Search in sources :

Example 1 with CustomScalarType

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

the class DocumentParser method readScalarExtensionType.

/**
 * Reads a GraphQL Custom Scalar, from its definition. This method checks that the CustomScalar has already been
 * defined, in the plugin configuration.
 *
 * @param node
 *            The {@link CustomScalarType} that represents this Custom Scalar
 * @return
 */
ScalarExtensionType readScalarExtensionType(ScalarTypeExtensionDefinition node) {
    String name = node.getName();
    // The current node is an extension of a GraphQL scalar. We must find the entry in the scalar list, to replace
    // it by the scalar extension definition
    boolean found = false;
    ScalarType scalarType = null;
    for (ScalarType t : scalarTypes) {
        if (t.getName().equals(name)) {
            found = true;
            scalarType = t;
        }
    }
    // for
    if (!found) {
        throw new RuntimeException("[Internal error] The '" + name + "' scalar definition was not properly initialized");
    }
    ScalarExtensionType scalarExtensionType = new ScalarExtensionType(name, scalarType.getPackageName(), scalarType.getClassSimpleName(), configuration, this);
    scalarExtensionType.setAppliedDirectives(readAppliedDirectives(node.getDirectives()));
    scalarExtensionType.setComments(node.getComments());
    // We replace the definition for the original GraphQL scalar by this one
    scalarTypes.remove(scalarType);
    scalarTypes.add(scalarExtensionType);
    return scalarExtensionType;
}
Also used : ScalarType(com.graphql_java_generator.plugin.language.impl.ScalarType) CustomScalarType(com.graphql_java_generator.plugin.language.impl.CustomScalarType) ScalarExtensionType(com.graphql_java_generator.plugin.language.impl.ScalarExtensionType)

Example 2 with CustomScalarType

use of com.graphql_java_generator.plugin.language.impl.CustomScalarType 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 3 with CustomScalarType

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

the class GenerateGraphQLSchemaDocumentParser method getCustomScalarType.

/**
 * This class doesn't need an implementation for the Custom Scalars. So a dummy one is returned. {@inheritDoc}
 */
@Override
protected CustomScalarType getCustomScalarType(String name) {
    // If this custom scalar has already been added to the list, let's return it
    for (CustomScalarType customScalarType : customScalars) {
        if (customScalarType.getName().equals(name)) {
            return customScalarType;
        }
    }
    // The custom scalar has not been added to the list yet, let's add it first.
    CustomScalarDefinition customScalarDefinition = new CustomScalarDefinition(name, "java.lang.String", "com.graphql_java_generator.customscalars.GraphQLScalarTypeString", null, null);
    CustomScalarType customScalarType = new CustomScalarType(customScalarDefinition, configuration, this);
    customScalars.add(customScalarType);
    types.put(customScalarType.getName(), customScalarType);
    return customScalarType;
}
Also used : CustomScalarDefinition(com.graphql_java_generator.plugin.conf.CustomScalarDefinition) CustomScalarType(com.graphql_java_generator.plugin.language.impl.CustomScalarType)

Example 4 with CustomScalarType

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

the class DocumentParser method readCustomScalarType.

/**
 * Reads a GraphQL Custom Scalar, from its definition. This method checks that the CustomScalar has already been
 * defined, in the plugin configuration.
 *
 * @param node
 *            The {@link CustomScalarType} that represents this Custom Scalar
 * @return
 */
CustomScalarType readCustomScalarType(ScalarTypeDefinition node) {
    String name = node.getName();
    CustomScalarType customScalarType = getCustomScalarType(name);
    customScalarType.setAppliedDirectives(readAppliedDirectives(node.getDirectives()));
    // Let's store its comments
    customScalarType.setComments(node.getComments());
    return customScalarType;
}
Also used : CustomScalarType(com.graphql_java_generator.plugin.language.impl.CustomScalarType)

Example 5 with CustomScalarType

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

the class GenerateCodeDocumentParser method initCustomSerializers.

/**
 * This method reads all the input types, to identify all the {@link CustomSerializer} that must be defined.
 */
private void initCustomSerializers() {
    Map<Type, Integer> maxListLevelPerType = new HashMap<>();
    getObjectTypes().stream().filter((o) -> o.isInputType()).flatMap((o) -> o.getFields().stream()).filter((f) -> f.getType() instanceof CustomScalarType).forEach((f) -> {
        // listLevel: 0 for non array GraphQL types, 1 for arrays like [Int], 2 for nested arrays like
        // [[Int]]...
        int listLevel = f.getFieldTypeAST().getListDepth();
        Integer alreadyDefinedListLevel = maxListLevelPerType.get(f.getType());
        if (alreadyDefinedListLevel == null || alreadyDefinedListLevel < listLevel) {
            // The current type is a deeper nested array
            maxListLevelPerType.put(f.getType(), listLevel);
        }
    });
    // We now know the maximum listLevel for each type. We can now define all the necessary custom serializers
    customSerializers = new ArrayList<>();
    for (Type t : maxListLevelPerType.keySet()) {
        // We manage all the list levels for the embedded arrays of this type, as found in the GraphQL schema.
        for (int i = 0; i <= maxListLevelPerType.get(t); i += 1) {
            customSerializers.add(new CustomSerializer(t.getName(), t.getClassFullName(), ((CustomScalar) t).getCustomScalarDefinition(), i));
        }
    }
}
Also used : GraphQLQuery(com.graphql_java_generator.annotation.GraphQLQuery) JsonProperty(com.fasterxml.jackson.annotation.JsonProperty) UnionType(com.graphql_java_generator.plugin.language.impl.UnionType) LoggerFactory(org.slf4j.LoggerFactory) GraphQlType(com.graphql_java_generator.plugin.language.Type.GraphQlType) Autowired(org.springframework.beans.factory.annotation.Autowired) GraphQLInterfaceType(com.graphql_java_generator.annotation.GraphQLInterfaceType) GraphQLInputParameters(com.graphql_java_generator.annotation.GraphQLInputParameters) FieldTypeAST(com.graphql_java_generator.plugin.language.FieldTypeAST) Relation(com.graphql_java_generator.plugin.language.Relation) Map(java.util.Map) RequestType(com.graphql_java_generator.annotation.RequestType) GraphQLInputType(com.graphql_java_generator.annotation.GraphQLInputType) Entity(javax.persistence.Entity) GenerateServerCodeConfiguration(com.graphql_java_generator.plugin.conf.GenerateServerCodeConfiguration) Field(com.graphql_java_generator.plugin.language.Field) GraphQLNonScalar(com.graphql_java_generator.annotation.GraphQLNonScalar) List(java.util.List) Stream(java.util.stream.Stream) BatchLoaderImpl(com.graphql_java_generator.plugin.language.impl.BatchLoaderImpl) GeneratedValue(javax.persistence.GeneratedValue) JsonTypeInfo(com.fasterxml.jackson.annotation.JsonTypeInfo) BatchLoader(com.graphql_java_generator.plugin.language.BatchLoader) PostConstruct(javax.annotation.PostConstruct) RelationType(com.graphql_java_generator.plugin.language.RelationType) GraphQLUnionType(com.graphql_java_generator.annotation.GraphQLUnionType) JsonDeserialize(com.fasterxml.jackson.databind.annotation.JsonDeserialize) AbstractType(com.graphql_java_generator.plugin.language.impl.AbstractType) FieldImpl(com.graphql_java_generator.plugin.language.impl.FieldImpl) DataFetcher(com.graphql_java_generator.plugin.language.DataFetcher) Getter(lombok.Getter) GraphQLScalar(com.graphql_java_generator.annotation.GraphQLScalar) RelationImpl(com.graphql_java_generator.plugin.language.impl.RelationImpl) InterfaceType(com.graphql_java_generator.plugin.language.impl.InterfaceType) HashMap(java.util.HashMap) GraphQLField(com.graphql_java_generator.GraphQLField) Type(com.graphql_java_generator.plugin.language.Type) DataFetchersDelegateImpl(com.graphql_java_generator.plugin.language.impl.DataFetchersDelegateImpl) ScalarType(com.graphql_java_generator.plugin.language.impl.ScalarType) ArrayList(java.util.ArrayList) ObjectType(com.graphql_java_generator.plugin.language.impl.ObjectType) Parser(graphql.parser.Parser) GenerateGraphQLSchemaConfiguration(com.graphql_java_generator.plugin.conf.GenerateGraphQLSchemaConfiguration) JsonSerialize(com.fasterxml.jackson.databind.annotation.JsonSerialize) GenerateCodeJsonSchemaPersonalization(com.graphql_java_generator.plugin.schema_personalization.GenerateCodeJsonSchemaPersonalization) Id(javax.persistence.Id) CustomScalar(com.graphql_java_generator.plugin.language.CustomScalar) EnumType(com.graphql_java_generator.plugin.language.impl.EnumType) Logger(org.slf4j.Logger) JsonSubTypes(com.fasterxml.jackson.annotation.JsonSubTypes) PluginMode(com.graphql_java_generator.plugin.conf.PluginMode) GenerateCodeCommonConfiguration(com.graphql_java_generator.plugin.conf.GenerateCodeCommonConfiguration) IOException(java.io.IOException) Component(org.springframework.stereotype.Component) DataFetcherImpl(com.graphql_java_generator.plugin.language.impl.DataFetcherImpl) DocumentParser(com.graphql_java_generator.plugin.DocumentParser) Transient(javax.persistence.Transient) GraphQLObjectType(com.graphql_java_generator.annotation.GraphQLObjectType) ResourceSchemaStringProvider(com.graphql_java_generator.plugin.ResourceSchemaStringProvider) CustomScalarType(com.graphql_java_generator.plugin.language.impl.CustomScalarType) CustomScalarDefinition(com.graphql_java_generator.plugin.conf.CustomScalarDefinition) DataFetchersDelegate(com.graphql_java_generator.plugin.language.DataFetchersDelegate) 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) HashMap(java.util.HashMap) CustomScalarType(com.graphql_java_generator.plugin.language.impl.CustomScalarType) CustomScalar(com.graphql_java_generator.plugin.language.CustomScalar)

Aggregations

CustomScalarType (com.graphql_java_generator.plugin.language.impl.CustomScalarType)5 CustomScalarDefinition (com.graphql_java_generator.plugin.conf.CustomScalarDefinition)3 GenerateServerCodeConfiguration (com.graphql_java_generator.plugin.conf.GenerateServerCodeConfiguration)2 JsonProperty (com.fasterxml.jackson.annotation.JsonProperty)1 JsonSubTypes (com.fasterxml.jackson.annotation.JsonSubTypes)1 JsonTypeInfo (com.fasterxml.jackson.annotation.JsonTypeInfo)1 JsonDeserialize (com.fasterxml.jackson.databind.annotation.JsonDeserialize)1 JsonSerialize (com.fasterxml.jackson.databind.annotation.JsonSerialize)1 GraphQLField (com.graphql_java_generator.GraphQLField)1 GraphQLInputParameters (com.graphql_java_generator.annotation.GraphQLInputParameters)1 GraphQLInputType (com.graphql_java_generator.annotation.GraphQLInputType)1 GraphQLInterfaceType (com.graphql_java_generator.annotation.GraphQLInterfaceType)1 GraphQLNonScalar (com.graphql_java_generator.annotation.GraphQLNonScalar)1 GraphQLObjectType (com.graphql_java_generator.annotation.GraphQLObjectType)1 GraphQLQuery (com.graphql_java_generator.annotation.GraphQLQuery)1 GraphQLScalar (com.graphql_java_generator.annotation.GraphQLScalar)1 GraphQLUnionType (com.graphql_java_generator.annotation.GraphQLUnionType)1 RequestType (com.graphql_java_generator.annotation.RequestType)1 DocumentParser (com.graphql_java_generator.plugin.DocumentParser)1 ResourceSchemaStringProvider (com.graphql_java_generator.plugin.ResourceSchemaStringProvider)1