GraphQL API and utilities for the rpdata project
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

28 lines
619 B

package scalars
import (
"fmt"
"io"
"time"
"github.com/99designs/gqlgen/graphql"
)
// MarshalDate marshals time into gql Date type
func MarshalDate(t time.Time) graphql.Marshaler {
return graphql.WriterFunc(func(w io.Writer) {
w.Write([]byte(`"` + t.Format(time.RFC3339Nano) + `"`))
})
}
// UnmarshalDate unmarshals time from gql Date type
func UnmarshalDate(v interface{}) (time.Time, error) {
switch v := v.(type) {
case string:
return time.Parse(time.RFC3339Nano, v)
case int:
return time.Unix(0, int64(v)*1000000), nil
default:
return time.Time{}, fmt.Errorf("%T is not a valid time", v)
}
}