package org.crygier.graphql;

import java.math.BigInteger;
import java.text.DateFormat;
import java.text.ParseException;
import java.util.Date;

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

public class JavaScalars {
    public static GraphQLScalarType GraphQLDate = new GraphQLScalarType("Date", "Date type", new Coercing() {
        @Override
        public Object serialize(Object input) {
            if (input instanceof String) {
                try {
                    return DateFormat.getInstance().parse((String)input);
                } catch (ParseException e) {
                    throw new IllegalArgumentException(e.getMessage());
                }
            } else if (input instanceof Date) {
                return input;
            } else if (input instanceof Long) {
                return new Date(((Long) input));
            } else if (input instanceof Integer) {
                return new Date(((Integer) input).longValue());
            } else {
                return null;
            }
        }

        @Override
        public Object parseValue(Object input) {
            return serialize(input);
        }

        @Override
        public Object parseLiteral(Object input) {
            if (input instanceof StringValue) {
                try {
                    return DateFormat.getInstance().parse(((StringValue) input).getValue());
                } catch (ParseException e) {
                    throw new IllegalArgumentException(e.getMessage());
                }
            } else if (input instanceof IntValue) {
                BigInteger value = ((IntValue) input).getValue();
                return new Date(value.longValue());
            }
            return null;
        }
    });
}

@@ -286,35 +286,167 @@
 pe;%0A
-%0Apublic class JavaScalars %7B
+import org.slf4j.Logger;%0Aimport org.slf4j.LoggerFactory;%0A%0Apublic class JavaScalars %7B%0A    static final Logger log = LoggerFactory.getLogger(JavaScalars.class);%0A
 %0A   
@@ -681,222 +681,49 @@
     
-try %7B%0A                    return DateFormat.getInstance().
+return 
 parse
-((
 String
-)input);%0A                %7D catch (ParseException e) %7B%0A                    throw new IllegalArgumentException(e.getMessage());%0A                %7D
+ToDate((String) input);
 %0A   
@@ -891,16 +891,28 @@
 ) input)
+.longValue()
 );%0A     
@@ -1038,20 +1038,9 @@
    %7D
- else %7B%0A    
+%0A
     
@@ -1060,30 +1060,16 @@
 n null;%0A
-            %7D%0A
         
@@ -1323,70 +1323,31 @@
     
-try %7B%0A                    return DateFormat.getInstance().pars
+return parseStringToDat
 e(((
@@ -1395,36 +1395,43 @@
     
-    %7D catch (ParseException 
+%7D else if (input instanceof IntValu
 e) %7B
@@ -1451,62 +1451,56 @@
     
-    
-throw new IllegalArgumentException(e.getMessag
+BigInteger value = ((IntValue) input).getValu
 e()
-)
 ;%0A  
@@ -1517,61 +1517,82 @@
     
-%7D%0A            %7D else if (input instanceof IntValue) %7B
+return new Date(value.longValue());%0A            %7D%0A            return null;
 %0A   
@@ -1592,32 +1592,35 @@
 ll;%0A        
+%7D%0A%0A
         
 BigInteger v
@@ -1611,124 +1611,254 @@
     
-BigInteger value = ((IntValue) input).getValue();%0A                return new Date(value.longValue());%0A            %7D%0A
+private Date parseStringToDate(String input) %7B%0A            try %7B%0A                return DateFormat.getInstance().parse(input);%0A            %7D catch (ParseException e) %7B%0A                log.warn(%22Failed to parse Date from input: %22 + input, e);%0A    
     
@@ -1870,32 +1870,46 @@
    return null;%0A
+            %7D%0A
         %7D%0A    %7D)

Return null on parse errors as per specification; Do not throw Exception