Search in sources :

Example 1 with EnumType

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

the class DocumentParser_allGraphQLCases_Server_Test method test_readEnumType.

@Test
@Execution(ExecutionMode.CONCURRENT)
void test_readEnumType() throws IOException {
    // Preparation
    String objectName = "Episode";
    EnumTypeDefinition def = null;
    for (Definition<?> node : documents.getDocuments().get(0).getDefinitions()) {
        if (node instanceof EnumTypeDefinition && ((EnumTypeDefinition) node).getName().equals(objectName)) {
            def = (EnumTypeDefinition) node;
        }
    }
    // for
    assertNotNull(def, "We should have found our test case (" + objectName + ")");
    // We need to read the directives first
    generateCodeDocumentParser.postConstruct();
    generateCodeDocumentParser.getDocuments().getDocuments().get(0).getDefinitions().stream().filter(n -> (n instanceof DirectiveDefinition)).forEach(node -> generateCodeDocumentParser.getDirectives().add(generateCodeDocumentParser.readDirectiveDefinition((DirectiveDefinition) node)));
    // To be sure to properly find our parsed object type, we empty the documentParser objects list.
    generateCodeDocumentParser.setQueryType(null);
    // Go, go, go
    EnumType type = generateCodeDocumentParser.readEnumType(def);
    // Verification
    assertEquals(objectName, type.getName(), "The name is " + objectName);
    assertEquals(4, type.getValues().size(), "Number of values");
    int i = 0;
    assertEquals("NEWHOPE", type.getValues().get(i++).getName());
    assertEquals("EMPIRE", type.getValues().get(i++).getName());
    assertEquals("JEDI", type.getValues().get(i++).getName());
    assertEquals("DOES_NOT_EXIST", type.getValues().get(i++).getName());
}
Also used : Assertions.fail(org.junit.jupiter.api.Assertions.fail) ObjectValue(graphql.language.ObjectValue) Assertions.assertNotNull(org.junit.jupiter.api.Assertions.assertNotNull) BeforeEach(org.junit.jupiter.api.BeforeEach) DataFetcher(com.graphql_java_generator.plugin.language.DataFetcher) FloatValue(graphql.language.FloatValue) Value(graphql.language.Value) Assertions.assertNull(org.junit.jupiter.api.Assertions.assertNull) InterfaceType(com.graphql_java_generator.plugin.language.impl.InterfaceType) DirectiveDefinition(graphql.language.DirectiveDefinition) Type(com.graphql_java_generator.plugin.language.Type) SchemaDefinition(graphql.language.SchemaDefinition) DataFetchersDelegateImpl(com.graphql_java_generator.plugin.language.impl.DataFetchersDelegateImpl) ArrayList(java.util.ArrayList) EnumTypeDefinition(graphql.language.EnumTypeDefinition) AllGraphQLCases_Server_SpringConfiguration(graphql.mavenplugin_notscannedbyspring.AllGraphQLCases_Server_SpringConfiguration) ObjectType(com.graphql_java_generator.plugin.language.impl.ObjectType) Assertions.assertFalse(org.junit.jupiter.api.Assertions.assertFalse) Definition(graphql.language.Definition) Documents(com.graphql_java_generator.plugin.Documents) BigInteger(java.math.BigInteger) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) EnumValue(com.graphql_java_generator.plugin.language.EnumValue) EnumType(com.graphql_java_generator.plugin.language.impl.EnumType) ObjectTypeDefinition(graphql.language.ObjectTypeDefinition) ExecutionMode(org.junit.jupiter.api.parallel.ExecutionMode) ObjectField(graphql.language.ObjectField) IOException(java.io.IOException) Field(com.graphql_java_generator.plugin.language.Field) UUID(java.util.UUID) AnnotationConfigApplicationContext(org.springframework.context.annotation.AnnotationConfigApplicationContext) Test(org.junit.jupiter.api.Test) List(java.util.List) DataFetcherImpl(com.graphql_java_generator.plugin.language.impl.DataFetcherImpl) AbstractApplicationContext(org.springframework.context.support.AbstractApplicationContext) GraphQLConfiguration(com.graphql_java_generator.plugin.conf.GraphQLConfiguration) StringValue(graphql.language.StringValue) ArrayValue(graphql.language.ArrayValue) IntValue(graphql.language.IntValue) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) Execution(org.junit.jupiter.api.parallel.Execution) DataFetchersDelegate(com.graphql_java_generator.plugin.language.DataFetchersDelegate) BooleanValue(graphql.language.BooleanValue) EnumTypeDefinition(graphql.language.EnumTypeDefinition) EnumType(com.graphql_java_generator.plugin.language.impl.EnumType) DirectiveDefinition(graphql.language.DirectiveDefinition) Execution(org.junit.jupiter.api.parallel.Execution) Test(org.junit.jupiter.api.Test)

Example 2 with EnumType

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

the class DocumentParserTest method test_addTypeAnnotationForServerMode.

