|
| 1 | +package graphql.scalars.uri; |
| 2 | + |
| 3 | +import graphql.GraphQLContext; |
| 4 | +import graphql.Internal; |
| 5 | +import graphql.execution.CoercedVariables; |
| 6 | +import graphql.language.StringValue; |
| 7 | +import graphql.language.Value; |
| 8 | +import graphql.schema.Coercing; |
| 9 | +import graphql.schema.CoercingParseLiteralException; |
| 10 | +import graphql.schema.CoercingParseValueException; |
| 11 | +import graphql.schema.CoercingSerializeException; |
| 12 | +import graphql.schema.GraphQLScalarType; |
| 13 | + |
| 14 | +import java.io.File; |
| 15 | +import java.net.URI; |
| 16 | +import java.net.URISyntaxException; |
| 17 | +import java.net.URL; |
| 18 | +import java.util.Locale; |
| 19 | +import java.util.Optional; |
| 20 | +import java.util.function.Function; |
| 21 | + |
| 22 | +import static graphql.scalars.util.Kit.typeName; |
| 23 | + |
| 24 | +@Internal |
| 25 | +public final class UriScalar { |
| 26 | + |
| 27 | + private UriScalar() { |
| 28 | + } |
| 29 | + |
| 30 | + public static final GraphQLScalarType INSTANCE; |
| 31 | + |
| 32 | + static { |
| 33 | + Coercing<URI, URI> coercing = new Coercing<>() { |
| 34 | + @Override |
| 35 | + public URI serialize(Object input, GraphQLContext graphQLContext, Locale locale) throws CoercingSerializeException { |
| 36 | + Optional<URI> uri; |
| 37 | + if (input instanceof String) { |
| 38 | + uri = Optional.of(parseURI(input.toString(), CoercingSerializeException::new)); |
| 39 | + } else { |
| 40 | + uri = toURI(input); |
| 41 | + } |
| 42 | + if (uri.isPresent()) { |
| 43 | + return uri.get(); |
| 44 | + } |
| 45 | + throw new CoercingSerializeException( |
| 46 | + "Expected a 'URI' like object but was '" + typeName(input) + "'." |
| 47 | + ); |
| 48 | + } |
| 49 | + |
| 50 | + @Override |
| 51 | + public URI parseValue(Object input, GraphQLContext graphQLContext, Locale locale) throws CoercingParseValueException { |
| 52 | + String uriStr; |
| 53 | + if (input instanceof String) { |
| 54 | + uriStr = String.valueOf(input); |
| 55 | + } else { |
| 56 | + Optional<URI> uri = toURI(input); |
| 57 | + if (uri.isEmpty()) { |
| 58 | + throw new CoercingParseValueException( |
| 59 | + "Expected a 'URI' like object but was '" + typeName(input) + "'." |
| 60 | + ); |
| 61 | + } |
| 62 | + return uri.get(); |
| 63 | + } |
| 64 | + return parseURI(uriStr, CoercingParseValueException::new); |
| 65 | + } |
| 66 | + |
| 67 | + @Override |
| 68 | + public URI parseLiteral(Value<?> input, CoercedVariables variables, GraphQLContext graphQLContext, Locale locale) throws CoercingParseLiteralException { |
| 69 | + if (!(input instanceof StringValue)) { |
| 70 | + throw new CoercingParseLiteralException( |
| 71 | + "Expected AST type 'StringValue' but was '" + typeName(input) + "'." |
| 72 | + ); |
| 73 | + } |
| 74 | + return parseURI(((StringValue) input).getValue(), CoercingParseLiteralException::new); |
| 75 | + } |
| 76 | + |
| 77 | + @Override |
| 78 | + public Value<?> valueToLiteral(Object input, GraphQLContext graphQLContext, Locale locale) { |
| 79 | + URI uri = serialize(input, graphQLContext, locale); |
| 80 | + return StringValue.newStringValue(uri.toString()).build(); |
| 81 | + } |
| 82 | + |
| 83 | + |
| 84 | + private URI parseURI(String input, Function<String, RuntimeException> exceptionMaker) { |
| 85 | + try { |
| 86 | + return new URI(input); |
| 87 | + } catch (URISyntaxException e) { |
| 88 | + throw exceptionMaker.apply("Invalid URI value : '" + input + "'."); |
| 89 | + } |
| 90 | + } |
| 91 | + }; |
| 92 | + |
| 93 | + INSTANCE = GraphQLScalarType.newScalar() |
| 94 | + .name("Uri") |
| 95 | + .description("A Uri scalar") |
| 96 | + .coercing(coercing) |
| 97 | + .build(); |
| 98 | + } |
| 99 | + |
| 100 | + private static Optional<URI> toURI(Object input) { |
| 101 | + if (input instanceof URI) { |
| 102 | + return Optional.of((URI) input); |
| 103 | + } else if (input instanceof URL) { |
| 104 | + try { |
| 105 | + return Optional.of(((URL) input).toURI()); |
| 106 | + } catch (URISyntaxException ignored) { |
| 107 | + } |
| 108 | + } else if (input instanceof File) { |
| 109 | + return Optional.of(((File) input).toURI()); |
| 110 | + } |
| 111 | + return Optional.empty(); |
| 112 | + } |
| 113 | + |
| 114 | +} |
0 commit comments