All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.xlrit.gears.server.graphql.CustomScalars Maven / Gradle / Ivy

There is a newer version: 1.17.6
Show newest version
package com.xlrit.gears.server.graphql;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import graphql.schema.*;
import org.springframework.web.multipart.MultipartFile;

public final class CustomScalars {
	private CustomScalars() {}

	public static GraphQLScalarType upload() {
		return GraphQLScalarType.newScalar()
			.name("Upload")
			.description("A file part in a multipart request")
			.coercing(new Coercing() {
				@Override
				public MultipartFile serialize(Object dataFetcherResult) throws CoercingSerializeException {
					throw new CoercingSerializeException("Upload is an input-only type");
				}

				@Override
				public MultipartFile parseValue(Object input) throws CoercingParseValueException {
					if (!(input instanceof MultipartFile)) {
						throw new CoercingParseValueException(
							String.format("Expected a 'MultipartFile' like object but was '%s'.", input != null ? input.getClass() : null)
						);
					}
					return (MultipartFile) input;
				}

				@Override
				public MultipartFile parseLiteral(Object input) throws CoercingParseLiteralException {
					throw new CoercingParseLiteralException("Parsing literal of 'MultipartFile' is not supported");
				}
			})
			.build();
	}

	public static GraphQLScalarType json(ObjectMapper objectMapper) {
		return GraphQLScalarType.newScalar()
			.name("JSON")
			.description("JSON value")
			.coercing(new Coercing() {
				@Override
				public Object serialize(Object dataFetcherResult) throws CoercingSerializeException {
					return dataFetcherResult;
				}

				@Override
				public JsonNode parseValue(Object input) throws CoercingParseValueException {
					return objectMapper.valueToTree(input);
				}

				@Override
				public JsonNode parseLiteral(Object input) throws CoercingParseLiteralException {
					return objectMapper.valueToTree(input);
				}
			})
			.build();
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy