add support for custom scalars -- mainly adding proper import machinery

This commit is contained in:
Ben Kraft
2021-04-08 13:07:31 -07:00
parent e68787ad35
commit b4e8316c6a
31 changed files with 283 additions and 89 deletions
+4 -4
View File
@@ -2,19 +2,19 @@ func (v *{{.Type}}) UnmarshalJSON(b []byte) error {
var firstPass struct{
*{{.Type}}
{{range .Fields -}}
{{.GoName}} json.RawMessage `json:"{{.JSONName}}"`
{{.GoName}} {{ref "encoding/json.RawMessage"}} `json:"{{.JSONName}}"`
{{end}}
}
firstPass.{{.Type}} = v
err := json.Unmarshal(b, &firstPass)
err := {{ref "encoding/json.Unmarshal"}}(b, &firstPass)
if err != nil {
return err
}
{{range .Fields -}}
var tn struct { TypeName string `json:"__typename"` }
err = json.Unmarshal(firstPass.{{.GoName}}, &tn)
err = {{ref "encoding/json.Unmarshal"}}(firstPass.{{.GoName}}, &tn)
if err != nil {
return err
}
@@ -24,7 +24,7 @@ func (v *{{.Type}}) UnmarshalJSON(b []byte) error {
case "{{.GraphQLName}}":
{{/* TODO: handle repeated fields! */}}
v.{{$field.GoName}} = {{.GoName}}{}
err = json.Unmarshal(
err = {{ref "encoding/json.Unmarshal"}}(
firstPass.{{$field.GoName}}, &v.{{$field.GoName}})
{{end}}
{{end}}