Skip to content

Commit 2097a0e

Browse files
committed
This removes all the deprecated Coercing methods and replaces them with the proper ones
It also addresses #156
1 parent fd47c81 commit 2097a0e

File tree

53 files changed

+740
-623
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

53 files changed

+740
-623
lines changed

src/main/java/graphql/scalars/alias/AliasedScalar.java

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,26 @@
11
package graphql.scalars.alias;
22

33
import graphql.Assert;
4+
import graphql.GraphQLContext;
45
import graphql.Internal;
6+
import graphql.execution.CoercedVariables;
57
import graphql.language.Value;
68
import graphql.schema.Coercing;
79
import graphql.schema.CoercingParseLiteralException;
810
import graphql.schema.CoercingParseValueException;
911
import graphql.schema.CoercingSerializeException;
1012
import graphql.schema.GraphQLScalarType;
1113

12-
import java.util.Map;
14+
import java.util.Locale;
1315

1416
/**
1517
* Access this via {@link graphql.scalars.ExtendedScalars#newAliasedScalar(String)}
1618
*/
1719
@Internal
1820
public final class AliasedScalar {
1921

20-
private AliasedScalar() {}
22+
private AliasedScalar() {
23+
}
2124

2225
/**
2326
* A builder for {@link graphql.scalars.alias.AliasedScalar}
@@ -75,31 +78,25 @@ public GraphQLScalarType build() {
7578

7679
private static GraphQLScalarType aliasedScalarImpl(String name, String description, GraphQLScalarType aliasedScalar) {
7780
Assert.assertNotNull(aliasedScalar);
78-
Coercing<Object, Object> coercing = new Coercing<Object, Object>() {
79-
@Override
80-
public Object serialize(Object input) throws CoercingSerializeException {
81-
return aliasedScalar.getCoercing().serialize(input);
82-
}
83-
81+
Coercing<Object, Object> coercing = new Coercing<>() {
8482
@Override
85-
public Object parseValue(Object input) throws CoercingParseValueException {
86-
return aliasedScalar.getCoercing().parseValue(input);
83+
public Object serialize(Object input, GraphQLContext graphQLContext, Locale locale) throws CoercingSerializeException {
84+
return aliasedScalar.getCoercing().serialize(input, graphQLContext, locale);
8785
}
8886

8987
@Override
90-
public Object parseLiteral(Object input) throws CoercingParseLiteralException {
91-
return aliasedScalar.getCoercing().parseLiteral(input);
88+
public Object parseValue(Object input, GraphQLContext graphQLContext, Locale locale) throws CoercingParseValueException {
89+
return aliasedScalar.getCoercing().parseValue(input, graphQLContext, locale);
9290
}
9391

9492
@Override
95-
public Object parseLiteral(Object input, Map<String, Object> variables) throws CoercingParseLiteralException {
96-
Coercing<?, ?> c = aliasedScalar.getCoercing();
97-
return c.parseLiteral(input, variables);
93+
public Object parseLiteral(Value<?> input, CoercedVariables variables, GraphQLContext graphQLContext, Locale locale) throws CoercingParseLiteralException {
94+
return aliasedScalar.getCoercing().parseLiteral(input, variables, graphQLContext, locale);
9895
}
9996

10097
@Override
101-
public Value<?> valueToLiteral(Object input) {
102-
return aliasedScalar.getCoercing().valueToLiteral(input);
98+
public Value<?> valueToLiteral(Object input, GraphQLContext graphQLContext, Locale locale) {
99+
return aliasedScalar.getCoercing().valueToLiteral(input, graphQLContext, locale);
103100
}
104101
};
105102
return GraphQLScalarType.newScalar()

src/main/java/graphql/scalars/color/hex/HexColorCodeScalar.java

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,22 @@
11
package graphql.scalars.color.hex;
22

3+
import graphql.GraphQLContext;
4+
import graphql.execution.CoercedVariables;
35
import graphql.language.StringValue;
46
import graphql.language.Value;
5-
import graphql.schema.*;
7+
import graphql.schema.Coercing;
8+
import graphql.schema.CoercingParseLiteralException;
9+
import graphql.schema.CoercingParseValueException;
10+
import graphql.schema.CoercingSerializeException;
11+
import graphql.schema.GraphQLScalarType;
612

713
import java.awt.*;
14+
import java.util.Locale;
815
import java.util.function.Function;
916
import java.util.regex.Pattern;
1017

1118
import static graphql.scalars.util.Kit.typeName;
19+
1220
/**
1321
* Access this via {@link graphql.scalars.ExtendedScalars#HexColorCode}
1422
* See the <a href="https://en.wikipedia.org/wiki/Web_colors">Web colors</a> for more details.
@@ -21,28 +29,28 @@ public class HexColorCodeScalar {
2129

2230

2331
static {
24-
Coercing<Color, String> coercing = new Coercing<Color, String>() {
32+
Coercing<Color, String> coercing = new Coercing<>() {
2533

2634
private final Pattern HEX_PATTERN = Pattern.compile("^(#([A-Fa-f0-9]{3,4}){1,2})$");
2735

2836
@Override
29-
public String serialize(Object input) throws CoercingSerializeException {
37+
public String serialize(Object input, GraphQLContext graphQLContext, Locale locale) throws CoercingSerializeException {
3038
Color color = parseColor(input, CoercingSerializeException::new);
3139
boolean hasAlpha = color.getAlpha() != 255;
32-
if (hasAlpha){
40+
if (hasAlpha) {
3341
return String.format("#%02x%02x%02x%02x", color.getRed(), color.getGreen(), color.getBlue(), color.getAlpha());
3442
} else {
3543
return String.format("#%02x%02x%02x", color.getRed(), color.getGreen(), color.getBlue());
3644
}
3745
}
3846

3947
@Override
40-
public Color parseValue(Object input) throws CoercingParseValueException {
48+
public Color parseValue(Object input, GraphQLContext graphQLContext, Locale locale) throws CoercingParseValueException {
4149
return parseColor(input, CoercingParseValueException::new);
4250
}
4351

4452
@Override
45-
public Color parseLiteral(Object input) throws CoercingParseLiteralException {
53+
public Color parseLiteral(Value<?> input, CoercedVariables variables, GraphQLContext graphQLContext, Locale locale) throws CoercingParseLiteralException {
4654
if (!(input instanceof StringValue)) {
4755
throw new CoercingParseLiteralException("Expected type 'StringValue' but was '" + typeName(input) + "'.");
4856
}
@@ -51,8 +59,8 @@ public Color parseLiteral(Object input) throws CoercingParseLiteralException {
5159
}
5260

5361
@Override
54-
public Value<?> valueToLiteral(Object input) {
55-
String s = serialize(input);
62+
public Value<?> valueToLiteral(Object input, GraphQLContext graphQLContext, Locale locale) {
63+
String s = serialize(input, graphQLContext, locale);
5664
return StringValue.newStringValue(s).build();
5765
}
5866

src/main/java/graphql/scalars/country/code/CountryCodeScalar.java

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,17 @@
11
package graphql.scalars.country.code;
22

3+
import graphql.GraphQLContext;
34
import graphql.Internal;
5+
import graphql.execution.CoercedVariables;
46
import graphql.language.StringValue;
57
import graphql.language.Value;
6-
import graphql.schema.*;
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;
713

14+
import java.util.Locale;
815
import java.util.function.Function;
916

1017
import static graphql.scalars.util.Kit.typeName;
@@ -18,21 +25,21 @@ public class CountryCodeScalar {
1825
public static final GraphQLScalarType INSTANCE;
1926

2027
static {
21-
Coercing<CountryCode, String> coercing = new Coercing<CountryCode, String>() {
28+
Coercing<CountryCode, String> coercing = new Coercing<>() {
2229

2330
@Override
24-
public String serialize(Object input) throws CoercingSerializeException {
31+
public String serialize(Object input, GraphQLContext graphQLContext, Locale locale) throws CoercingSerializeException {
2532
CountryCode countryCode = parseCountryCode(input, CoercingParseValueException::new);
2633
return countryCode.name();
2734
}
2835

2936
@Override
30-
public CountryCode parseValue(Object input) throws CoercingParseValueException {
37+
public CountryCode parseValue(Object input, GraphQLContext graphQLContext, Locale locale) throws CoercingParseValueException {
3138
return parseCountryCode(input, CoercingParseValueException::new);
3239
}
3340

3441
@Override
35-
public CountryCode parseLiteral(Object input) throws CoercingParseLiteralException {
42+
public CountryCode parseLiteral(Value<?> input, CoercedVariables variables, GraphQLContext graphQLContext, Locale locale) throws CoercingParseLiteralException {
3643
if (!(input instanceof StringValue)) {
3744
throw new CoercingParseLiteralException("Expected AST type 'StringValue' but was '" + typeName(input) + "'.");
3845
}
@@ -42,8 +49,8 @@ public CountryCode parseLiteral(Object input) throws CoercingParseLiteralExcepti
4249
}
4350

4451
@Override
45-
public Value<?> valueToLiteral(Object input) {
46-
String s = serialize(input);
52+
public Value<?> valueToLiteral(Object input, GraphQLContext graphQLContext, Locale locale) {
53+
String s = serialize(input, graphQLContext, locale);
4754
return StringValue.newStringValue(s).build();
4855
}
4956

src/main/java/graphql/scalars/currency/CurrencyScalar.java

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,18 @@
11
package graphql.scalars.currency;
22

3+
import graphql.GraphQLContext;
34
import graphql.Internal;
5+
import graphql.execution.CoercedVariables;
46
import graphql.language.StringValue;
57
import graphql.language.Value;
6-
import graphql.schema.*;
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;
713

814
import java.util.Currency;
15+
import java.util.Locale;
916
import java.util.function.Function;
1017

1118
import static graphql.scalars.util.Kit.typeName;
@@ -19,21 +26,21 @@ public class CurrencyScalar {
1926
public static final GraphQLScalarType INSTANCE;
2027

2128
static {
22-
Coercing<Currency, String> coercing = new Coercing<Currency, String>() {
29+
Coercing<Currency, String> coercing = new Coercing<>() {
2330
@Override
24-
public String serialize(Object input) throws CoercingSerializeException {
31+
public String serialize(Object input, GraphQLContext graphQLContext, Locale locale) throws CoercingSerializeException {
2532
Currency currency = parseCurrency(input, CoercingSerializeException::new);
2633
return currency.getCurrencyCode();
2734
}
2835

2936
@Override
30-
public Currency parseValue(Object input) throws CoercingParseValueException {
37+
public Currency parseValue(Object input, GraphQLContext graphQLContext, Locale locale) throws CoercingParseValueException {
3138
return parseCurrency(input, CoercingParseValueException::new);
3239
}
3340

3441

3542
@Override
36-
public Currency parseLiteral(Object input) throws CoercingParseLiteralException {
43+
public Currency parseLiteral(Value<?> input, CoercedVariables variables, GraphQLContext graphQLContext, Locale locale) throws CoercingParseLiteralException {
3744
if (!(input instanceof StringValue)) {
3845
throw new CoercingParseLiteralException("Expected AST type 'StringValue' but was '" + typeName(input) + "'.");
3946
}
@@ -42,8 +49,8 @@ public Currency parseLiteral(Object input) throws CoercingParseLiteralException
4249
}
4350

4451
@Override
45-
public Value<?> valueToLiteral(Object input) {
46-
String serializedInput = serialize(input);
52+
public Value<?> valueToLiteral(Object input, GraphQLContext graphQLContext, Locale locale) {
53+
String serializedInput = serialize(input, graphQLContext, locale);
4754
return StringValue.newStringValue(serializedInput).build();
4855
}
4956

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package graphql.scalars.datetime;
22

3+
import graphql.GraphQLContext;
34
import graphql.Internal;
5+
import graphql.execution.CoercedVariables;
46
import graphql.language.StringValue;
57
import graphql.language.Value;
68
import graphql.schema.Coercing;
@@ -11,6 +13,7 @@
1113

1214
import java.time.Duration;
1315
import java.time.format.DateTimeParseException;
16+
import java.util.Locale;
1417
import java.util.function.Function;
1518

1619
import static graphql.scalars.util.Kit.typeName;
@@ -23,53 +26,54 @@ public class AccurateDurationScalar {
2326

2427
public static final GraphQLScalarType INSTANCE;
2528

26-
private AccurateDurationScalar() {}
29+
private AccurateDurationScalar() {
30+
}
2731

2832
static {
29-
Coercing<Duration, String> coercing = new Coercing<Duration, String>() {
33+
Coercing<Duration, String> coercing = new Coercing<>() {
3034
@Override
31-
public String serialize(Object input) throws CoercingSerializeException {
35+
public String serialize(Object input, GraphQLContext graphQLContext, Locale locale) throws CoercingSerializeException {
3236
Duration duration;
3337
if (input instanceof Duration) {
3438
duration = (Duration) input;
3539
} else if (input instanceof String) {
3640
duration = parseDuration(input.toString(), CoercingSerializeException::new);
3741
} else {
3842
throw new CoercingSerializeException(
39-
"Expected something we can convert to 'java.time.Duration' but was '" + typeName(input) + "'."
43+
"Expected something we can convert to 'java.time.Duration' but was '" + typeName(input) + "'."
4044
);
4145
}
4246
return duration.toString();
4347
}
4448

4549
@Override
46-
public Duration parseValue(Object input) throws CoercingParseValueException {
50+
public Duration parseValue(Object input, GraphQLContext graphQLContext, Locale locale) throws CoercingParseValueException {
4751
Duration duration;
4852
if (input instanceof Duration) {
4953
duration = (Duration) input;
5054
} else if (input instanceof String) {
5155
duration = parseDuration(input.toString(), CoercingParseValueException::new);
5256
} else {
5357
throw new CoercingParseValueException(
54-
"Expected a 'String' but was '" + typeName(input) + "'."
58+
"Expected a 'String' but was '" + typeName(input) + "'."
5559
);
5660
}
5761
return duration;
5862
}
5963

6064
@Override
61-
public Duration parseLiteral(Object input) throws CoercingParseLiteralException {
65+
public Duration parseLiteral(Value<?> input, CoercedVariables variables, GraphQLContext graphQLContext, Locale locale) throws CoercingParseLiteralException {
6266
if (!(input instanceof StringValue)) {
6367
throw new CoercingParseLiteralException(
64-
"Expected AST type 'StringValue' but was '" + typeName(input) + "'."
68+
"Expected AST type 'StringValue' but was '" + typeName(input) + "'."
6569
);
6670
}
6771
return parseDuration(((StringValue) input).getValue(), CoercingParseLiteralException::new);
6872
}
6973

7074
@Override
71-
public Value<?> valueToLiteral(Object input) {
72-
String s = serialize(input);
75+
public Value<?> valueToLiteral(Object input, GraphQLContext graphQLContext, Locale locale) {
76+
String s = serialize(input, graphQLContext, locale);
7377
return StringValue.newStringValue(s).build();
7478
}
7579

@@ -83,11 +87,11 @@ private Duration parseDuration(String s, Function<String, RuntimeException> exce
8387
};
8488

8589
INSTANCE = GraphQLScalarType.newScalar()
86-
.name("AccurateDuration")
87-
.description("A ISO 8601 duration scalar with only day, hour, minute, second components.")
88-
.specifiedByUrl("https://scalars.graphql.org/AlexandreCarlton/accurate-duration") // TODO: Change to .specifiedByURL when builder added to graphql-java
89-
.coercing(coercing)
90-
.build();
90+
.name("AccurateDuration")
91+
.description("A ISO 8601 duration scalar with only day, hour, minute, second components.")
92+
.specifiedByUrl("https://scalars.graphql.org/AlexandreCarlton/accurate-duration") // TODO: Change to .specifiedByURL when builder added to graphql-java
93+
.coercing(coercing)
94+
.build();
9195
}
9296

9397
}

0 commit comments

Comments
 (0)