Skip to content

Commit

Permalink
Allow nulls in input fields
Browse files Browse the repository at this point in the history
  • Loading branch information
Adam Scarr committed Mar 19, 2018
1 parent 54fbe16 commit 84b0661
Show file tree
Hide file tree
Showing 9 changed files with 70 additions and 85 deletions.
13 changes: 8 additions & 5 deletions codegen/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,14 @@ func (t Type) unmarshal(result, raw string, remainingMods []string, depth int) s
case len(remainingMods) > 0 && remainingMods[0] == modPtr:
ptr := "ptr" + strconv.Itoa(depth)
return tpl(`var {{.ptr}} {{.mods}}{{.t.FullName}}
{{.next}}
{{.result}} = &{{.ptr -}}
if {{.raw}} != nil {
{{.next}}
{{.result}} = &{{.ptr -}}
}
`, map[string]interface{}{
"ptr": ptr,
"t": t,
"raw": raw,
"result": result,
"mods": strings.Join(remainingMods[1:], ""),
"next": t.unmarshal(ptr, raw, remainingMods[1:], depth+1),
Expand Down Expand Up @@ -122,11 +125,11 @@ func (t Type) unmarshal(result, raw string, remainingMods []string, depth int) s

return tpl(`{{- if .t.CastType }}
var castTmp {{.t.FullName}}
{{- end }}
{{ end }}
{{- if .t.Marshaler }}
{{ .result }}, err = {{ .t.Marshaler.PkgDot }}Unmarshal{{.t.Marshaler.GoType}}({{.raw}})
{{- .result }}, err = {{ .t.Marshaler.PkgDot }}Unmarshal{{.t.Marshaler.GoType}}({{.raw}})
{{- else }}
err = (&{{.result}}).UnmarshalGQL({{.raw}})
err = (&{{.result}}).UnmarshalGQL({{.raw}})
{{- end }}
{{- if .t.CastType }}
{{ .realResult }} = {{.t.CastType}}(castTmp)
Expand Down
8 changes: 0 additions & 8 deletions example/chat/generated.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,6 @@ func (ec *executionContext) _Mutation_post(field graphql.CollectedField) graphql
var arg0 string
if tmp, ok := field.Args["text"]; ok {
var err error

arg0, err = graphql.UnmarshalString(tmp)
if err != nil {
ec.Error(err)
Expand All @@ -216,7 +215,6 @@ func (ec *executionContext) _Mutation_post(field graphql.CollectedField) graphql
var arg1 string
if tmp, ok := field.Args["username"]; ok {
var err error

arg1, err = graphql.UnmarshalString(tmp)
if err != nil {
ec.Error(err)
Expand All @@ -226,7 +224,6 @@ func (ec *executionContext) _Mutation_post(field graphql.CollectedField) graphql
var arg2 string
if tmp, ok := field.Args["roomName"]; ok {
var err error

arg2, err = graphql.UnmarshalString(tmp)
if err != nil {
ec.Error(err)
Expand Down Expand Up @@ -271,7 +268,6 @@ func (ec *executionContext) _Query_room(field graphql.CollectedField) graphql.Ma
var arg0 string
if tmp, ok := field.Args["name"]; ok {
var err error

arg0, err = graphql.UnmarshalString(tmp)
if err != nil {
ec.Error(err)
Expand Down Expand Up @@ -310,7 +306,6 @@ func (ec *executionContext) _Query___type(field graphql.CollectedField) graphql.
var arg0 string
if tmp, ok := field.Args["name"]; ok {
var err error

arg0, err = graphql.UnmarshalString(tmp)
if err != nil {
ec.Error(err)
Expand Down Expand Up @@ -347,7 +342,6 @@ func (ec *executionContext) _Subscription_messageAdded(field graphql.CollectedFi
var arg0 string
if tmp, ok := field.Args["roomName"]; ok {
var err error

arg0, err = graphql.UnmarshalString(tmp)
if err != nil {
ec.Error(err)
Expand Down Expand Up @@ -770,7 +764,6 @@ func (ec *executionContext) ___Type_fields(field graphql.CollectedField, obj *in
var arg0 bool
if tmp, ok := field.Args["includeDeprecated"]; ok {
var err error

arg0, err = graphql.UnmarshalBoolean(tmp)
if err != nil {
ec.Error(err)
Expand Down Expand Up @@ -822,7 +815,6 @@ func (ec *executionContext) ___Type_enumValues(field graphql.CollectedField, obj
var arg0 bool
if tmp, ok := field.Args["includeDeprecated"]; ok {
var err error

arg0, err = graphql.UnmarshalBoolean(tmp)
if err != nil {
ec.Error(err)
Expand Down
4 changes: 0 additions & 4 deletions example/dataloader/generated.go
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,6 @@ func (ec *executionContext) _Query_torture(field graphql.CollectedField) graphql
rawIf2 := rawIf1[idx1].([]interface{})
arg0[idx1] = make([]int, len(rawIf2))
for idx2 := range rawIf2 {

arg0[idx1][idx2], err = graphql.UnmarshalInt(rawIf2[idx2])
}
}
Expand Down Expand Up @@ -387,7 +386,6 @@ func (ec *executionContext) _Query___type(field graphql.CollectedField) graphql.
var arg0 string
if tmp, ok := field.Args["name"]; ok {
var err error

arg0, err = graphql.UnmarshalString(tmp)
if err != nil {
ec.Error(err)
Expand Down Expand Up @@ -801,7 +799,6 @@ func (ec *executionContext) ___Type_fields(field graphql.CollectedField, obj *in
var arg0 bool
if tmp, ok := field.Args["includeDeprecated"]; ok {
var err error

arg0, err = graphql.UnmarshalBoolean(tmp)
if err != nil {
ec.Error(err)
Expand Down Expand Up @@ -853,7 +850,6 @@ func (ec *executionContext) ___Type_enumValues(field graphql.CollectedField, obj
var arg0 bool
if tmp, ok := field.Args["includeDeprecated"]; ok {
var err error

arg0, err = graphql.UnmarshalBoolean(tmp)
if err != nil {
ec.Error(err)
Expand Down
19 changes: 9 additions & 10 deletions example/scalars/generated.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,6 @@ func (ec *executionContext) _Query_user(field graphql.CollectedField) graphql.Ma
var arg0 string
if tmp, ok := field.Args["id"]; ok {
var err error

arg0, err = graphql.UnmarshalID(tmp)
if err != nil {
ec.Error(err)
Expand Down Expand Up @@ -125,7 +124,6 @@ func (ec *executionContext) _Query_search(field graphql.CollectedField) graphql.
var arg0 SearchArgs
if tmp, ok := field.Args["input"]; ok {
var err error

arg0, err = UnmarshalSearchArgs(tmp)
if err != nil {
ec.Error(err)
Expand All @@ -134,7 +132,6 @@ func (ec *executionContext) _Query_search(field graphql.CollectedField) graphql.
} else {
var tmp interface{} = map[string]interface{}{"location": "37,144"}
var err error

arg0, err = UnmarshalSearchArgs(tmp)
if err != nil {
ec.Error(err)
Expand Down Expand Up @@ -175,7 +172,6 @@ func (ec *executionContext) _Query___type(field graphql.CollectedField) graphql.
var arg0 string
if tmp, ok := field.Args["name"]; ok {
var err error

arg0, err = graphql.UnmarshalString(tmp)
if err != nil {
ec.Error(err)
Expand Down Expand Up @@ -644,7 +640,6 @@ func (ec *executionContext) ___Type_fields(field graphql.CollectedField, obj *in
var arg0 bool
if tmp, ok := field.Args["includeDeprecated"]; ok {
var err error

arg0, err = graphql.UnmarshalBoolean(tmp)
if err != nil {
ec.Error(err)
Expand Down Expand Up @@ -696,7 +691,6 @@ func (ec *executionContext) ___Type_enumValues(field graphql.CollectedField, obj
var arg0 bool
if tmp, ok := field.Args["includeDeprecated"]; ok {
var err error

arg0, err = graphql.UnmarshalBoolean(tmp)
if err != nil {
ec.Error(err)
Expand Down Expand Up @@ -746,18 +740,23 @@ func UnmarshalSearchArgs(v interface{}) (SearchArgs, error) {
case "location":
var err error
var ptr1 Point
if v != nil {

err = (&ptr1).UnmarshalGQL(v)
it.Location = &ptr1
}

err = (&ptr1).UnmarshalGQL(v)
it.Location = &ptr1
if err != nil {
return it, err
}
case "createdAfter":
var err error
var ptr1 time.Time
if v != nil {
ptr1, err = UnmarshalTimestamp(v)
it.CreatedAfter = &ptr1
}

ptr1, err = UnmarshalTimestamp(v)
it.CreatedAfter = &ptr1
if err != nil {
return it, err
}
Expand Down
Loading

0 comments on commit 84b0661

Please sign in to comment.