Search in sources :

Example 1 with CoercingParseValueException

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;
}
Also used : AbstractMap(java.util.AbstractMap) MultipartFile(org.springframework.web.multipart.MultipartFile) FileRefDatatype(io.jmix.core.metamodel.datatype.impl.FileRefDatatype) FileRef(io.jmix.core.FileRef) CoercingParseValueException(graphql.schema.CoercingParseValueException) FileNotFoundException(java.io.FileNotFoundException) FileStorage(io.jmix.core.FileStorage) IOException(java.io.IOException) CoercingSerializeException(graphql.schema.CoercingSerializeException) IOException(java.io.IOException) CoercingParseValueException(graphql.schema.CoercingParseValueException) FileNotFoundException(java.io.FileNotFoundException)

Example 2 with CoercingParseValueException

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;
}
Also used : GraphQLInputType(graphql.schema.GraphQLInputType) GraphqlFieldVisibility(graphql.schema.visibility.GraphqlFieldVisibility) DefaultGraphqlFieldVisibility(graphql.schema.visibility.DefaultGraphqlFieldVisibility) VariableDefinition(graphql.language.VariableDefinition) CoercingParseValueException(graphql.schema.CoercingParseValueException) GraphQLType(graphql.schema.GraphQLType) FloatValue(graphql.language.FloatValue) Value(graphql.language.Value) NormalizedInputValue(graphql.normalized.NormalizedInputValue) ArrayValue(graphql.language.ArrayValue) NullValue(graphql.language.NullValue) ObjectValue(graphql.language.ObjectValue) EnumValue(graphql.language.EnumValue) NullValue.newNullValue(graphql.language.NullValue.newNullValue) StringValue(graphql.language.StringValue) IntValue(graphql.language.IntValue) BooleanValue(graphql.language.BooleanValue) LinkedHashMap(java.util.LinkedHashMap)

Aggregations

CoercingParseValueException (graphql.schema.CoercingParseValueException)2 ArrayValue (graphql.language.ArrayValue)1 BooleanValue (graphql.language.BooleanValue)1 EnumValue (graphql.language.EnumValue)1 FloatValue (graphql.language.FloatValue)1 IntValue (graphql.language.IntValue)1 NullValue (graphql.language.NullValue)1 NullValue.newNullValue (graphql.language.NullValue.newNullValue)1 ObjectValue (graphql.language.ObjectValue)1 StringValue (graphql.language.StringValue)1 Value (graphql.language.Value)1 VariableDefinition (graphql.language.VariableDefinition)1 NormalizedInputValue (graphql.normalized.NormalizedInputValue)1 CoercingSerializeException (graphql.schema.CoercingSerializeException)1 GraphQLInputType (graphql.schema.GraphQLInputType)1 GraphQLType (graphql.schema.GraphQLType)1 DefaultGraphqlFieldVisibility (graphql.schema.visibility.DefaultGraphqlFieldVisibility)1 GraphqlFieldVisibility (graphql.schema.visibility.GraphqlFieldVisibility)1 FileRef (io.jmix.core.FileRef)1 FileStorage (io.jmix.core.FileStorage)1