Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix DateTime literal parsing #419

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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.