From 937a90182270a6f0526e30e72f3dbe17f235e3b6 Mon Sep 17 00:00:00 2001 From: Nick Randall Date: Thu, 8 Dec 2016 14:54:31 -0700 Subject: [PATCH 1/2] implement sql.Scanner and driver.Value for graphql.ID type --- graphql.go | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/graphql.go b/graphql.go index 9a066ff1..5b4ece35 100644 --- a/graphql.go +++ b/graphql.go @@ -2,6 +2,8 @@ package graphql import ( "context" + "database/sql" + "database/sql/driver" "encoding/json" "fmt" @@ -28,6 +30,26 @@ const OpenTracingTagError = "graphql.error" type ID string +// compile time check +var ( + _ sql.Scanner = (*ID)(nil) + _ driver.Valuer = (*ID)(nil) +) + +func (id *ID) Scan(src interface{}) error { + switch t := src.(type) { + case string: + *id = ID(t) + default: + return fmt.Errorf("wrong type") + } + return nil +} + +func (id ID) Value() (driver.Value, error) { + return string(id), nil +} + func ParseSchema(schemaString string, resolver interface{}) (*Schema, error) { b := New() if err := b.Parse(schemaString); err != nil { From cefcfcab6d5b649a71e268771346a1f16cf45e9f Mon Sep 17 00:00:00 2001 From: Nick Randall Date: Thu, 8 Dec 2016 15:51:59 -0700 Subject: [PATCH 2/2] adding more cases to scan method because others may want that --- graphql.go | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/graphql.go b/graphql.go index 5b4ece35..178cfb1a 100644 --- a/graphql.go +++ b/graphql.go @@ -6,6 +6,7 @@ import ( "database/sql/driver" "encoding/json" "fmt" + "strconv" opentracing "github.com/opentracing/opentracing-go" "github.com/opentracing/opentracing-go/ext" @@ -40,6 +41,12 @@ func (id *ID) Scan(src interface{}) error { switch t := src.(type) { case string: *id = ID(t) + case int64: + *id = ID(strconv.FormatInt(t, 10)) + case []byte: + *id = ID(t) + case nil: + // do nothing default: return fmt.Errorf("wrong type") }