Skip to content

Commit

Permalink
Merge pull request #419 from ccbrown/fix-date-time-literal-parsing
Browse files Browse the repository at this point in the history
Fix DateTime literal parsing
  • Loading branch information
chris-ramon committed Nov 22, 2018
2 parents a7e15c0 + be84dc7 commit 993e01c
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 28 deletions.
2 changes: 1 addition & 1 deletion scalars.go
Expand Up @@ -561,7 +561,7 @@ var DateTime = NewScalar(ScalarConfig{
ParseLiteral: func(valueAST ast.Value) interface{} {
switch valueAST := valueAST.(type) {
case *ast.StringValue:
return valueAST.Value
return unserializeDateTime(valueAST.Value)
}
return nil
},
Expand Down
54 changes: 54 additions & 0 deletions scalars_parse_test.go
@@ -0,0 +1,54 @@
package graphql_test

import (
"reflect"
"testing"
"time"

"github.com/graphql-go/graphql"
"github.com/graphql-go/graphql/language/ast"
)

func TestTypeSystem_Scalar_ParseValueOutputDateTime(t *testing.T) {
t1, _ := time.Parse(time.RFC3339, "2017-07-23T03:46:56.647Z")
tests := []dateTimeSerializationTest{
{nil, nil},
{"", nil},
{(*string)(nil), nil},
{"2017-07-23", nil},
{"2017-07-23T03:46:56.647Z", t1},
}
for _, test := range tests {
val := graphql.DateTime.ParseValue(test.Value)
if val != test.Expected {
reflectedValue := reflect.ValueOf(test.Value)
t.Fatalf("failed DateTime.ParseValue(%v(%v)), expected: %v, got %v", reflectedValue.Type(), test.Value, test.Expected, val)
}
}
}

func TestTypeSystem_Scalar_ParseLiteralOutputDateTime(t *testing.T) {
t1, _ := time.Parse(time.RFC3339, "2017-07-23T03:46:56.647Z")
for name, testCase := range map[string]struct {
Literal ast.Value
Expected interface{}
}{
"String": {
Literal: &ast.StringValue{
Value: "2017-07-23T03:46:56.647Z",
},
Expected: t1,
},
"NotAString": {
Literal: &ast.IntValue{},
Expected: nil,
},
} {
t.Run(name, func(t *testing.T) {
parsed := graphql.DateTime.ParseLiteral(testCase.Literal)
if parsed != testCase.Expected {
t.Fatalf("failed DateTime.ParseLiteral(%T(%v)), expected: %v, got %v", testCase.Literal, testCase.Literal, parsed, testCase.Expected)
}
})
}
}
27 changes: 0 additions & 27 deletions scalars_parsevalue_test.go

This file was deleted.

0 comments on commit 993e01c

Please sign in to comment.