| 90 | } |
| 91 | |
| 92 | private static GraphQLScalarType urlType() { |
| 93 | return GraphQLScalarType.newScalar() |
| 94 | .name("Url") |
| 95 | .coercing( |
| 96 | new Coercing<URL, String>() { |
| 97 | @Override |
| 98 | public String serialize(Object o) throws CoercingSerializeException { |
| 99 | if (o instanceof String) { |
| 100 | return (String) o; |
| 101 | } |
| 102 | |
| 103 | if (o instanceof URL || o instanceof URI) { |
| 104 | return String.valueOf(o); |
| 105 | } |
| 106 | |
| 107 | throw new CoercingSerializeException("Unable to coerce " + o); |
| 108 | } |
| 109 | |
| 110 | @Nullable |
| 111 | @Override |
| 112 | public URL parseValue(@Nullable Object input) throws CoercingParseValueException { |
| 113 | if (input == null) { |
| 114 | return null; |
| 115 | } |
| 116 | |
| 117 | if (input instanceof URL) { |
| 118 | return (URL) input; |
| 119 | } |
| 120 | |
| 121 | try { |
| 122 | if (input instanceof String) { |
| 123 | return new URL((String) input); |
| 124 | } |
| 125 | |
| 126 | if (input instanceof URI) { |
| 127 | return ((URI) input).toURL(); |
| 128 | } |
| 129 | } catch (MalformedURLException e) { |
| 130 | throw new CoercingParseValueException("Unable to create URL: " + input, e); |
| 131 | } |
| 132 | |
| 133 | throw new CoercingParseValueException("Unable to create URL: " + input); |
| 134 | } |
| 135 | |
| 136 | @Override |
| 137 | public URL parseLiteral(Object input) throws CoercingParseLiteralException { |
| 138 | if (!(input instanceof StringValue)) { |
| 139 | throw new CoercingParseLiteralException("Cannot convert to URL: " + input); |
| 140 | } |
| 141 | |
| 142 | try { |
| 143 | return new URL(((StringValue) input).getValue()); |
| 144 | } catch (MalformedURLException e) { |
| 145 | throw new CoercingParseLiteralException("Unable to parse: " + input); |
| 146 | } |
| 147 | } |
| 148 | }) |
| 149 | .build(); |