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

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

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

import static com.spring.boxes.dollar.JSONUtils.DEFAULT_DATE_TIME_PATTERN;

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

import com.spring.boxes.dollar.JSONUtils;

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 LocalDateTimeScalar implements Coercing {

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

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

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

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




© 2015 - 2024 Weber Informatics LLC | Privacy Policy