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());
}
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());
}
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;
}
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");
}
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());
}
}
Aggregations