Skip to content

Added CountryCodeScalar & CurrencyScalar #83

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,56 @@ An example query to look for customers in the Romanian locale might look like:
}

```
## Country Code Scalar
The CountryCode scalar type as defined by [ISO 3166-1 alpha-2](https://en.wikipedia.org/wiki/ISO_3166-1_alpha-2).


An example declaration in SDL might be:
```graphql
scalar CountryCode

type Customer {
name : String
countryCode : CountryCode
}
```

And example query might look like:

```graphql
query {
customers(code : "US") {
name
countryCode
}
}
```
## Currency Scalar

A field whose value is an [ISO-4217](https://en.wikipedia.org/wiki/ISO_4217) currency.

An example declaration in SDL might be:
```graphql
scalar Currency

type Account {
id : String
currency : Currency
accountNumber: String
}
```

And example query might look like:

```graphql
query {
accounts(currency : "USD") {
id
currency
accountNumber
}
}
```

## Alias Scalars

Expand Down
16 changes: 14 additions & 2 deletions src/main/java/graphql/scalars/ExtendedScalars.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import graphql.PublicApi;
import graphql.scalars.alias.AliasedScalar;
import graphql.scalars.country.code.CountryCodeScalar;
import graphql.scalars.currency.CurrencyScalar;
import graphql.scalars.datetime.DateScalar;
import graphql.scalars.datetime.DateTimeScalar;
import graphql.scalars.datetime.LocalTimeCoercing;
Expand All @@ -23,8 +25,6 @@
import graphql.scalars.url.UrlScalar;
import graphql.schema.GraphQLScalarType;

import java.util.UUID;

/**
* This is the API entry point for all the extended scalars
*/
Expand Down Expand Up @@ -138,6 +138,18 @@ public class ExtendedScalars {
*/
public static final GraphQLScalarType Locale = LocaleScalar.INSTANCE;

/**
* A field whose value is an ISO-4217 currency.
* See the <a href="https://en.wikipedia.org/wiki/ISO_4217">ISO-4217</a> for more details.
*/
public static final GraphQLScalarType Currency = CurrencyScalar.INSTANCE;

/**
* The CountryCode scalar type 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 static final GraphQLScalarType CountryCode = CountryCodeScalar.INSTANCE;

/**
* A UUID scalar that accepts a universally unique identifier and produces {@link
* java.util.UUID} objects at runtime.
Expand Down
18 changes: 18 additions & 0 deletions src/main/java/graphql/scalars/country/code/CountryCode.java
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 src/main/java/graphql/scalars/country/code/CountryCodeScalar.java
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 src/main/java/graphql/scalars/currency/CurrencyScalar.java
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();
}
}
Loading