Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added CountryCodeScalar & CurrencyScalar
- Loading branch information
Showing
8 changed files
with
868 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
src/main/java/graphql/scalars/country/code/CountryCode.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package graphql.scalars.country.code; | ||
|
||
/** | ||
* The CountryCode list as defined by ISO 3166-1 alpha-2. | ||
* See the <a href="https://en.wikipedia.org/wiki/ISO_3166-1_alpha-2">ISO 3166-1 alpha-2</a> for more details. | ||
*/ | ||
public enum CountryCode { | ||
AD, AE, AF, AG, AI, AL, AM, AO, AQ, AR, AS, AT, AU, AW, AX, AZ, BA, BB, BD, BE, BF, BG, BH, BI, BJ, BL, BM, BN, BO, | ||
BQ, BR, BS, BT, BV, BW, BY, BZ, CA, CC, CD, CF, CG, CH, CI, CK, CL, CM, CN, CO, CR, CU, CV, CW, CX, CY, CZ, DE, DJ, | ||
DK, DM, DO, DZ, EC, EE, EG, EH, ER, ES, ET, FI, FJ, FK, FM, FO, FR, GA, GB, GD, GE, GF, GG, GH, GI, GL, GM, GN, GP, | ||
GQ, GR, GS, GT, GU, GW, GY, HK, HM, HN, HR, HT, HU, ID, IE, IL, IM, IN, IO, IQ, IR, IS, IT, JE, JM, JO, JP, KE, KG, | ||
KH, KI, KM, KN, KP, KR, KW, KY, KZ, LA, LB, LC, LI, LK, LR, LS, LT, LU, LV, LY, MA, MC, MD, ME, MF, MG, MH, MK, ML, | ||
MM, MN, MO, MP, MQ, MR, MS, MT, MU, MV, MW, MX, MY, MZ, NA, NC, NE, NF, NG, NI, NL, NO, NP, NR, NU, NZ, OM, PA, PE, | ||
PF, PG, PH, PK, PL, PM, PN, PR, PS, PT, PW, PY, QA, RE, RO, RS, RU, RW, SA, SB, SC, SD, SE, SG, SH, SI, SJ, SK, SL, | ||
SM, SN, SO, SR, SS, ST, SV, SX, SY, SZ, TC, TD, TF, TG, TH, TJ, TK, TL, TM, TN, TO, TR, TT, TV, TW, TZ, UA, UG, UM, | ||
US, UY, UZ, VA, VC, VE, VG, VI, VN, VU, WF, WS, YE, YT, ZA, ZM, ZW | ||
} | ||
|
72 changes: 72 additions & 0 deletions
72
src/main/java/graphql/scalars/country/code/CountryCodeScalar.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package graphql.scalars.country.code; | ||
|
||
import graphql.Internal; | ||
import graphql.language.StringValue; | ||
import graphql.language.Value; | ||
import graphql.schema.*; | ||
|
||
import java.util.function.Function; | ||
|
||
import static graphql.scalars.util.Kit.typeName; | ||
|
||
/** | ||
* Access this via {@link graphql.scalars.ExtendedScalars#CountryCode} | ||
*/ | ||
@Internal | ||
public class CountryCodeScalar { | ||
|
||
public static final GraphQLScalarType INSTANCE; | ||
|
||
static { | ||
Coercing<CountryCode, String> coercing = new Coercing<CountryCode, String>() { | ||
|
||
@Override | ||
public String serialize(Object input) throws CoercingSerializeException { | ||
CountryCode countryCode = parseCountryCode(input, CoercingParseValueException::new); | ||
return countryCode.name(); | ||
} | ||
|
||
@Override | ||
public CountryCode parseValue(Object input) throws CoercingParseValueException { | ||
return parseCountryCode(input, CoercingParseValueException::new); | ||
} | ||
|
||
@Override | ||
public CountryCode parseLiteral(Object input) throws CoercingParseLiteralException { | ||
if (!(input instanceof StringValue)) { | ||
throw new CoercingParseLiteralException("Expected AST type 'StringValue' but was '" + typeName(input) + "'."); | ||
} | ||
String stringValue = ((StringValue) input).getValue(); | ||
return parseCountryCode(stringValue, CoercingParseLiteralException::new); | ||
|
||
} | ||
|
||
@Override | ||
public Value<?> valueToLiteral(Object input) { | ||
String s = serialize(input); | ||
return StringValue.newStringValue(s).build(); | ||
} | ||
|
||
private CountryCode parseCountryCode(Object input, Function<String, RuntimeException> exceptionMaker) { | ||
final CountryCode result; | ||
if (input instanceof String) { | ||
try { | ||
result = CountryCode.valueOf((String) input); | ||
} catch (NullPointerException | IllegalArgumentException ex) { | ||
throw exceptionMaker.apply("Invalid ISO 3166-1 alpha-2 value : '" + input + "'. because of : '" + ex.getMessage() + "'"); | ||
} | ||
} else if (input instanceof CountryCode) { | ||
result = (CountryCode) input; | ||
} else { | ||
throw exceptionMaker.apply("Expected a 'String' or 'CountryCode' but was '" + typeName(input) + "'."); | ||
} | ||
return result; | ||
} | ||
}; | ||
|
||
INSTANCE = GraphQLScalarType.newScalar() | ||
.name("CountryCode") | ||
.description("The CountryCode scalar type as defined by ISO 3166-1 alpha-2.") | ||
.coercing(coercing).build(); | ||
} | ||
} |
73 changes: 73 additions & 0 deletions
73
src/main/java/graphql/scalars/currency/CurrencyScalar.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package graphql.scalars.currency; | ||
|
||
import graphql.Internal; | ||
import graphql.language.StringValue; | ||
import graphql.language.Value; | ||
import graphql.schema.*; | ||
|
||
import java.util.Currency; | ||
import java.util.function.Function; | ||
|
||
import static graphql.scalars.util.Kit.typeName; | ||
|
||
/** | ||
* Access this via {@link graphql.scalars.ExtendedScalars#Currency} | ||
*/ | ||
@Internal | ||
public class CurrencyScalar { | ||
|
||
public static final GraphQLScalarType INSTANCE; | ||
|
||
static { | ||
Coercing<Currency, String> coercing = new Coercing<Currency, String>() { | ||
@Override | ||
public String serialize(Object input) throws CoercingSerializeException { | ||
Currency currency = parseCurrency(input, CoercingSerializeException::new); | ||
return currency.getCurrencyCode(); | ||
} | ||
|
||
@Override | ||
public Currency parseValue(Object input) throws CoercingParseValueException { | ||
return parseCurrency(input, CoercingParseValueException::new); | ||
} | ||
|
||
|
||
@Override | ||
public Currency parseLiteral(Object input) throws CoercingParseLiteralException { | ||
if (!(input instanceof StringValue)) { | ||
throw new CoercingParseLiteralException("Expected AST type 'StringValue' but was '" + typeName(input) + "'."); | ||
} | ||
String stringValue = ((StringValue) input).getValue(); | ||
return parseCurrency(stringValue, CoercingParseLiteralException::new); | ||
} | ||
|
||
@Override | ||
public Value<?> valueToLiteral(Object input) { | ||
String serializedInput = serialize(input); | ||
return StringValue.newStringValue(serializedInput).build(); | ||
} | ||
|
||
|
||
private Currency parseCurrency(Object input, Function<String, RuntimeException> exceptionMaker) { | ||
final Currency result; | ||
if (input instanceof Currency) { | ||
result = (Currency) input; | ||
} else if (input instanceof String) { | ||
try { | ||
result = Currency.getInstance((String) input); | ||
} catch (NullPointerException | IllegalArgumentException ex) { | ||
throw exceptionMaker.apply("Invalid ISO 4217 value : '" + input + "'. because of : '" + ex.getMessage() + "'"); | ||
} | ||
} else { | ||
throw exceptionMaker.apply("Expected a 'String' or 'Currency' but was '" + typeName(input) + "'."); | ||
} | ||
return result; | ||
} | ||
}; | ||
|
||
INSTANCE = GraphQLScalarType.newScalar() | ||
.name("Currency") | ||
.description("An ISO-4217 compliant Currency Scalar") | ||
.coercing(coercing).build(); | ||
} | ||
} |
Oops, something went wrong.