stufflog graphql server
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.

30 lines
656 B

  1. package scalars
  2. import (
  3. "fmt"
  4. "github.com/99designs/gqlgen/graphql"
  5. "io"
  6. "strconv"
  7. "time"
  8. )
  9. func MarshalDuration(d time.Duration) graphql.Marshaler {
  10. return graphql.WriterFunc(func(w io.Writer) {
  11. _, _ = w.Write([]byte(strconv.Itoa(int(d.Milliseconds()))))
  12. })
  13. }
  14. func UnmarshalDuration(v interface{}) (time.Duration, error) {
  15. switch v := v.(type) {
  16. case string:
  17. return time.ParseDuration(v)
  18. case int:
  19. return time.Millisecond * time.Duration(v), nil
  20. case int64:
  21. return time.Millisecond * time.Duration(v), nil
  22. case float64:
  23. return time.Millisecond * time.Duration(v), nil
  24. default:
  25. return 0, fmt.Errorf("%T is not a bool", v)
  26. }
  27. }