com.spring.boxes.dollar.support.graphql.scalar.LocalTimeScalar Maven / Gradle / Ivy
The newest version!
package com.spring.boxes.dollar.support.graphql.scalar;
import graphql.language.StringValue;
import graphql.schema.Coercing;
import graphql.schema.CoercingParseLiteralException;
import graphql.schema.CoercingParseValueException;
import graphql.schema.CoercingSerializeException;
import lombok.extern.slf4j.Slf4j;
import java.time.LocalDate;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import static com.spring.boxes.dollar.JSONUtils.DEFAULT_TIME_PATTERN;
@Slf4j
public class LocalTimeScalar implements Coercing {
@Override
public LocalTime serialize(Object dataFetcherResult) throws CoercingSerializeException {
log.debug("[LocalTime] input.serialize:{}", dataFetcherResult.toString());
if (dataFetcherResult instanceof LocalTime) {
return ((LocalTime) dataFetcherResult);
} else {
throw new CoercingSerializeException("Not a valid LocalTime");
}
}
@Override
public LocalTime parseValue(Object input) throws CoercingParseValueException {
log.debug("[LocalTime] input.parseValue:{}", input.toString());
return LocalTime.parse(input.toString(), DateTimeFormatter.ofPattern(DEFAULT_TIME_PATTERN));
}
@Override
public LocalTime parseLiteral(Object input) throws CoercingParseLiteralException {
log.debug("[LocalTime] input: {}", input.toString());
if (input instanceof StringValue) {
return LocalTime.parse(((StringValue) input).getValue(), DateTimeFormatter.ofPattern(DEFAULT_TIME_PATTERN));
}
throw new CoercingParseLiteralException("Value is not a valid ISO date time");
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy