Skip to content

Instantly share code, notes, and snippets.

@jonlundy
Created June 11, 2018 22:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jonlundy/5dbb78346750f20bf683eda40f6ed24d to your computer and use it in GitHub Desktop.
Save jonlundy/5dbb78346750f20bf683eda40f6ed24d to your computer and use it in GitHub Desktop.
type Foo {
id: ID!
name: String!
}
type Query {
foo(id: ID!): Foo!
}
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)
}
{
"ID": "pkg/model.ID"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment