All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.spring.boxes.dollar.support.graphql.scalar.DateScalar Maven / Gradle / Ivy

The newest version!
package com.spring.boxes.dollar.support.graphql.scalar;

import static com.spring.boxes.dollar.TimeUtils.PATTERN_DATETIME;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;

import com.spring.boxes.dollar.TimeUtils;

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;

@Slf4j
public class DateScalar implements Coercing {

    @Override
    public Date serialize(Object dataFetcherResult) throws CoercingSerializeException {
        log.debug("[DateScalar] input.serialize: {}", dataFetcherResult.toString());
        if (dataFetcherResult instanceof LocalDateTime) {
            return TimeUtils.from(((LocalDateTime) dataFetcherResult));
        } else {
            throw new CoercingSerializeException("Not a valid LocalDateTime");
        }
    }

    @Override
    public LocalDateTime parseValue(Object input) throws CoercingParseValueException {
        log.debug("[DateScalar] input.parseValue: {}", input.toString());
        return LocalDateTime.parse(input.toString(), DateTimeFormatter.ofPattern(PATTERN_DATETIME));
    }

    @Override
    public LocalDateTime parseLiteral(Object input) throws CoercingParseLiteralException {
        log.debug("[DateScalar] input: {}", input.toString());
        if (input instanceof StringValue) {
            return LocalDateTime.parse(((StringValue) input).getValue(), DateTimeFormatter.ofPattern(PATTERN_DATETIME));
        }

        throw new CoercingParseLiteralException("Value is not a valid date");
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy