Skip to content

Commit 993e01c

Browse files
authored
Merge pull request #419 from ccbrown/fix-date-time-literal-parsing
Fix DateTime literal parsing
2 parents a7e15c0 + be84dc7 commit 993e01c

File tree

3 files changed

+55
-28
lines changed

3 files changed

+55
-28
lines changed

scalars.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -561,7 +561,7 @@ var DateTime = NewScalar(ScalarConfig{
561561
ParseLiteral: func(valueAST ast.Value) interface{} {
562562
switch valueAST := valueAST.(type) {
563563
case *ast.StringValue:
564-
return valueAST.Value
564+
return unserializeDateTime(valueAST.Value)
565565
}
566566
return nil
567567
},

scalars_parse_test.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package graphql_test
2+
3+
import (
4+
"reflect"
5+
"testing"
6+
"time"
7+
8+
"github.com/graphql-go/graphql"
9+
"github.com/graphql-go/graphql/language/ast"
10+
)
11+
12+
func TestTypeSystem_Scalar_ParseValueOutputDateTime(t *testing.T) {
13+
t1, _ := time.Parse(time.RFC3339, "2017-07-23T03:46:56.647Z")
14+
tests := []dateTimeSerializationTest{
15+
{nil, nil},
16+
{"", nil},
17+
{(*string)(nil), nil},
18+
{"2017-07-23", nil},
19+
{"2017-07-23T03:46:56.647Z", t1},
20+
}
21+
for _, test := range tests {
22+
val := graphql.DateTime.ParseValue(test.Value)
23+
if val != test.Expected {
24+
reflectedValue := reflect.ValueOf(test.Value)
25+
t.Fatalf("failed DateTime.ParseValue(%v(%v)), expected: %v, got %v", reflectedValue.Type(), test.Value, test.Expected, val)
26+
}
27+
}
28+
}
29+
30+
func TestTypeSystem_Scalar_ParseLiteralOutputDateTime(t *testing.T) {
31+
t1, _ := time.Parse(time.RFC3339, "2017-07-23T03:46:56.647Z")
32+
for name, testCase := range map[string]struct {
33+
Literal ast.Value
34+
Expected interface{}
35+
}{
36+
"String": {
37+
Literal: &ast.StringValue{
38+
Value: "2017-07-23T03:46:56.647Z",
39+
},
40+
Expected: t1,
41+
},
42+
"NotAString": {
43+
Literal: &ast.IntValue{},
44+
Expected: nil,
45+
},
46+
} {
47+
t.Run(name, func(t *testing.T) {
48+
parsed := graphql.DateTime.ParseLiteral(testCase.Literal)
49+
if parsed != testCase.Expected {
50+
t.Fatalf("failed DateTime.ParseLiteral(%T(%v)), expected: %v, got %v", testCase.Literal, testCase.Literal, parsed, testCase.Expected)
51+
}
52+
})
53+
}
54+
}

scalars_parsevalue_test.go

Lines changed: 0 additions & 27 deletions
This file was deleted.

0 commit comments

Comments
 (0)