use of com.graphql_java_generator.plugin.language.impl.CustomScalarType 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;
}
use of com.graphql_java_generator.plugin.language.impl.CustomScalarType in project graphql-maven-plugin-project by graphql-java-generator.
the class GenerateCodeDocumentParser method initScalarTypes.
/**
* This method initializes the {@link #scalarTypes} list. This list depends on the use case
*/
@Override
protected void initScalarTypes(Class<?> notUsed) {
initConfiguration();
// Let's load the standard Scalar types
if (configuration.getMode().equals(PluginMode.server)) {
try {
super.initScalarTypes(Class.forName(((GenerateServerCodeConfiguration) configuration).getJavaTypeForIDType()));
} catch (ClassNotFoundException e) {
throw new RuntimeException(e.getMessage(), e);
}
} else {
// In client mode, ID type is managed as a String
super.initScalarTypes(String.class);
}
// ////////////////////////////////////////////////////////////////////////////////////////
// Add of all GraphQL custom scalar implementations must be provided by the plugin configuration
logger.debug("Storing custom scalar's implementations [START]");
if (configuration.getCustomScalars() != null) {
for (CustomScalarDefinition customScalarDef : configuration.getCustomScalars()) {
CustomScalarType type = new CustomScalarType(customScalarDef, configuration, this);
getCustomScalars().add(type);
getTypes().put(type.getName(), type);
}
}
logger.debug("Storing custom scalar's implementations [END]");
}
use of com.graphql_java_generator.plugin.language.impl.CustomScalarType in project graphql-maven-plugin-project by graphql-java-generator.
the class GenerateGraphQLSchemaDocumentParser method getCustomScalarType.
/**
* This class doesn't need an implementation for the Custom Scalars. So a dummy one is returned. {@inheritDoc}
*/
@Override
protected CustomScalarType getCustomScalarType(String name) {
// If this custom scalar has already been added to the list, let's return it
for (CustomScalarType customScalarType : customScalars) {
if (customScalarType.getName().equals(name)) {
return customScalarType;
}
}
// The custom scalar has not been added to the list yet, let's add it first.
CustomScalarDefinition customScalarDefinition = new CustomScalarDefinition(name, "java.lang.String", "com.graphql_java_generator.customscalars.GraphQLScalarTypeString", null, null);
CustomScalarType customScalarType = new CustomScalarType(customScalarDefinition, configuration, this);
customScalars.add(customScalarType);
types.put(customScalarType.getName(), customScalarType);
return customScalarType;
}
use of com.graphql_java_generator.plugin.language.impl.CustomScalarType in project graphql-maven-plugin-project by graphql-java-generator.
the class DocumentParser method readCustomScalarType.
/**
* 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
*/
CustomScalarType readCustomScalarType(ScalarTypeDefinition node) {
String name = node.getName();
CustomScalarType customScalarType = getCustomScalarType(name);
customScalarType.setAppliedDirectives(readAppliedDirectives(node.getDirectives()));
// Let's store its comments
customScalarType.setComments(node.getComments());
return customScalarType;
}
use of com.graphql_java_generator.plugin.language.impl.CustomScalarType in project graphql-maven-plugin-project by graphql-java-generator.
the class GenerateCodeDocumentParser method initCustomSerializers.
/**
* This method reads all the input types, to identify all the {@link CustomSerializer} that must be defined.
*/
private void initCustomSerializers() {
Map<Type, Integer> maxListLevelPerType = new HashMap<>();
getObjectTypes().stream().filter((o) -> o.isInputType()).flatMap((o) -> o.getFields().stream()).filter((f) -> f.getType() instanceof CustomScalarType).forEach((f) -> {
// listLevel: 0 for non array GraphQL types, 1 for arrays like [Int], 2 for nested arrays like
// [[Int]]...
int listLevel = f.getFieldTypeAST().getListDepth();
Integer alreadyDefinedListLevel = maxListLevelPerType.get(f.getType());
if (alreadyDefinedListLevel == null || alreadyDefinedListLevel < listLevel) {
// The current type is a deeper nested array
maxListLevelPerType.put(f.getType(), listLevel);
}
});
// We now know the maximum listLevel for each type. We can now define all the necessary custom serializers
customSerializers = new ArrayList<>();
for (Type t : maxListLevelPerType.keySet()) {
// We manage all the list levels for the embedded arrays of this type, as found in the GraphQL schema.
for (int i = 0; i <= maxListLevelPerType.get(t); i += 1) {
customSerializers.add(new CustomSerializer(t.getName(), t.getClassFullName(), ((CustomScalar) t).getCustomScalarDefinition(), i));
}
}
}
Aggregations