Skip to content

Commit

Permalink
Marshaling & Unmarshaling time return initial value
Browse files Browse the repository at this point in the history
There was a lack of symmetry that would prevent times for being
symmetrical. That is because time.Parse actually parses an RFC3339Nano
implicitly, thereby allowing nanosecond resolution on unmarshaling a
time. Therefore we now marshal into nanoseconds, getting more
information into GraphQL times when querying for a time, and restoring
the symmetry
  • Loading branch information
frankywahl committed Apr 13, 2021
1 parent f4bf1f5 commit 481a4e4
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
2 changes: 1 addition & 1 deletion graphql/time.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ func MarshalTime(t time.Time) Marshaler {
}

return WriterFunc(func(w io.Writer) {
io.WriteString(w, strconv.Quote(t.Format(time.RFC3339)))
io.WriteString(w, strconv.Quote(t.Format(time.RFC3339Nano)))
})
}

Expand Down
25 changes: 25 additions & 0 deletions graphql/time_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package graphql

import (
"bytes"
"strconv"
"testing"
"time"

"github.com/stretchr/testify/require"
)

func TestTime(t *testing.T) {
t.Run("symmetry", func(t *testing.T) {
initialTime := time.Now()
buf := bytes.NewBuffer([]byte{})
MarshalTime(initialTime).MarshalGQL(buf)

str, err := strconv.Unquote(buf.String())
require.Nil(t, err)
newTime, err := UnmarshalTime(str)
require.Nil(t, err)

require.True(t, initialTime.Equal(newTime), "expected times %v and %v to equal", initialTime, newTime)
})
}

0 comments on commit 481a4e4

Please sign in to comment.