package mil.dds.anet.graphql;

import java.math.BigInteger;

import org.joda.time.DateTime;

import graphql.language.IntValue;
import graphql.schema.Coercing;
import graphql.schema.GraphQLScalarType;

public class GraphQLDateTimeType extends GraphQLScalarType {

	private static final Coercing<DateTime, Long> coercing = new Coercing<DateTime, Long>() {
		@Override
		public Long serialize(Object input) {
			return ((DateTime) input).getMillis();
		}

		@Override
		public DateTime parseValue(Object input) {
			if (input instanceof Long) {
				return new DateTime((Long)input);
			} else {
				return new DateTime(Long.parseLong(input.toString()));
			}
		}

		@Override
		public DateTime parseLiteral(Object input) {
			if (input.getClass().equals(IntValue.class)) {
				BigInteger value = ((IntValue) input).getValue();
				return new DateTime(value.longValue());
			}
			throw new RuntimeException("Unexpected input, expected Unix Millis as long");
		}
	};

	public GraphQLDateTimeType() {
		super("DateTime", null, coercing);
	}

}

@@ -84,16 +84,63 @@
 ateTime;
+%0Aimport org.joda.time.format.ISODateTimeFormat;
 %0A%0Aimport
@@ -624,17 +624,145 @@
 t);%0A%09%09%09%7D
- 
+%0A%09%09%09else if (input instanceof String) %7B%0A%09%09%09%09return DateTime.parse(input.toString(), ISODateTimeFormat.dateTimeParser());%0A%09%09%09%7D%0A%09%09%09
 else %7B%0A%09

Make GraphQLDateTimeType also parse string values