{{/* (the blank lines at the start are intentional, to separate the helper from the function it follows) */}} func __unmarshal{{.GoName}}(v *{{.GoName}}, m {{ref "encoding/json.RawMessage"}}) error { if string(m) == "null" { return nil } var tn struct { TypeName string `json:"__typename"` } err := {{ref "encoding/json.Unmarshal"}}(m, &tn) if err != nil { return err } switch tn.TypeName { {{range .Implementations -}} case "{{.GraphQLName}}": *v = new({{.GoName}}) return {{ref "encoding/json.Unmarshal"}}(m, *v) {{end -}} case "": {{/* Likely if we're making a request to a mock server and the author of the mock didn't know to add __typename, so give a special error. */ -}} return {{ref "fmt.Errorf"}}( "Response was missing {{.GraphQLName}}.__typename") default: return {{ref "fmt.Errorf"}}( `Unexpected concrete type for {{.GoName}}: "%v"`, tn.TypeName) } }