use of com.graphql_java_generator.plugin.conf.GenerateCodeCommonConfiguration in project graphql-maven-plugin-project by graphql-java-generator.
the class GenerateCodeJsonSchemaPersonalization method applySchemaPersonalization.
/**
* This is the 'main' method for this class: it loads the schema personalization from the json user file, and update
* what the {@link GenerateCodeDocumentParser} has already loaded according to the user's needs.
*/
public void applySchemaPersonalization() {
try {
if (!(configuration instanceof GenerateCodeCommonConfiguration) || !((GenerateCodeCommonConfiguration) configuration).getMode().equals(PluginMode.server)) {
logger.debug("The plugin configuration is not in server mode: no schema personalization is to be applied");
} else {
// First step: we load the schema personalization
if (getSchemaPersonalization() != null) {
// Then, we apply what has been loaded from the json file
for (EntityPersonalization objectPers : schemaPersonalization.getEntityPersonalizations()) {
ObjectType objectType = findObjectTypeFromName(objectPers.getName());
// Should we add an annotation ?
if (objectPers.getAddAnnotation() != null) {
objectType.addAnnotation(objectPers.getAddAnnotation());
}
// Should we replace the annotation ?
if (objectPers.getReplaceAnnotation() != null) {
objectType.addAnnotation(objectPers.getReplaceAnnotation(), true);
}
// Let's add all new fields
for (com.graphql_java_generator.plugin.schema_personalization.Field field : objectPers.getNewFields()) {
// There must not be any field of that name in that object
if (checkIfFieldExists(objectType, field.getName())) {
throw new RuntimeException("The object " + objectType.getName() + " already has a field of name " + field.getName());
}
// Ok, we can add this new field
FieldImpl newField;
if (field.getList() != null && field.getList()) {
// The new field is a list
FieldTypeAST listItem = FieldTypeAST.builder().graphQLTypeSimpleName(field.getType()).build();
FieldTypeAST list = FieldTypeAST.builder().listDepth(1).listItemFieldTypeAST(listItem).mandatory(field.getMandatory()).build();
newField = FieldImpl.builder().documentParser(documentParser).name(field.getName()).owningType(objectType).fieldTypeAST(list).build();
} else {
// The new field is not a list
newField = FieldImpl.builder().documentParser(documentParser).name(field.getName()).id(field.getId()).owningType(objectType).fieldTypeAST(FieldTypeAST.builder().graphQLTypeSimpleName(field.getType()).mandatory(field.getMandatory()).build()).build();
}
if (field.getAddAnnotation() != null) {
newField.addAnnotation(field.getAddAnnotation());
}
if (field.getReplaceAnnotation() != null) {
// We replace the annotation, even if there was an addAnnotation in the json file
newField.addAnnotation(field.getReplaceAnnotation(), true);
}
objectType.getFields().add(newField);
}
// Let's add personalize existing fields
for (com.graphql_java_generator.plugin.schema_personalization.Field field : objectPers.getFieldPersonalizations()) {
// Ok, we can add the field to personalize. This will throw an exception if not found
FieldImpl existingField = (FieldImpl) findFieldFromName(objectType, field.getName());
existingField.setName(field.getName());
if (field.getList() != null && (field.getList() != (existingField.getFieldTypeAST().getListDepth() > 0))) {
// The list attribute changed
if (field.getList()) {
// It's now a list (and it wasn't before)
FieldTypeAST list = FieldTypeAST.builder().listDepth(1).listItemFieldTypeAST(existingField.getFieldTypeAST()).build();
existingField.setFieldTypeAST(list);
} else {
// It's no more a list
existingField.setFieldTypeAST(existingField.getFieldTypeAST().getListItemFieldTypeAST());
}
}
if (field.getType() != null) {
existingField.getFieldTypeAST().setGraphQLTypeSimpleName(field.getType());
}
if (field.getId() != null) {
existingField.setId(field.getId());
}
if (field.getMandatory() != null) {
existingField.getFieldTypeAST().setMandatory(field.getMandatory());
}
if (field.getAddAnnotation() != null) {
existingField.addAnnotation(field.getAddAnnotation());
}
if (field.getReplaceAnnotation() != null) {
// We replace the annotation, even if there was an addAnnotation in the json file
existingField.addAnnotation(field.getReplaceAnnotation(), true);
}
}
// for personalize existing fields
}
}
}
} catch (IOException | URISyntaxException e) {
throw new RuntimeException("Can't apply schema personalization, due to: " + e.getMessage(), e);
}
}
use of com.graphql_java_generator.plugin.conf.GenerateCodeCommonConfiguration in project graphql-maven-plugin-project by graphql-java-generator.
the class AddRelayConnectionsTest method checkEdgeInterface.
@SuppressWarnings("unused")
private void checkEdgeInterface() {
final String EDGE = "Edge";
boolean found = false;
for (InterfaceType d : documentParser.getInterfaceTypes()) {
if (EDGE.equals(d.getName())) {
// The interface should exist only once, so we may not already have found it.
if (found) {
fail("There are two interfaces with '" + EDGE + "' as a name");
}
// We've found it.
found = true;
// Let's check its properties
assertEquals(0, d.getMemberOfUnions().size(), "No unions");
assertEquals(2, d.getFields().size(), "Two fields");
// checkField(type, j, name, list, mandatory, itemMandatory, typeName, classname, nbParameters)
int j = 0;
checkField(d, j++, "cursor", 0, true, false, "String", "String", 0);
checkField(d, j++, "node", 0, false, false, "Node", ((GenerateCodeCommonConfiguration) configuration).getPackageName() + ".Node", 0);
assertEquals(null, d.getRequestType(), "not a query/mutation/subscription");
assertEquals(false, d.isInputType(), "Not an input type");
}
}
if (!found) {
fail("The interface " + EDGE + " has not been found");
}
}
use of com.graphql_java_generator.plugin.conf.GenerateCodeCommonConfiguration in project graphql-maven-plugin-project by graphql-java-generator.
the class ResourceSchemaStringProvider method schemas.
public List<org.springframework.core.io.Resource> schemas() throws IOException {
String fullPathPattern;
if (configuration.getSchemaFilePattern().startsWith("classpath:")) {
// We take the file pattern as is
fullPathPattern = configuration.getSchemaFilePattern();
} else {
if (logger.isDebugEnabled()) {
logger.debug("Before getCanonicalPath(" + configuration.getSchemaFileFolder() + ")");
configuration.getSchemaFileFolder().getCanonicalPath();
}
fullPathPattern = "file:///" + configuration.getSchemaFileFolder().getCanonicalPath() + ((configuration.getSchemaFilePattern().startsWith("/") || (configuration.getSchemaFilePattern().startsWith("\\"))) ? "" : "/") + configuration.getSchemaFilePattern();
}
// Let's look for the GraphQL schema files
List<org.springframework.core.io.Resource> ret = new ArrayList<>(Arrays.asList(applicationContext.getResources(fullPathPattern)));
// A little debug may be useful
if (logger.isDebugEnabled()) {
if (ret.size() == 0) {
logger.debug("No GraphQL schema file found (with this fullPathPattern: '" + fullPathPattern + "'");
} else {
logger.debug("The GraphQL schema files found (with this fullPathPattern: '" + fullPathPattern + "') are: ");
for (Resource schema : ret) {
logger.debug(" * " + schema.getURI().toString());
}
}
}
// We musts have found at least one schema
if (ret.size() == 0) {
throw new RuntimeException("No GraphQL schema found (the searched file pattern is: '" + configuration.getSchemaFilePattern() + "', and search folder is '" + configuration.getSchemaFileFolder().getCanonicalPath() + "')");
}
// In client mode, we need to read the introspection schema
if (configuration instanceof GenerateCodeCommonConfiguration && ((GenerateCodeCommonConfiguration) configuration).getMode().equals(PluginMode.client)) {
org.springframework.core.io.Resource introspection = applicationContext.getResource(INTROSPECTION_SCHEMA);
if (!introspection.exists()) {
throw new IOException("The introspection GraphQL schema doesn't exist (" + INTROSPECTION_SCHEMA + ")");
}
ret.add(introspection);
}
return ret;
}
use of com.graphql_java_generator.plugin.conf.GenerateCodeCommonConfiguration in project graphql-maven-plugin-project by graphql-java-generator.
the class AddRelayConnectionsTest method checkConnectionInterface.
@SuppressWarnings("unused")
private void checkConnectionInterface() {
final String CONNECTION = "Connection";
boolean found = false;
for (InterfaceType d : documentParser.getInterfaceTypes()) {
if (CONNECTION.equals(d.getName())) {
// The interface should exist only once, so we may not already have found it.
if (found) {
fail("There are two interfaces with '" + CONNECTION + "' as a name");
}
// We've found it.
found = true;
// Let's check its properties
assertEquals(0, d.getMemberOfUnions().size(), "No unions");
assertEquals(2, d.getFields().size(), "Two fields");
// checkField(type, j, name, list, mandatory, itemMandatory, typeName, classname, nbParameters)
int j = 0;
checkField(d, j++, "edges", 1, false, false, "Edge", ((GenerateCodeCommonConfiguration) configuration).getPackageName() + ".Edge", 0);
checkField(d, j++, "pageInfo", 0, true, false, "PageInfo", ((GenerateCodeCommonConfiguration) configuration).getPackageName() + ".PageInfo", 0);
assertEquals(null, d.getRequestType(), "not a query/mutation/subscription");
assertEquals(false, d.isInputType(), "Not an input type");
}
}
if (!found) {
fail("The interface " + CONNECTION + " has not been found");
}
}
Aggregations