-
Notifications
You must be signed in to change notification settings - Fork 28
Closed
Description
Needed this so wrote my own (pasted below). I also was unable to use this library because graphql-java doesn't allow clients to change the scalar name (I wanted a Java LocalDate
to be called just Date
as a scalar in graphql).
This more closely matches the behavior I expect https://www.npmjs.com/package/graphql-iso-date to have wrt its Date
and DateTime
scalars.
// example: 2007-12-03T10:15:30+01:00
// example: 2007-12-03T10:15:30Z
public static final GraphQLScalarType OFFSET_DATE_TIME_TYPE = new GraphQLScalarType("Timestamp",
"A Java OffsetDateTime",
new Coercing<OffsetDateTime, String>() {
private OffsetDateTime convertImpl(Object input) {
if (input instanceof String) {
try {
return OffsetDateTime.parse((String) input, DateTimeFormatter.ISO_OFFSET_DATE_TIME);
} catch (DateTimeParseException ignored) {
return null;
}
}
return null;
}
@Override
public String serialize(Object input) {
if (input instanceof OffsetDateTime) {
return DateTimeFormatter.ISO_OFFSET_DATE_TIME.format((OffsetDateTime) input);
} else {
OffsetDateTime result = convertImpl(input);
if (result == null) {
throw new CoercingSerializeException(
"Invalid value '" + input + "' for Timestamp (a Java OffsetDateTime)");
}
return DateTimeFormatter.ISO_OFFSET_DATE_TIME.format(result);
}
}
@Override
public OffsetDateTime parseValue(Object input) {
OffsetDateTime result = convertImpl(input);
if (result == null) {
throw new CoercingParseValueException(
"Invalid value '" + input + "' for Timestamp (a Java OffsetDateTime)");
}
return result;
}
@Override
public OffsetDateTime parseLiteral(Object input) {
if (!(input instanceof StringValue)) {
return null;
}
String value = ((StringValue) input).getValue();
OffsetDateTime result = convertImpl(value);
return result;
}
});
// example: 2012-01-05
public static final GraphQLScalarType LOCAL_DATE_TYPE = new GraphQLScalarType("Date", "A Java LocalDate",
new Coercing<LocalDate, String>() {
private LocalDate convertImpl(Object input) {
if (input instanceof String) {
try {
return LocalDate.parse((String) input, DateTimeFormatter.ISO_LOCAL_DATE);
} catch (DateTimeParseException ignored) {
return null;
}
}
return null;
}
@Override
public String serialize(Object input) {
if (input instanceof LocalDate) {
return DateTimeFormatter.ISO_LOCAL_DATE.format((LocalDate) input);
} else {
LocalDate result = convertImpl(input);
if (result == null) {
throw new CoercingSerializeException(
"Invalid value '" + input + "' for Date (a Java LocalDate)");
}
return DateTimeFormatter.ISO_LOCAL_DATE.format(result);
}
}
@Override
public LocalDate parseValue(Object input) {
LocalDate result = convertImpl(input);
if (result == null) {
throw new CoercingParseValueException(
"Invalid value '" + input + "' for Date (a Java LocalDate)");
}
return result;
}
@Override
public LocalDate parseLiteral(Object input) {
if (!(input instanceof StringValue)) {
return null;
}
String value = ((StringValue) input).getValue();
LocalDate result = convertImpl(value);
return result;
}
});
ieugen
Metadata
Metadata
Assignees
Labels
No labels