use of com.graphql_java_generator.plugin.language.Field in project graphql-maven-plugin-project by graphql-java-generator.
the class GenerateCodeDocumentParser method addFieldAnnotationForClientMode.
/**
* This method add the needed annotation(s) to the given field. It should be called when the maven plugin is in
* client mode. This typically add the Jackson annotation, to allow the desialization of the GraphQL server
* response.
*
* @param field
*/
void addFieldAnnotationForClientMode(FieldImpl field) {
if (configuration.isGenerateJacksonAnnotations()) {
field.getOwningType().addImport(configuration.getPackageName(), JsonProperty.class.getName());
field.addAnnotation("@JsonProperty(\"" + field.getName() + "\")");
}
if (configuration.isGenerateJacksonAnnotations()) {
// No json deserialization for input type
if (!field.getOwningType().isInputType() && (field.getFieldTypeAST().getListDepth() > 0 || field.getType().isCustomScalar())) {
// Custom Deserializer (for all lists and custom scalars)
String classSimpleName = //
"CustomJacksonDeserializers." + CustomDeserializer.getCustomDeserializerClassSimpleName(field.getFieldTypeAST().getListDepth(), graphqlUtils.getJavaName(field.getType().getName()));
field.getOwningType().addImport(configuration.getPackageName(), getUtilPackageName() + ".CustomJacksonDeserializers");
field.getOwningType().addImport(configuration.getPackageName(), JsonDeserialize.class.getName());
field.addAnnotation(buildJsonDeserializeAnnotation(null, classSimpleName + ".class"));
}
// json serialization is only for input types
if (field.getOwningType().isInputType() && field.getType().isCustomScalar()) {
// Custom Serializer (only for custom scalars)
String classSimpleName = //
"CustomJacksonSerializers." + CustomSerializer.getCustomSerializerClassSimpleName(field.getFieldTypeAST().getListDepth(), graphqlUtils.getJavaName(field.getType().getName()));
field.getOwningType().addImport(configuration.getPackageName(), getUtilPackageName() + ".CustomJacksonSerializers");
field.getOwningType().addImport(configuration.getPackageName(), JsonSerialize.class.getName());
field.getOwningType().addImport(configuration.getPackageName(), field.getType().getClassFullName());
field.addAnnotation(buildJsonSerializeAnnotation(classSimpleName + ".class"));
}
}
if (field.getInputParameters().size() > 0) {
// Let's add the @GraphQLInputParameters annotation
field.getOwningType().addImport(configuration.getPackageName(), GraphQLInputParameters.class.getName());
StringBuilder names = new StringBuilder();
StringBuilder types = new StringBuilder();
StringBuilder mandatories = new StringBuilder();
StringBuilder listDepths = new StringBuilder();
StringBuilder itemsMandatory = new StringBuilder();
String separator = "";
for (Field param : field.getInputParameters()) {
names.append(separator).append('"').append(param.getName()).append('"');
types.append(separator).append('"').append(param.getGraphQLTypeSimpleName()).append('"');
mandatories.append(separator).append(param.getFieldTypeAST().isMandatory());
listDepths.append(separator).append(param.getFieldTypeAST().getListDepth());
itemsMandatory.append(separator).append(param.getFieldTypeAST().isItemMandatory());
separator = ", ";
}
field.addAnnotation("@GraphQLInputParameters(names = {" + names + "}, types = {" + types + "}, mandatories = {" + mandatories + "}, listDepths = {" + listDepths + "}, itemsMandatory = {" + itemsMandatory + "})");
}
addFieldAnnotationForBothClientAndServerMode(field);
}
use of com.graphql_java_generator.plugin.language.Field in project graphql-maven-plugin-project by graphql-java-generator.
the class GenerateCodeDocumentParser method addImportsForOneType.
/**
* Add all imports that are needed for this type
*
* @param type
*/
private void addImportsForOneType(Type type) {
if (type != null) {
// Let's loop through all the fields
for (Field f : type.getFields()) {
if (f.getFieldTypeAST().getListDepth() > 0) {
type.addImportForUtilityClasses(getUtilPackageName(), List.class.getName());
}
for (Field param : f.getInputParameters()) {
if (param.getFieldTypeAST().getListDepth() > 0) {
type.addImportForUtilityClasses(getUtilPackageName(), List.class.getName());
}
}
// for(inputParameters)
}
// for(Fields)
// Let's add some common imports
type.addImportForUtilityClasses(getUtilPackageName(), GraphQLField.class.getName());
type.addImportForUtilityClasses(getUtilPackageName(), GraphQLInputParameters.class.getName());
// Some imports that are only for utility classes
type.addImportForUtilityClasses(getUtilPackageName(), RequestType.class.getName());
switch(configuration.getMode()) {
case client:
if (configuration.isGenerateJacksonAnnotations()) {
type.addImportForUtilityClasses(getUtilPackageName(), JsonDeserialize.class.getName());
type.addImportForUtilityClasses(getUtilPackageName(), JsonProperty.class.getName());
}
break;
case server:
break;
default:
throw new RuntimeException("unexpected plugin mode: " + configuration.getMode().name());
}
}
}
use of com.graphql_java_generator.plugin.language.Field in project graphql-maven-plugin-project by graphql-java-generator.
the class GenerateCodeDocumentParser method initRelations.
/**
* Reads all the GraphQl objects, interfaces, union... that have been read from the GraphQL schema, and list all the
* relations between Server objects (that is: all objects out of the Query/Mutation/Subscription types and the input
* types). The found relations are stored, to be reused during the code generation.<BR/>
* These relations are important for the server mode of the plugin, to generate the proper JPA annotations.
*/
void initRelations() {
for (ObjectType type : getObjectTypes()) {
// We initiate the relations only for regular objects (not query/mutation/subscription)
if (type.getRequestType() == null) {
if (!type.isInputType()) {
for (Field field : type.getFields()) {
if (field.getType() instanceof ObjectType) {
RelationType relType = field.getFieldTypeAST().getListDepth() > 0 ? RelationType.OneToMany : RelationType.ManyToOne;
RelationImpl relation = new RelationImpl(type, field, relType);
//
((FieldImpl) field).setRelation(relation);
relations.add(relation);
}
// if (instanceof ObjectType)
}
// if (!type.isInputType())
}
// for (field)
}
// if (type.getRequestType()== null)
}
// for (type)
}
use of com.graphql_java_generator.plugin.language.Field in project graphql-maven-plugin-project by graphql-java-generator.
the class AddRelayConnectionsTest method test_getFieldInheritedFrom_interfaceThatImplementsInterface.
@Test
@Execution(ExecutionMode.CONCURRENT)
void test_getFieldInheritedFrom_interfaceThatImplementsInterface() throws IOException {
// Preparation
loadSpringContext(AllGraphQLCasesRelayConnection_Client_SpringConfiguration.class, "test_getFieldInheritedFrom_interfaceThatImplementsInterface", true);
Field f = getField("AllFieldCasesInterface", "id");
// Go, go, go
List<Field> fields = addRelayConnections.getFieldInheritedFrom(f);
// Verification
assertEquals(1, fields.size());
assertEquals("id", fields.get(0).getName());
assertEquals("WithID", fields.get(0).getOwningType().getName());
}
use of com.graphql_java_generator.plugin.language.Field in project graphql-maven-plugin-project by graphql-java-generator.
the class ObjectType method toString.
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
boolean addSeparator;
sb.append(getClass().getSimpleName() + " {name:").append(getName());
sb.append(", fields:{");
addSeparator = false;
for (Field f : getFields()) {
if (addSeparator)
sb.append(",");
else
addSeparator = true;
sb.append(f.toString());
}
sb.append("}");
if (getImplementz().size() > 0) {
sb.append(", implements ");
sb.append(String.join(",", getImplementz()));
}
if (getComments() == null) {
sb.append(", comments=null");
} else if (getComments().size() > 0) {
sb.append(", comments=empty");
} else {
sb.append(", comments \"");
sb.append(String.join("\\n", getComments()));
sb.append("\"");
}
return sb.toString();
}
Aggregations