Skip to content

Commit 30e4a80

Browse files
committed
Improved tests.
1 parent 9a46e1e commit 30e4a80

24 files changed

+134
-92
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.tailrocks.graphql.datetime;
2+
3+
import graphql.Internal;
4+
5+
@Internal
6+
final class CoercingUtil {
7+
8+
private CoercingUtil() {
9+
}
10+
11+
static String typeName(Object input) {
12+
return input == null ? "null" : input.getClass().getSimpleName();
13+
}
14+
15+
}

graphql-java-datetime/src/main/java/com/tailrocks/graphql/datetime/DateScalar.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ private DateScalar() {
3030
public static GraphQLScalarType create(String name) {
3131
return GraphQLScalarType.newScalar()
3232
.name(name != null ? name : "Date")
33-
.description("Date type")
33+
.description("A Java Date type")
3434
.coercing(new GraphqlDateCoercing())
3535
.build();
3636
}

graphql-java-datetime/src/main/java/com/tailrocks/graphql/datetime/DateTimeHelper.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
*/
1616
package com.tailrocks.graphql.datetime;
1717

18+
import graphql.Internal;
19+
1820
import java.time.LocalDateTime;
1921
import java.time.LocalTime;
2022
import java.time.ZoneOffset;
@@ -27,6 +29,7 @@
2729
/**
2830
* @author Alexey Zhokhov
2931
*/
32+
@Internal
3033
final class DateTimeHelper {
3134

3235
private DateTimeHelper() {

graphql-java-datetime/src/main/java/com/tailrocks/graphql/datetime/DurationScalar.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ private DurationScalar() {
3030
public static GraphQLScalarType create(String name) {
3131
return GraphQLScalarType.newScalar()
3232
.name(name != null ? name : "Duration")
33-
.description("A Java 8 ISO 8601 Duration")
33+
.description("A Java Duration type")
3434
.coercing(new GraphqlDurationCoercing())
3535
.build();
3636
}

graphql-java-datetime/src/main/java/com/tailrocks/graphql/datetime/GraphqlDateCoercing.java

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -58,22 +58,22 @@ private void initBasicFormatters() {
5858
}
5959

6060
private Date convertImpl(Object input) {
61-
if (input instanceof String) {
62-
LocalDateTime localDateTime = parseDate((String) input);
61+
if (input instanceof String string) {
62+
LocalDateTime localDateTime = parseDate(string);
6363

6464
if (localDateTime != null) {
6565
return DateTimeHelper.toDate(localDateTime);
6666
}
67-
} else if (input instanceof Date) {
68-
return (Date) input;
67+
} else if (input instanceof Date date) {
68+
return date;
6969
}
7070
return null;
7171
}
7272

7373
@Override
7474
public String serialize(Object input) {
75-
if (input instanceof Date) {
76-
return DateTimeHelper.toISOString((Date) input);
75+
if (input instanceof Date date) {
76+
return DateTimeHelper.toISOString(date);
7777
} else {
7878
Date result = convertImpl(input);
7979
if (result == null) {
@@ -94,6 +94,10 @@ public Date parseValue(Object input) {
9494

9595
@Override
9696
public Date parseLiteral(Object input) {
97+
if (!(input instanceof StringValue)) {
98+
throw new CoercingParseLiteralException("Expected AST type 'StringValue' but was '" + CoercingUtil.typeName(input) + "'.");
99+
}
100+
97101
String value = ((StringValue) input).getValue();
98102
Date result = convertImpl(value);
99103
if (result == null) {
@@ -123,4 +127,8 @@ private LocalDateTime parseDate(String date) {
123127
return null;
124128
}
125129

130+
private String getErrorMessage(Object input) {
131+
return "Invalid value '" + input + "' for Date";
132+
}
133+
126134
}

graphql-java-datetime/src/main/java/com/tailrocks/graphql/datetime/GraphqlDurationCoercing.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,10 @@ public Duration parseValue(Object input) {
7272

7373
@Override
7474
public Duration parseLiteral(Object input) {
75+
if (!(input instanceof StringValue)) {
76+
throw new CoercingParseLiteralException("Expected AST type 'StringValue' but was '" + CoercingUtil.typeName(input) + "'.");
77+
}
78+
7579
String value = ((StringValue) input).getValue();
7680
Duration result = convertImpl(value);
7781
if (result == null) {

graphql-java-datetime/src/main/java/com/tailrocks/graphql/datetime/GraphqlLocalDateCoercing.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,10 @@ public LocalDate parseValue(Object input) {
7777

7878
@Override
7979
public LocalDate parseLiteral(Object input) {
80+
if (!(input instanceof StringValue)) {
81+
throw new CoercingParseLiteralException("Expected AST type 'StringValue' but was '" + CoercingUtil.typeName(input) + "'.");
82+
}
83+
8084
String value = ((StringValue) input).getValue();
8185
LocalDate result = convertImpl(value);
8286
if (result == null) {

graphql-java-datetime/src/main/java/com/tailrocks/graphql/datetime/GraphqlLocalDateTimeCoercing.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,10 @@ public LocalDateTime parseValue(Object input) {
7676

7777
@Override
7878
public LocalDateTime parseLiteral(Object input) {
79+
if (!(input instanceof StringValue)) {
80+
throw new CoercingParseLiteralException("Expected AST type 'StringValue' but was '" + CoercingUtil.typeName(input) + "'.");
81+
}
82+
7983
String value = ((StringValue) input).getValue();
8084
LocalDateTime result = convertImpl(value);
8185
if (result == null) {

graphql-java-datetime/src/main/java/com/tailrocks/graphql/datetime/GraphqlLocalTimeCoercing.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,10 @@ public LocalTime parseValue(Object input) {
7272

7373
@Override
7474
public LocalTime parseLiteral(Object input) {
75+
if (!(input instanceof StringValue)) {
76+
throw new CoercingParseLiteralException("Expected AST type 'StringValue' but was '" + CoercingUtil.typeName(input) + "'.");
77+
}
78+
7579
String value = ((StringValue) input).getValue();
7680
LocalTime result = convertImpl(value);
7781
if (result == null) {

graphql-java-datetime/src/main/java/com/tailrocks/graphql/datetime/GraphqlOffsetDateTimeCoercing.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,10 @@ public OffsetDateTime parseValue(Object input) {
6969

7070
@Override
7171
public OffsetDateTime parseLiteral(Object input) {
72+
if (!(input instanceof StringValue)) {
73+
throw new CoercingParseLiteralException("Expected AST type 'StringValue' but was '" + CoercingUtil.typeName(input) + "'.");
74+
}
75+
7276
String value = ((StringValue) input).getValue();
7377
OffsetDateTime result = convertImpl(value);
7478
if (result == null) {

0 commit comments

Comments
 (0)