1212import graphql .schema .GraphQLScalarType ;
1313
1414import java .io .File ;
15- import java .net .MalformedURLException ;
1615import java .net .URI ;
1716import java .net .URISyntaxException ;
1817import java .net .URL ;
@@ -31,86 +30,83 @@ private UriScalar() {
3130 public static final GraphQLScalarType INSTANCE ;
3231
3332 static {
34- Coercing <URL , URL > coercing = new Coercing <>() {
33+ Coercing <URI , URI > coercing = new Coercing <>() {
3534 @ Override
36- public URL serialize (Object input , GraphQLContext graphQLContext , Locale locale ) throws CoercingSerializeException {
37- Optional <URL > url ;
35+ public URI serialize (Object input , GraphQLContext graphQLContext , Locale locale ) throws CoercingSerializeException {
36+ Optional <URI > uri ;
3837 if (input instanceof String ) {
39- url = Optional .of (parseURL (input .toString (), CoercingSerializeException ::new ));
38+ uri = Optional .of (parseURI (input .toString (), CoercingSerializeException ::new ));
4039 } else {
41- url = toURL (input );
40+ uri = toURI (input );
4241 }
43- if (url .isPresent ()) {
44- return url .get ();
42+ if (uri .isPresent ()) {
43+ return uri .get ();
4544 }
4645 throw new CoercingSerializeException (
47- "Expected a 'URL ' like object but was '" + typeName (input ) + "'."
46+ "Expected a 'URI ' like object but was '" + typeName (input ) + "'."
4847 );
4948 }
5049
5150 @ Override
52- public URL parseValue (Object input , GraphQLContext graphQLContext , Locale locale ) throws CoercingParseValueException {
53- String urlStr ;
51+ public URI parseValue (Object input , GraphQLContext graphQLContext , Locale locale ) throws CoercingParseValueException {
52+ String uriStr ;
5453 if (input instanceof String ) {
55- urlStr = String .valueOf (input );
54+ uriStr = String .valueOf (input );
5655 } else {
57- Optional <URL > url = toURL (input );
58- if (url .isEmpty ()) {
56+ Optional <URI > uri = toURI (input );
57+ if (uri .isEmpty ()) {
5958 throw new CoercingParseValueException (
60- "Expected a 'URL ' like object but was '" + typeName (input ) + "'."
59+ "Expected a 'URI ' like object but was '" + typeName (input ) + "'."
6160 );
6261 }
63- return url .get ();
62+ return uri .get ();
6463 }
65- return parseURL ( urlStr , CoercingParseValueException ::new );
64+ return parseURI ( uriStr , CoercingParseValueException ::new );
6665 }
6766
6867 @ Override
69- public URL parseLiteral (Value <?> input , CoercedVariables variables , GraphQLContext graphQLContext , Locale locale ) throws CoercingParseLiteralException {
68+ public URI parseLiteral (Value <?> input , CoercedVariables variables , GraphQLContext graphQLContext , Locale locale ) throws CoercingParseLiteralException {
7069 if (!(input instanceof StringValue )) {
7170 throw new CoercingParseLiteralException (
7271 "Expected AST type 'StringValue' but was '" + typeName (input ) + "'."
7372 );
7473 }
75- return parseURL (((StringValue ) input ).getValue (), CoercingParseLiteralException ::new );
74+ return parseURI (((StringValue ) input ).getValue (), CoercingParseLiteralException ::new );
7675 }
7776
7877 @ Override
7978 public Value <?> valueToLiteral (Object input , GraphQLContext graphQLContext , Locale locale ) {
80- URL url = serialize (input , graphQLContext , locale );
81- return StringValue .newStringValue (url . toExternalForm ()).build ();
79+ URI uri = serialize (input , graphQLContext , locale );
80+ return StringValue .newStringValue (uri . toString ()).build ();
8281 }
8382
8483
85- private URL parseURL (String input , Function <String , RuntimeException > exceptionMaker ) {
84+ private URI parseURI (String input , Function <String , RuntimeException > exceptionMaker ) {
8685 try {
87- return new URI (input ). toURL () ;
88- } catch (URISyntaxException | IllegalArgumentException | MalformedURLException e ) {
89- throw exceptionMaker .apply ("Invalid URL value : '" + input + "'." );
86+ return new URI (input );
87+ } catch (URISyntaxException e ) {
88+ throw exceptionMaker .apply ("Invalid URI value : '" + input + "'." );
9089 }
9190 }
9291 };
9392
9493 INSTANCE = GraphQLScalarType .newScalar ()
95- .name ("Url " )
96- .description ("A Url scalar" )
94+ .name ("Uri " )
95+ .description ("A Uri scalar" )
9796 .coercing (coercing )
9897 .build ();
9998 }
10099
101- private static Optional <URL > toURL (Object input ) {
102- if (input instanceof URL ) {
103- return Optional .of ((URL ) input );
104- } else if (input instanceof URI ) {
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 ) {
105104 try {
106- return Optional .of (((URI ) input ).toURL ());
107- } catch (MalformedURLException ignored ) {
105+ return Optional .of (((URL ) input ).toURI ());
106+ } catch (URISyntaxException ignored ) {
108107 }
109108 } else if (input instanceof File ) {
110- try {
111- return Optional .of (((File ) input ).toURI ().toURL ());
112- } catch (MalformedURLException ignored ) {
113- }
109+ return Optional .of (((File ) input ).toURI ());
114110 }
115111 return Optional .empty ();
116112 }
0 commit comments