Skip to content

Commit c548f50

Browse files
committed
More test code plus URLs
1 parent feea5be commit c548f50

15 files changed

+651
-236
lines changed

build.gradle

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ repositories {
1919

2020
dependencies {
2121
compile "com.graphql-java:graphql-java:10.0"
22-
compile "com.ethlo.time:itu:1.2"
22+
compile "com.squareup.okhttp3:okhttp:3.2.0"
2323

2424
testCompile 'org.spockframework:spock-core:1.1-groovy-2.4'
2525
testCompile 'org.codehaus.groovy:groovy-all:2.4.13'

settings.gradle

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
rootProject.name = 'graphql-java-scalars'
1+
rootProject.name = 'graphql-java-extended-scalars'

src/main/java/graphql/scalars/ExtendedScalars.java

+3
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import graphql.scalars.datetime.FullTimeScalar;
77
import graphql.scalars.object.JsonScalar;
88
import graphql.scalars.object.ObjectScalar;
9+
import graphql.scalars.url.UrlScalar;
910
import graphql.schema.GraphQLScalarType;
1011

1112
@PublicApi
@@ -55,4 +56,6 @@ public class ExtendedScalars {
5556
* </pre>
5657
*/
5758
public static GraphQLScalarType Json = new JsonScalar();
59+
60+
public static GraphQLScalarType Url = new UrlScalar();
5861
}

src/main/java/graphql/scalars/datetime/DateTimeScalar.java

+54-47
Original file line numberDiff line numberDiff line change
@@ -20,59 +20,66 @@
2020
@Internal
2121
public class DateTimeScalar extends GraphQLScalarType {
2222

23-
24-
static Coercing<OffsetDateTime, String> RFC3339_DATETIME_COERCING = new Coercing<OffsetDateTime, String>() {
25-
@Override
26-
public String serialize(Object input) throws CoercingSerializeException {
27-
OffsetDateTime offsetDateTime;
28-
if (input instanceof OffsetDateTime) {
29-
offsetDateTime = (OffsetDateTime) input;
30-
} else if (input instanceof ZonedDateTime) {
31-
offsetDateTime = ((ZonedDateTime) input).toOffsetDateTime();
32-
} else {
33-
throw new CoercingParseValueException(
34-
"Expected something we can convert to 'java.time.OffsetDateTime' but was '" + typeName(input) + "'."
35-
);
36-
}
37-
try {
38-
return DateTimeFormatter.ISO_OFFSET_DATE_TIME.format(offsetDateTime);
39-
} catch (DateTimeException e) {
40-
throw new CoercingSerializeException(
41-
"Unable to turn TemporalAccessor into OffsetDateTime because of : '" + e.getMessage() + "'."
42-
);
23+
public DateTimeScalar() {
24+
super("DateTime", "An RFC-3339 compliant DateTime Scalar", new Coercing<OffsetDateTime, String>() {
25+
@Override
26+
public String serialize(Object input) throws CoercingSerializeException {
27+
OffsetDateTime offsetDateTime;
28+
if (input instanceof OffsetDateTime) {
29+
offsetDateTime = (OffsetDateTime) input;
30+
} else if (input instanceof ZonedDateTime) {
31+
offsetDateTime = ((ZonedDateTime) input).toOffsetDateTime();
32+
} else if (input instanceof String) {
33+
offsetDateTime = parseOffsetDateTime(input.toString(), CoercingSerializeException::new);
34+
} else {
35+
throw new CoercingSerializeException(
36+
"Expected something we can convert to 'java.time.OffsetDateTime' but was '" + typeName(input) + "'."
37+
);
38+
}
39+
try {
40+
return DateTimeFormatter.ISO_OFFSET_DATE_TIME.format(offsetDateTime);
41+
} catch (DateTimeException e) {
42+
throw new CoercingSerializeException(
43+
"Unable to turn TemporalAccessor into OffsetDateTime because of : '" + e.getMessage() + "'."
44+
);
45+
}
4346
}
44-
}
4547

46-
@Override
47-
public OffsetDateTime parseValue(Object input) throws CoercingParseValueException {
48-
if (!(input instanceof String)) {
49-
throw new CoercingParseValueException(
50-
"Expected a 'String' but was '" + typeName(input) + "'."
51-
);
48+
@Override
49+
public OffsetDateTime parseValue(Object input) throws CoercingParseValueException {
50+
OffsetDateTime offsetDateTime;
51+
if (input instanceof OffsetDateTime) {
52+
offsetDateTime = (OffsetDateTime) input;
53+
} else if (input instanceof ZonedDateTime) {
54+
offsetDateTime = ((ZonedDateTime) input).toOffsetDateTime();
55+
} else if (input instanceof String) {
56+
offsetDateTime = parseOffsetDateTime(input.toString(), CoercingParseValueException::new);
57+
} else {
58+
throw new CoercingParseValueException(
59+
"Expected a 'String' but was '" + typeName(input) + "'."
60+
);
61+
}
62+
return offsetDateTime;
5263
}
53-
return parseStr(input.toString(), CoercingParseValueException::new);
54-
}
5564

56-
@Override
57-
public OffsetDateTime parseLiteral(Object input) throws CoercingParseLiteralException {
58-
if (!(input instanceof StringValue)) {
59-
throw new CoercingParseLiteralException(
60-
"Expected AST type 'StringValue' but was '" + typeName(input) + "'."
61-
);
65+
@Override
66+
public OffsetDateTime parseLiteral(Object input) throws CoercingParseLiteralException {
67+
if (!(input instanceof StringValue)) {
68+
throw new CoercingParseLiteralException(
69+
"Expected AST type 'StringValue' but was '" + typeName(input) + "'."
70+
);
71+
}
72+
return parseOffsetDateTime(((StringValue) input).getValue(), CoercingParseLiteralException::new);
6273
}
63-
return parseStr(((StringValue) input).getValue(), CoercingParseLiteralException::new);
64-
}
6574

66-
private OffsetDateTime parseStr(String s, Function<String, RuntimeException> exceptionMaker) {
67-
try {
68-
return OffsetDateTime.parse(s, DateTimeFormatter.ISO_OFFSET_DATE_TIME);
69-
} catch (DateTimeParseException e) {
70-
throw exceptionMaker.apply("Invalid RFC3339 value : '" + s + "'. because of : '" + e.getMessage() + "'");
75+
private OffsetDateTime parseOffsetDateTime(String s, Function<String, RuntimeException> exceptionMaker) {
76+
try {
77+
return OffsetDateTime.parse(s, DateTimeFormatter.ISO_OFFSET_DATE_TIME);
78+
} catch (DateTimeParseException e) {
79+
throw exceptionMaker.apply("Invalid RFC3339 value : '" + s + "'. because of : '" + e.getMessage() + "'");
80+
}
7181
}
72-
}
73-
};
74-
75-
public DateTimeScalar() {
76-
super("DateTime", "An RFC-3339 compliant DateTime Scalar", RFC3339_DATETIME_COERCING);
82+
});
7783
}
84+
7885
}

src/main/java/graphql/scalars/datetime/FullDateScalar.java

+57-43
Original file line numberDiff line numberDiff line change
@@ -23,55 +23,69 @@ public class FullDateScalar extends GraphQLScalarType {
2323
private final static DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
2424
;
2525

26-
static Coercing<LocalDate, String> RFC3339_DATE_COERCING = new Coercing<LocalDate, String>() {
27-
@Override
28-
public String serialize(Object input) throws CoercingSerializeException {
29-
if (!(input instanceof TemporalAccessor)) {
30-
throw new CoercingParseValueException(
31-
"Expected a 'java.time.temporal.TemporalAccessor' but was '" + typeName(input) + "'."
32-
);
33-
}
34-
TemporalAccessor temporalAccessor = (TemporalAccessor) input;
35-
try {
36-
return dateFormatter.format(temporalAccessor);
37-
} catch (DateTimeException e) {
38-
throw new CoercingSerializeException(
39-
"Unable to turn TemporalAccessor into full date because of : '" + e.getMessage() + "'."
40-
);
26+
public FullDateScalar() {
27+
super("Date", "An RFC-3339 compliant Full Date Scalar", new Coercing<LocalDate, String>() {
28+
@Override
29+
public String serialize(Object input) throws CoercingSerializeException {
30+
TemporalAccessor temporalAccessor;
31+
if (input instanceof TemporalAccessor) {
32+
temporalAccessor = (TemporalAccessor) input;
33+
} else if (input instanceof String) {
34+
temporalAccessor = parseLocalDate(input.toString(), CoercingSerializeException::new);
35+
} else {
36+
throw new CoercingSerializeException(
37+
"Expected a 'String' or 'java.time.temporal.TemporalAccessor' but was '" + typeName(input) + "'."
38+
);
39+
}
40+
try {
41+
return dateFormatter.format(temporalAccessor);
42+
} catch (DateTimeException e) {
43+
throw new CoercingSerializeException(
44+
"Unable to turn TemporalAccessor into full date because of : '" + e.getMessage() + "'."
45+
);
46+
}
4147
}
42-
}
4348

44-
@Override
45-
public LocalDate parseValue(Object input) throws CoercingParseValueException {
46-
if (!(input instanceof String)) {
47-
throw new CoercingParseValueException(
48-
"Expected a 'String' but was '" + typeName(input) + "'."
49-
);
49+
@Override
50+
public LocalDate parseValue(Object input) throws CoercingParseValueException {
51+
TemporalAccessor temporalAccessor;
52+
if (input instanceof TemporalAccessor) {
53+
temporalAccessor = (TemporalAccessor) input;
54+
} else if (input instanceof String) {
55+
temporalAccessor = parseLocalDate(input.toString(), CoercingParseValueException::new);
56+
} else {
57+
throw new CoercingParseValueException(
58+
"Expected a 'String' or 'java.time.temporal.TemporalAccessor' but was '" + typeName(input) + "'."
59+
);
60+
}
61+
try {
62+
return LocalDate.from(temporalAccessor);
63+
} catch (DateTimeException e) {
64+
throw new CoercingParseValueException(
65+
"Unable to turn TemporalAccessor into full date because of : '" + e.getMessage() + "'."
66+
);
67+
}
5068
}
51-
return parseStr(input.toString(), CoercingParseValueException::new);
52-
}
5369

54-
@Override
55-
public LocalDate parseLiteral(Object input) throws CoercingParseLiteralException {
56-
if (!(input instanceof StringValue)) {
57-
throw new CoercingParseLiteralException(
58-
"Expected AST type 'StringValue' but was '" + typeName(input) + "'."
59-
);
70+
@Override
71+
public LocalDate parseLiteral(Object input) throws CoercingParseLiteralException {
72+
if (!(input instanceof StringValue)) {
73+
throw new CoercingParseLiteralException(
74+
"Expected AST type 'StringValue' but was '" + typeName(input) + "'."
75+
);
76+
}
77+
return parseLocalDate(((StringValue) input).getValue(), CoercingParseLiteralException::new);
6078
}
61-
return parseStr(((StringValue) input).getValue(), CoercingParseLiteralException::new);
62-
}
6379

64-
private LocalDate parseStr(String s, Function<String, RuntimeException> exceptionMaker) {
65-
try {
66-
TemporalAccessor temporalAccessor = dateFormatter.parse(s);
67-
return LocalDate.from(temporalAccessor);
68-
} catch (DateTimeParseException e) {
69-
throw exceptionMaker.apply("Invalid RFC3339 full date value : '" + s + "'. because of : '" + e.getMessage() + "'");
80+
private LocalDate parseLocalDate(String s, Function<String, RuntimeException> exceptionMaker) {
81+
try {
82+
TemporalAccessor temporalAccessor = dateFormatter.parse(s);
83+
return LocalDate.from(temporalAccessor);
84+
} catch (DateTimeParseException e) {
85+
throw exceptionMaker.apply("Invalid RFC3339 full date value : '" + s + "'. because of : '" + e.getMessage() + "'");
86+
}
7087
}
71-
}
72-
};
73-
74-
public FullDateScalar() {
75-
super("Date", "An RFC-3339 compliant Full Date Scalar", RFC3339_DATE_COERCING);
88+
});
7689
}
90+
7791
}

src/main/java/graphql/scalars/datetime/FullTimeScalar.java

+57-44
Original file line numberDiff line numberDiff line change
@@ -21,57 +21,70 @@
2121
public class FullTimeScalar extends GraphQLScalarType {
2222

2323
private final static DateTimeFormatter dateFormatter = DateTimeFormatter.ISO_OFFSET_TIME;
24-
;
2524

26-
static Coercing<OffsetTime, String> RFC3339_TIME_COERCING = new Coercing<OffsetTime, String>() {
27-
@Override
28-
public String serialize(Object input) throws CoercingSerializeException {
29-
if (!(input instanceof TemporalAccessor)) {
30-
throw new CoercingParseValueException(
31-
"Expected a 'java.time.temporal.TemporalAccessor' but was '" + typeName(input) + "'."
32-
);
33-
}
34-
TemporalAccessor temporalAccessor = (TemporalAccessor) input;
35-
try {
36-
return dateFormatter.format(temporalAccessor);
37-
} catch (DateTimeException e) {
38-
throw new CoercingSerializeException(
39-
"Unable to turn TemporalAccessor into full time because of : '" + e.getMessage() + "'."
40-
);
25+
public FullTimeScalar() {
26+
super("Time", "An RFC-3339 compliant Full Time Scalar", new Coercing<OffsetTime, String>() {
27+
@Override
28+
public String serialize(Object input) throws CoercingSerializeException {
29+
TemporalAccessor temporalAccessor;
30+
if (input instanceof TemporalAccessor) {
31+
temporalAccessor = (TemporalAccessor) input;
32+
} else if (input instanceof String) {
33+
temporalAccessor = parseOffsetTime(input.toString(), CoercingSerializeException::new);
34+
} else {
35+
throw new CoercingSerializeException(
36+
"Expected a 'String' or 'java.time.temporal.TemporalAccessor' but was '" + typeName(input) + "'."
37+
);
38+
}
39+
try {
40+
return dateFormatter.format(temporalAccessor);
41+
} catch (DateTimeException e) {
42+
throw new CoercingSerializeException(
43+
"Unable to turn TemporalAccessor into full time because of : '" + e.getMessage() + "'."
44+
);
45+
}
4146
}
42-
}
4347

44-
@Override
45-
public OffsetTime parseValue(Object input) throws CoercingParseValueException {
46-
if (!(input instanceof String)) {
47-
throw new CoercingParseValueException(
48-
"Expected a 'String' but was '" + typeName(input) + "'."
49-
);
48+
@Override
49+
public OffsetTime parseValue(Object input) throws CoercingParseValueException {
50+
TemporalAccessor temporalAccessor;
51+
if (input instanceof TemporalAccessor) {
52+
temporalAccessor = (TemporalAccessor) input;
53+
} else if (input instanceof String) {
54+
temporalAccessor = parseOffsetTime(input.toString(), CoercingParseValueException::new);
55+
} else {
56+
throw new CoercingParseValueException(
57+
"Expected a 'String' or 'java.time.temporal.TemporalAccessor' but was '" + typeName(input) + "'."
58+
);
59+
}
60+
try {
61+
return OffsetTime.from(temporalAccessor);
62+
} catch (DateTimeException e) {
63+
throw new CoercingParseValueException(
64+
"Unable to turn TemporalAccessor into full time because of : '" + e.getMessage() + "'."
65+
);
66+
}
5067
}
51-
return parseStr(input.toString(), CoercingParseValueException::new);
52-
}
5368

54-
@Override
55-
public OffsetTime parseLiteral(Object input) throws CoercingParseLiteralException {
56-
if (!(input instanceof StringValue)) {
57-
throw new CoercingParseLiteralException(
58-
"Expected AST type 'StringValue' but was '" + typeName(input) + "'."
59-
);
69+
@Override
70+
public OffsetTime parseLiteral(Object input) throws CoercingParseLiteralException {
71+
if (!(input instanceof StringValue)) {
72+
throw new CoercingParseLiteralException(
73+
"Expected AST type 'StringValue' but was '" + typeName(input) + "'."
74+
);
75+
}
76+
return parseOffsetTime(((StringValue) input).getValue(), CoercingParseLiteralException::new);
6077
}
61-
return parseStr(((StringValue) input).getValue(), CoercingParseLiteralException::new);
62-
}
6378

64-
private OffsetTime parseStr(String s, Function<String, RuntimeException> exceptionMaker) {
65-
try {
66-
TemporalAccessor temporalAccessor = dateFormatter.parse(s);
67-
return OffsetTime.from(temporalAccessor);
68-
} catch (DateTimeParseException e) {
69-
throw exceptionMaker.apply("Invalid RFC3339 full time value : '" + s + "'. because of : '" + e.getMessage() + "'");
79+
private OffsetTime parseOffsetTime(String s, Function<String, RuntimeException> exceptionMaker) {
80+
try {
81+
TemporalAccessor temporalAccessor = dateFormatter.parse(s);
82+
return OffsetTime.from(temporalAccessor);
83+
} catch (DateTimeParseException e) {
84+
throw exceptionMaker.apply("Invalid RFC3339 full time value : '" + s + "'. because of : '" + e.getMessage() + "'");
85+
}
7086
}
71-
}
72-
};
73-
74-
public FullTimeScalar() {
75-
super("Time", "An RFC-3339 compliant Full Time Scalar", RFC3339_TIME_COERCING);
87+
});
7688
}
89+
7790
}

src/main/java/graphql/scalars/object/JsonScalar.java

+3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
package graphql.scalars.object;
22

3+
import graphql.Internal;
4+
35
/**
46
* A synonym class for {@link graphql.scalars.object.ObjectScalar}
57
*/
8+
@Internal
69
public class JsonScalar extends ObjectScalar {
710
public JsonScalar() {
811
super("JSON", "A JSON scalar");

0 commit comments

Comments
 (0)