Created
June 11, 2018 22:43
-
-
Save jonlundy/5dbb78346750f20bf683eda40f6ed24d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type Foo { | |
id: ID! | |
name: String! | |
} | |
type Query { | |
foo(id: ID!): Foo! | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package model | |
import ( | |
"io" | |
"fmt" | |
"strconv" | |
"github.com/vektah/gqlgen/graphql" | |
) | |
// if the type referenced in types.json is a function that returns a marshaller we can use it to encode and decode | |
// onto any existing go type. | |
func MarshalID(t uint64) graphql.Marshaler { | |
return graphql.WriterFunc(func(w io.Writer) { | |
io.WriteString(w, strconv.FormatUint(t, 10)) | |
}) | |
} | |
// Unmarshal{Typename} is only required if the scalar appears as an input. The raw values have already been decoded | |
// from json into int/float64/bool/nil/map[string]interface/[]interface | |
func UnmarshalID(v interface{}) (uint64, error) { | |
switch v.(type) { | |
case string: | |
return strconv.ParseUint(v.(string), 10, 64) | |
case int: | |
return uint64(v.(int)), nil | |
case float64: | |
return uint64(v.(float64)), nil | |
} | |
return 0, fmt.Errorf("unable to unmarshal: %#v %T", v, v) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"ID": "pkg/model.ID" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment