Search in sources :

Example 1 with ScalarType

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

the class DocumentParser method initScalarTypes.

/**
 * This method initializes the {@link #scalarTypes} list. This list depends on the use case
 *
 * @param IDclass
 */
protected void initScalarTypes(Class<?> IDclass) {
    scalarTypes.add(new ScalarType("Boolean", "java.lang", "Boolean", configuration, this));
    // GraphQL Float is a double precision number
    scalarTypes.add(new ScalarType("Float", "java.lang", "Double", configuration, this));
    // By default, we use the UUID type for the ID GraphQL type
    scalarTypes.add(new ScalarType("ID", IDclass.getPackage().getName(), IDclass.getSimpleName(), configuration, this));
    scalarTypes.add(new ScalarType("Int", "java.lang", "Integer", configuration, this));
    scalarTypes.add(new ScalarType("String", "java.lang", "String", configuration, this));
}
Also used : ScalarType(com.graphql_java_generator.plugin.language.impl.ScalarType) CustomScalarType(com.graphql_java_generator.plugin.language.impl.CustomScalarType)

Example 2 with ScalarType

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

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

the class DocumentParserTest method test_initDataFetcherForOneObject.

@Test
public void test_initDataFetcherForOneObject() {
    // Preparation
    documentParser.setTypes(new HashMap<>());
    documentParser.getTypes().put("Object1", new ObjectType("Object1", pluginConfiguration, documentParser));
    documentParser.getTypes().put("GraphQLScalar", new ScalarType("GraphQLScalar", "packageName", "classSimpleName", pluginConfiguration, documentParser));
    documentParser.getTypes().put("Interface0", new InterfaceType("Interface0", pluginConfiguration, documentParser));
    documentParser.getTypes().put("Enum0", new EnumType("Enum0", pluginConfiguration, documentParser));
    documentParser.getTypes().put("Object2", new ObjectType("Object2", pluginConfiguration, documentParser));
    documentParser.setObjectTypes(new ArrayList<>());
    documentParser.getObjectTypes().add((ObjectType) documentParser.getType("Object1"));
    documentParser.getObjectTypes().add((ObjectType) documentParser.getType("Object2"));
    // 
    documentParser.getScalarTypes().add((ScalarType) documentParser.getType("GraphQLScalar"));
    // 
    documentParser.setInterfaceTypes(new ArrayList<>());
    documentParser.getInterfaceTypes().add((InterfaceType) documentParser.getType("Interface0"));
    // 
    documentParser.setEnumTypes(new ArrayList<>());
    documentParser.getEnumTypes().add((EnumType) documentParser.getType("Enum0"));
    ObjectType type = new ObjectType("NameOfTheType", pluginConfiguration, documentParser);
    String[] fields = { "Object1", "GraphQLScalar", "Interface0", "Enum0", "Object2" };
    for (int i = 0; i < 5; i += 1) {
        // When the field is a list, there is two fieldTypeAST: the list, then the real type
        FieldTypeAST fieldTypeAST;
        if ((i % 2) == 0) {
            FieldTypeAST realType = new FieldTypeAST(documentParser.getType(fields[i]).getName());
            fieldTypeAST = FieldTypeAST.builder().listDepth(1).listItemFieldTypeAST(realType).build();
        } else {
            fieldTypeAST = FieldTypeAST.builder().listDepth(0).graphQLTypeSimpleName(documentParser.getType(fields[i]).getName()).build();
        }
        FieldImpl f = FieldImpl.builder().documentParser(documentParser).name("field" + i).owningType(type).fieldTypeAST(fieldTypeAST).build();
        type.getFields().add(f);
        // Let's create its argument list
        List<Field> args = new ArrayList<>();
        for (int j = 0; j < i; j += 1) {
            // means: the first field has
            // When the field is a list, there is two fieldTypeAST: the list, then the real type
            FieldTypeAST argTypeAST;
            if ((j % 2) == 0) {
                FieldTypeAST realType = new FieldTypeAST(documentParser.getType(fields[i]).getName());
                argTypeAST = FieldTypeAST.builder().listDepth(1).listItemFieldTypeAST(realType).build();
            } else {
                argTypeAST = FieldTypeAST.builder().listDepth(0).graphQLTypeSimpleName(documentParser.getType(fields[i]).getName()).build();
            }
            FieldImpl arg = FieldImpl.builder().documentParser(documentParser).name("arg" + j).fieldTypeAST(argTypeAST).build();
            args.add(arg);
        }
        f.setInputParameters(args);
    }
    // /////////////////////////////////////////////////////////////////////////////
    // //////////////////// TEST FOR QUERY TYPES
    // /////////////////////////////////////////////////////////////////////////////
    documentParser.setQueryType(type);
    documentParser.setInterfaceTypes(new ArrayList<>());
    documentParser.setObjectTypes(new ArrayList<>());
    documentParser.dataFetchers = new ArrayList<>();
    documentParser.dataFetchersDelegates = new ArrayList<>();
    documentParser.getEnumTypes().add(new EnumType("AnEnumType", pluginConfiguration, documentParser));
    documentParser.getScalarTypes().add(new ScalarType("Float", "java.lang", "Float", pluginConfiguration, documentParser));
    documentParser.getScalarTypes().add((ScalarType) documentParser.getType("GraphQLScalar"));
    type.setRequestType("AQuery");
    // Go, go, go
    documentParser.initDataFetcherForOneObject(type);
    // Verification
    int i = 0;
    assertEquals(5, documentParser.dataFetchers.size(), "size");
    // 
    // For query types, there must be a Data Fetcher for each field.
    checkDataFetcher(documentParser.dataFetchers.get(i), "field0", 1, type, null, type.getFields().get(i++).getInputParameters());
    checkDataFetcher(documentParser.dataFetchers.get(i), "field1", 0, type, null, type.getFields().get(i++).getInputParameters());
    checkDataFetcher(documentParser.dataFetchers.get(i), "field2", 1, type, null, type.getFields().get(i++).getInputParameters());
    checkDataFetcher(documentParser.dataFetchers.get(i), "field3", 0, type, null, type.getFields().get(i++).getInputParameters());
    checkDataFetcher(documentParser.dataFetchers.get(i), "field4", 1, type, null, type.getFields().get(i++).getInputParameters());
    // 
    // There should be one DataFetchersDelegate, as we have only one type.
    assertEquals(1, documentParser.dataFetchersDelegates.size(), "nb DataFetchersDelegates");
    assertEquals(5, documentParser.dataFetchersDelegates.get(0).getDataFetchers().size(), "nb DataFetchers in the DataFetchersDelegate");
    // ///////////////////////////////////////////////////////////////////////////// ::
    // //////////////////// TEST FOR OBJECT TYPES
    // ///////////////////////////////////////////////////////////////////////////// ::
    documentParser.setQueryType(type);
    documentParser.setInterfaceTypes(new ArrayList<>());
    documentParser.setObjectTypes(new ArrayList<>());
    documentParser.setEnumTypes(new ArrayList<>());
    documentParser.setScalarTypes(new ArrayList<>());
    documentParser.dataFetchers = new ArrayList<>();
    documentParser.dataFetchersDelegates = new ArrayList<>();
    // 
    documentParser.getObjectTypes().add(type);
    documentParser.getEnumTypes().add(new EnumType("AnEnumType", pluginConfiguration, documentParser));
    documentParser.getScalarTypes().add(new ScalarType("Float", "java.lang", "Float", pluginConfiguration, documentParser));
    documentParser.getScalarTypes().add((ScalarType) documentParser.getType("GraphQLScalar"));
    documentParser.getEnumTypes().add((EnumType) documentParser.getType("Enum0"));
    documentParser.fillTypesMap();
    type.setRequestType(null);
    // Go, go, go
    documentParser.initDataFetcherForOneObject(type);
    // Verification
    i = 0;
    assertEquals(3, documentParser.dataFetchers.size(), "size");
    // 
    // For non query types, there must be a Data Fetcher only for non GraphQLScalar and non Enum field.
    checkDataFetcher(documentParser.dataFetchers.get(i++), "field0", 1, type, type.getName(), type.getFields().get(0).getInputParameters());
    checkDataFetcher(documentParser.dataFetchers.get(i++), "field2", 1, type, type.getName(), type.getFields().get(2).getInputParameters());
    checkDataFetcher(documentParser.dataFetchers.get(i++), "field4", 1, type, type.getName(), type.getFields().get(4).getInputParameters());
    // 
    // There should be one DataFetchersDelegate, as we have only one type.
    assertEquals(1, documentParser.dataFetchersDelegates.size(), "nb DataFetchersDelegates");
    assertEquals(3, documentParser.dataFetchersDelegates.get(0).getDataFetchers().size(), "nb DataFetchers in the DataFetchersDelegate");
    // ///////////////////////////////////////////////////////////////////////////// ::
    // //////////////////// TEST FOR INTERFACE TYPES
    // ///////////////////////////////////////////////////////////////////////////// ::
    documentParser.setQueryType(null);
    documentParser.setInterfaceTypes(new ArrayList<>());
    documentParser.setObjectTypes(new ArrayList<>());
    documentParser.setEnumTypes(new ArrayList<>());
    documentParser.setScalarTypes(new ArrayList<>());
    documentParser.dataFetchers = new ArrayList<>();
    documentParser.dataFetchersDelegates = new ArrayList<>();
    // 
    documentParser.getInterfaceTypes().add(new InterfaceType("AnInterface", pluginConfiguration, documentParser));
    documentParser.getEnumTypes().add(new EnumType("AnEnumType", pluginConfiguration, documentParser));
    documentParser.getScalarTypes().add(new ScalarType("Float", "java.lang", "Float", pluginConfiguration, documentParser));
    type.setRequestType(null);
    // Go, go, go
    documentParser.initDataFetcherForOneObject(type);
    // Verification
    i = 0;
    assertEquals(3, documentParser.dataFetchers.size(), "size");
    // 
    // For non query types, there must be a Data Fetcher only for non GraphQLScalar and non Enum field.
    checkDataFetcher(documentParser.dataFetchers.get(i++), "field0", 1, type, type.getName(), type.getFields().get(0).getInputParameters());
    checkDataFetcher(documentParser.dataFetchers.get(i++), "field2", 1, type, type.getName(), type.getFields().get(2).getInputParameters());
    checkDataFetcher(documentParser.dataFetchers.get(i++), "field4", 1, type, type.getName(), type.getFields().get(4).getInputParameters());
    // 
    // There should be one DataFetchersDelegate, as we have only one type.
    assertEquals(1, documentParser.dataFetchersDelegates.size(), "nb DataFetchersDelegates");
    assertEquals(3, documentParser.dataFetchersDelegates.get(0).getDataFetchers().size(), "nb DataFetchers in the DataFetchersDelegate");
}
Also used : FieldTypeAST(com.graphql_java_generator.plugin.language.FieldTypeAST) ScalarType(com.graphql_java_generator.plugin.language.impl.ScalarType) ArrayList(java.util.ArrayList) FieldImpl(com.graphql_java_generator.plugin.language.impl.FieldImpl) ObjectType(com.graphql_java_generator.plugin.language.impl.ObjectType) Field(com.graphql_java_generator.plugin.language.Field) InterfaceType(com.graphql_java_generator.plugin.language.impl.InterfaceType) EnumType(com.graphql_java_generator.plugin.language.impl.EnumType) Test(org.junit.jupiter.api.Test)

Aggregations

ScalarType (com.graphql_java_generator.plugin.language.impl.ScalarType)3 CustomScalarType (com.graphql_java_generator.plugin.language.impl.CustomScalarType)2 Field (com.graphql_java_generator.plugin.language.Field)1 FieldTypeAST (com.graphql_java_generator.plugin.language.FieldTypeAST)1 EnumType (com.graphql_java_generator.plugin.language.impl.EnumType)1 FieldImpl (com.graphql_java_generator.plugin.language.impl.FieldImpl)1 InterfaceType (com.graphql_java_generator.plugin.language.impl.InterfaceType)1 ObjectType (com.graphql_java_generator.plugin.language.impl.ObjectType)1 ScalarExtensionType (com.graphql_java_generator.plugin.language.impl.ScalarExtensionType)1 ArrayList (java.util.ArrayList)1 Test (org.junit.jupiter.api.Test)1