@Test
public void test_addTypeAnnotationForServerMode() {
    Type type;
    pluginConfiguration.mode = PluginMode.server;
    type = new ObjectType("TheName", pluginConfiguration, documentParser);
    documentParser.addTypeAnnotationForServerMode(type);
    assertEquals("@Entity\n@GraphQLObjectType(\"TheName\")", type.getAnnotation(), type.getClass().getName());
    type = new InterfaceType("TheName", pluginConfiguration, documentParser);
    documentParser.addTypeAnnotationForServerMode(type);
    assertEquals("@GraphQLInterfaceType(\"TheName\")", type.getAnnotation(), type.getClass().getName());
    type = new EnumType("TheName", pluginConfiguration, documentParser);
    documentParser.addTypeAnnotationForServerMode(type);
    assertEquals("", type.getAnnotation(), type.getClass().getName());
}
Also used : ObjectType(com.graphql_java_generator.plugin.language.impl.ObjectType) EnumType(com.graphql_java_generator.plugin.language.impl.EnumType) 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) InterfaceType(com.graphql_java_generator.plugin.language.impl.InterfaceType) EnumType(com.graphql_java_generator.plugin.language.impl.EnumType) Test(org.junit.jupiter.api.Test)

Example 3 with EnumType

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

the class DocumentParser method readEnumType.

/**
 * Reads an enum definition, and create the relevant {@link EnumType}
 *
 * @param node
 * @return
 */
public EnumType readEnumType(EnumTypeDefinition node) {
    EnumType enumType = new EnumType(node.getName(), configuration, this);
    enumType.setAppliedDirectives(readAppliedDirectives(node.getDirectives()));
    // Let's store its comments
    enumType.setComments(node.getComments());
    for (EnumValueDefinition enumValDef : node.getEnumValueDefinitions()) {
        EnumValue val = EnumValueImpl.builder().name(enumValDef.getName()).appliedDirectives(readAppliedDirectives(enumValDef.getDirectives())).build();
        enumType.getValues().add(val);
    }
    return enumType;
}
Also used : EnumType(com.graphql_java_generator.plugin.language.impl.EnumType) EnumValueDefinition(graphql.language.EnumValueDefinition) EnumValue(com.graphql_java_generator.plugin.language.EnumValue)

Example 4 with EnumType

use of com.graphql_java_generator.plugin.language.impl.EnumType 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)

Example 5 with EnumType

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

the class DocumentParser_allGraphQLCases_Server_Test method checkDirectivesOnEnumValue.

/**
 * Check that a Directive for an object, field, scalar (...) has been properly parsed
 *
 * @param type
 * @param enumValueName
 *            The name of the field, within the given type
 * @param containsTestDirective
 *            true if this type contains the testDirective
 * @param value
 *            Value of the 'value' field of the testDirective
 * @param anotherValue
 *            Value of the 'anotherValue' field of the testDirective
 * @param containsAnotherTestDirective
 *            true if this type contains the anotherTestDirective
 */
private void checkDirectivesOnEnumValue(Type type, String enumValueName, boolean containsTestDirective, String value, String anotherValue, boolean containsAnotherTestDirective) {
    EnumValue enumValue = null;
    for (EnumValue f : ((EnumType) type).getValues()) {
        if (f.getName().equals(enumValueName)) {
            enumValue = f;
            break;
        }
    }
    if (enumValue == null) {
        fail("Could not find the enum value '" + enumValueName + "' on enum '" + type.getName() + "'");
    }
    int nbDirectives = (containsTestDirective ? 1 : 0) + (containsAnotherTestDirective ? 1 : 0);
    assertEquals(nbDirectives, enumValue.getAppliedDirectives().size());
    if (containsTestDirective) {
        assertEquals("testDirective", enumValue.getAppliedDirectives().get(0).getDirective().getName());
        // check arguments
        assertEquals(value, ((StringValue) enumValue.getAppliedDirectives().get(0).getArgumentValues().get("value")).getValue());
        if (anotherValue != null)
            assertEquals(anotherValue, ((StringValue) enumValue.getAppliedDirectives().get(0).getArgumentValues().get("anotherValue")).getValue());
    }
    if (containsAnotherTestDirective) {
        int index = containsTestDirective ? 1 : 0;
        assertEquals("anotherTestDirective", enumValue.getAppliedDirectives().get(index).getDirective().getName());
    }
}
Also used : EnumValue(com.graphql_java_generator.plugin.language.EnumValue) EnumType(com.graphql_java_generator.plugin.language.impl.EnumType) StringValue(graphql.language.StringValue)

Aggregations

EnumType (com.graphql_java_generator.plugin.language.impl.EnumType)5 EnumValue (com.graphql_java_generator.plugin.language.EnumValue)3 InterfaceType (com.graphql_java_generator.plugin.language.impl.InterfaceType)3 ObjectType (com.graphql_java_generator.plugin.language.impl.ObjectType)3 Test (org.junit.jupiter.api.Test)3 Field (com.graphql_java_generator.plugin.language.Field)2 Type (com.graphql_java_generator.plugin.language.Type)2 ScalarType (com.graphql_java_generator.plugin.language.impl.ScalarType)2 StringValue (graphql.language.StringValue)2 ArrayList (java.util.ArrayList)2 Documents (com.graphql_java_generator.plugin.Documents)1 GraphQLConfiguration (com.graphql_java_generator.plugin.conf.GraphQLConfiguration)1 DataFetcher (com.graphql_java_generator.plugin.language.DataFetcher)1 DataFetchersDelegate (com.graphql_java_generator.plugin.language.DataFetchersDelegate)1 FieldTypeAST (com.graphql_java_generator.plugin.language.FieldTypeAST)1 DataFetcherImpl (com.graphql_java_generator.plugin.language.impl.DataFetcherImpl)1 DataFetchersDelegateImpl (com.graphql_java_generator.plugin.language.impl.DataFetchersDelegateImpl)1 FieldImpl (com.graphql_java_generator.plugin.language.impl.FieldImpl)1 ArrayValue (graphql.language.ArrayValue)1 BooleanValue (graphql.language.BooleanValue)1