use of graphql.schema.CoercingParseValueException in project jmix by jmix-framework.
the class FileRefCoercing method parseValue.
@Override
public Object parseValue(Object o) throws CoercingParseValueException {
String storageName = null;
try {
if (o instanceof AbstractMap.SimpleEntry) {
storageName = ((AbstractMap.SimpleEntry<String, MultipartFile>) o).getKey();
FileStorage fileStorage = fileService.getFileStorage(storageName);
MultipartFile multipartFile = ((AbstractMap.SimpleEntry<String, MultipartFile>) o).getValue();
FileRefDatatype refDatatype = new FileRefDatatype();
FileRef fileRef = fileService.saveFileIntoStorage(multipartFile, fileStorage);
return refDatatype.format(fileRef);
}
if (o instanceof String) {
FileRef fileRef = FileRef.fromString((String) o);
FileStorage fileStorage = fileService.getFileStorage(fileRef.getStorageName());
if (!fileStorage.fileExists(fileRef)) {
throw new FileNotFoundException();
}
return o;
}
} catch (FileNotFoundException e) {
throw new CoercingParseValueException("File with name " + FileRef.fromString((String) o).getFileName() + " not found");
} catch (IOException e) {
throw new CoercingParseValueException("Exception with saving file");
} catch (Exception e) {
throw new CoercingParseValueException("File storage with name " + storageName + " not found");
}
return null;
}
use of graphql.schema.CoercingParseValueException in project graphql-java by graphql-java.
the class ValuesResolver method externalValueToInternalValueForVariables.
/**
* performs validation too
*/
private Map<String, Object> externalValueToInternalValueForVariables(GraphQLSchema schema, List<VariableDefinition> variableDefinitions, Map<String, Object> rawVariables) {
GraphqlFieldVisibility fieldVisibility = schema.getCodeRegistry().getFieldVisibility();
Map<String, Object> coercedValues = new LinkedHashMap<>();
for (VariableDefinition variableDefinition : variableDefinitions) {
try {
String variableName = variableDefinition.getName();
GraphQLType variableType = TypeFromAST.getTypeFromAST(schema, variableDefinition.getType());
assertTrue(variableType instanceof GraphQLInputType);
// can be NullValue
Value defaultValue = variableDefinition.getDefaultValue();
boolean hasValue = rawVariables.containsKey(variableName);
Object value = rawVariables.get(variableName);
if (!hasValue && defaultValue != null) {
Object coercedDefaultValue = literalToInternalValue(fieldVisibility, variableType, defaultValue, Collections.emptyMap());
coercedValues.put(variableName, coercedDefaultValue);
} else if (isNonNull(variableType) && (!hasValue || value == null)) {
throw new NonNullableValueCoercedAsNullException(variableDefinition, variableType);
} else if (hasValue) {
if (value == null) {
coercedValues.put(variableName, null);
} else {
Object coercedValue = externalValueToInternalValue(fieldVisibility, variableType, value);
coercedValues.put(variableName, coercedValue);
}
}
} catch (CoercingParseValueException e) {
throw CoercingParseValueException.newCoercingParseValueException().message(String.format("Variable '%s' has an invalid value: %s", variableDefinition.getName(), e.getMessage())).extensions(e.getExtensions()).cause(e.getCause()).sourceLocation(variableDefinition.getSourceLocation()).build();
}
}
return coercedValues;
}
Aggregations