From 14d1a4dc0a9242154d3a22787d92ab70239f079a Mon Sep 17 00:00:00 2001 From: Luke Cawood Date: Fri, 10 Jul 2020 16:53:06 +1000 Subject: [PATCH] Only return nil for nilable types when the graphql spec would allow it --- codegen/testserver/generated.go | 24 ------------------- codegen/type.gotpl | 2 +- example/chat/generated.go | 3 --- example/config/generated.go | 3 --- example/dataloader/generated.go | 3 --- .../accounts/graph/generated/generated.go | 9 ------- .../products/graph/generated/generated.go | 9 ------- .../reviews/graph/generated/generated.go | 9 ------- example/fileupload/generated.go | 15 ------------ example/scalars/generated.go | 6 ----- example/selection/generated.go | 3 --- example/starwars/generated/exec.go | 12 ---------- example/todo/generated.go | 6 ----- example/type-system-extension/generated.go | 3 --- integration/generated.go | 6 ----- 15 files changed, 1 insertion(+), 112 deletions(-) diff --git a/codegen/testserver/generated.go b/codegen/testserver/generated.go index b737424dd7..ce5a94e73a 100644 --- a/codegen/testserver/generated.go +++ b/codegen/testserver/generated.go @@ -12478,9 +12478,6 @@ func (ec *executionContext) marshalNBoolean2bool(ctx context.Context, sel ast.Se } func (ec *executionContext) unmarshalNBytes2ᚕbyte(ctx context.Context, v interface{}) ([]byte, error) { - if v == nil { - return nil, nil - } return UnmarshalBytes(v) } @@ -12599,9 +12596,6 @@ func (ec *executionContext) unmarshalNInnerDirectives2githubᚗcomᚋ99designs } func (ec *executionContext) unmarshalNInnerDirectives2ᚖgithubᚗcomᚋ99designsᚋgqlgenᚋcodegenᚋtestserverᚐInnerDirectives(ctx context.Context, v interface{}) (*InnerDirectives, error) { - if v == nil { - return nil, nil - } res, err := ec.unmarshalNInnerDirectives2githubᚗcomᚋ99designsᚋgqlgenᚋcodegenᚋtestserverᚐInnerDirectives(ctx, v) return &res, err } @@ -12611,9 +12605,6 @@ func (ec *executionContext) unmarshalNInnerInput2githubᚗcomᚋ99designsᚋgqlg } func (ec *executionContext) unmarshalNInnerInput2ᚖgithubᚗcomᚋ99designsᚋgqlgenᚋcodegenᚋtestserverᚐInnerInput(ctx context.Context, v interface{}) (*InnerInput, error) { - if v == nil { - return nil, nil - } res, err := ec.unmarshalNInnerInput2githubᚗcomᚋ99designsᚋgqlgenᚋcodegenᚋtestserverᚐInnerInput(ctx, v) return &res, err } @@ -12716,9 +12707,6 @@ func (ec *executionContext) marshalNMarshalPanic2githubᚗcomᚋ99designsᚋgqlg } func (ec *executionContext) unmarshalNMarshalPanic2ᚕgithubᚗcomᚋ99designsᚋgqlgenᚋcodegenᚋtestserverᚐMarshalPanicᚄ(ctx context.Context, v interface{}) ([]MarshalPanic, error) { - if v == nil { - return nil, nil - } var vSlice []interface{} if v != nil { if tmp1, ok := v.([]interface{}); ok { @@ -12868,9 +12856,6 @@ func (ec *executionContext) marshalNString2string(ctx context.Context, sel ast.S } func (ec *executionContext) unmarshalNString2ᚕstringᚄ(ctx context.Context, v interface{}) ([]string, error) { - if v == nil { - return nil, nil - } var vSlice []interface{} if v != nil { if tmp1, ok := v.([]interface{}); ok { @@ -12900,9 +12885,6 @@ func (ec *executionContext) marshalNString2ᚕstringᚄ(ctx context.Context, sel } func (ec *executionContext) unmarshalNString2ᚕᚖstring(ctx context.Context, v interface{}) ([]*string, error) { - if v == nil { - return nil, nil - } var vSlice []interface{} if v != nil { if tmp1, ok := v.([]interface{}); ok { @@ -12932,9 +12914,6 @@ func (ec *executionContext) marshalNString2ᚕᚖstring(ctx context.Context, sel } func (ec *executionContext) unmarshalNString2ᚖstring(ctx context.Context, v interface{}) (*string, error) { - if v == nil { - return nil, nil - } res, err := ec.unmarshalNString2string(ctx, v) return &res, err } @@ -13133,9 +13112,6 @@ func (ec *executionContext) marshalN__DirectiveLocation2string(ctx context.Conte } func (ec *executionContext) unmarshalN__DirectiveLocation2ᚕstringᚄ(ctx context.Context, v interface{}) ([]string, error) { - if v == nil { - return nil, nil - } var vSlice []interface{} if v != nil { if tmp1, ok := v.([]interface{}); ok { diff --git a/codegen/type.gotpl b/codegen/type.gotpl index f283f5603a..469c2a373d 100644 --- a/codegen/type.gotpl +++ b/codegen/type.gotpl @@ -1,7 +1,7 @@ {{- range $type := .ReferencedTypes }} {{ with $type.UnmarshalFunc }} func (ec *executionContext) {{ . }}(ctx context.Context, v interface{}) ({{ $type.GO | ref }}, error) { - {{- if $type.IsNilable }} + {{- if and $type.IsNilable (not $type.GQL.NonNull) }} if v == nil { return nil, nil } {{- end }} {{- if $type.IsPtr }} diff --git a/example/chat/generated.go b/example/chat/generated.go index 9ed9ae91b4..f64e588ceb 100644 --- a/example/chat/generated.go +++ b/example/chat/generated.go @@ -2490,9 +2490,6 @@ func (ec *executionContext) marshalN__DirectiveLocation2string(ctx context.Conte } func (ec *executionContext) unmarshalN__DirectiveLocation2ᚕstringᚄ(ctx context.Context, v interface{}) ([]string, error) { - if v == nil { - return nil, nil - } var vSlice []interface{} if v != nil { if tmp1, ok := v.([]interface{}); ok { diff --git a/example/config/generated.go b/example/config/generated.go index f14c0cdaa5..4322c888ba 100644 --- a/example/config/generated.go +++ b/example/config/generated.go @@ -2381,9 +2381,6 @@ func (ec *executionContext) marshalN__DirectiveLocation2string(ctx context.Conte } func (ec *executionContext) unmarshalN__DirectiveLocation2ᚕstringᚄ(ctx context.Context, v interface{}) ([]string, error) { - if v == nil { - return nil, nil - } var vSlice []interface{} if v != nil { if tmp1, ok := v.([]interface{}); ok { diff --git a/example/dataloader/generated.go b/example/dataloader/generated.go index 915473b509..ddb182fc75 100644 --- a/example/dataloader/generated.go +++ b/example/dataloader/generated.go @@ -2666,9 +2666,6 @@ func (ec *executionContext) marshalN__DirectiveLocation2string(ctx context.Conte } func (ec *executionContext) unmarshalN__DirectiveLocation2ᚕstringᚄ(ctx context.Context, v interface{}) ([]string, error) { - if v == nil { - return nil, nil - } var vSlice []interface{} if v != nil { if tmp1, ok := v.([]interface{}); ok { diff --git a/example/federation/accounts/graph/generated/generated.go b/example/federation/accounts/graph/generated/generated.go index 08286ab0bb..d799cc0132 100644 --- a/example/federation/accounts/graph/generated/generated.go +++ b/example/federation/accounts/graph/generated/generated.go @@ -2180,9 +2180,6 @@ func (ec *executionContext) marshalNUser2ᚖgithubᚗcomᚋ99designsᚋgqlgenᚋ } func (ec *executionContext) unmarshalN_Any2map(ctx context.Context, v interface{}) (map[string]interface{}, error) { - if v == nil { - return nil, nil - } return graphql.UnmarshalMap(v) } @@ -2203,9 +2200,6 @@ func (ec *executionContext) marshalN_Any2map(ctx context.Context, sel ast.Select } func (ec *executionContext) unmarshalN_Any2ᚕmapᚄ(ctx context.Context, v interface{}) ([]map[string]interface{}, error) { - if v == nil { - return nil, nil - } var vSlice []interface{} if v != nil { if tmp1, ok := v.([]interface{}); ok { @@ -2345,9 +2339,6 @@ func (ec *executionContext) marshalN__DirectiveLocation2string(ctx context.Conte } func (ec *executionContext) unmarshalN__DirectiveLocation2ᚕstringᚄ(ctx context.Context, v interface{}) ([]string, error) { - if v == nil { - return nil, nil - } var vSlice []interface{} if v != nil { if tmp1, ok := v.([]interface{}); ok { diff --git a/example/federation/products/graph/generated/generated.go b/example/federation/products/graph/generated/generated.go index d6005ae532..2fee8579f0 100644 --- a/example/federation/products/graph/generated/generated.go +++ b/example/federation/products/graph/generated/generated.go @@ -2254,9 +2254,6 @@ func (ec *executionContext) marshalNString2string(ctx context.Context, sel ast.S } func (ec *executionContext) unmarshalN_Any2map(ctx context.Context, v interface{}) (map[string]interface{}, error) { - if v == nil { - return nil, nil - } return graphql.UnmarshalMap(v) } @@ -2277,9 +2274,6 @@ func (ec *executionContext) marshalN_Any2map(ctx context.Context, sel ast.Select } func (ec *executionContext) unmarshalN_Any2ᚕmapᚄ(ctx context.Context, v interface{}) ([]map[string]interface{}, error) { - if v == nil { - return nil, nil - } var vSlice []interface{} if v != nil { if tmp1, ok := v.([]interface{}); ok { @@ -2419,9 +2413,6 @@ func (ec *executionContext) marshalN__DirectiveLocation2string(ctx context.Conte } func (ec *executionContext) unmarshalN__DirectiveLocation2ᚕstringᚄ(ctx context.Context, v interface{}) ([]string, error) { - if v == nil { - return nil, nil - } var vSlice []interface{} if v != nil { if tmp1, ok := v.([]interface{}); ok { diff --git a/example/federation/reviews/graph/generated/generated.go b/example/federation/reviews/graph/generated/generated.go index 9326171535..3b702da640 100644 --- a/example/federation/reviews/graph/generated/generated.go +++ b/example/federation/reviews/graph/generated/generated.go @@ -2537,9 +2537,6 @@ func (ec *executionContext) marshalNUser2ᚖgithubᚗcomᚋ99designsᚋgqlgenᚋ } func (ec *executionContext) unmarshalN_Any2map(ctx context.Context, v interface{}) (map[string]interface{}, error) { - if v == nil { - return nil, nil - } return graphql.UnmarshalMap(v) } @@ -2560,9 +2557,6 @@ func (ec *executionContext) marshalN_Any2map(ctx context.Context, sel ast.Select } func (ec *executionContext) unmarshalN_Any2ᚕmapᚄ(ctx context.Context, v interface{}) ([]map[string]interface{}, error) { - if v == nil { - return nil, nil - } var vSlice []interface{} if v != nil { if tmp1, ok := v.([]interface{}); ok { @@ -2702,9 +2696,6 @@ func (ec *executionContext) marshalN__DirectiveLocation2string(ctx context.Conte } func (ec *executionContext) unmarshalN__DirectiveLocation2ᚕstringᚄ(ctx context.Context, v interface{}) ([]string, error) { - if v == nil { - return nil, nil - } var vSlice []interface{} if v != nil { if tmp1, ok := v.([]interface{}); ok { diff --git a/example/fileupload/generated.go b/example/fileupload/generated.go index 630055cacc..517cbb1fca 100644 --- a/example/fileupload/generated.go +++ b/example/fileupload/generated.go @@ -2353,9 +2353,6 @@ func (ec *executionContext) marshalNUpload2githubᚗcomᚋ99designsᚋgqlgenᚋg } func (ec *executionContext) unmarshalNUpload2ᚕᚖgithubᚗcomᚋ99designsᚋgqlgenᚋgraphqlᚐUploadᚄ(ctx context.Context, v interface{}) ([]*graphql.Upload, error) { - if v == nil { - return nil, nil - } var vSlice []interface{} if v != nil { if tmp1, ok := v.([]interface{}); ok { @@ -2385,9 +2382,6 @@ func (ec *executionContext) marshalNUpload2ᚕᚖgithubᚗcomᚋ99designsᚋgqlg } func (ec *executionContext) unmarshalNUpload2ᚖgithubᚗcomᚋ99designsᚋgqlgenᚋgraphqlᚐUpload(ctx context.Context, v interface{}) (*graphql.Upload, error) { - if v == nil { - return nil, nil - } res, err := ec.unmarshalNUpload2githubᚗcomᚋ99designsᚋgqlgenᚋgraphqlᚐUpload(ctx, v) return &res, err } @@ -2407,9 +2401,6 @@ func (ec *executionContext) unmarshalNUploadFile2githubᚗcomᚋ99designsᚋgqlg } func (ec *executionContext) unmarshalNUploadFile2ᚕᚖgithubᚗcomᚋ99designsᚋgqlgenᚋexampleᚋfileuploadᚋmodelᚐUploadFileᚄ(ctx context.Context, v interface{}) ([]*model.UploadFile, error) { - if v == nil { - return nil, nil - } var vSlice []interface{} if v != nil { if tmp1, ok := v.([]interface{}); ok { @@ -2430,9 +2421,6 @@ func (ec *executionContext) unmarshalNUploadFile2ᚕᚖgithubᚗcomᚋ99designs } func (ec *executionContext) unmarshalNUploadFile2ᚖgithubᚗcomᚋ99designsᚋgqlgenᚋexampleᚋfileuploadᚋmodelᚐUploadFile(ctx context.Context, v interface{}) (*model.UploadFile, error) { - if v == nil { - return nil, nil - } res, err := ec.unmarshalNUploadFile2githubᚗcomᚋ99designsᚋgqlgenᚋexampleᚋfileuploadᚋmodelᚐUploadFile(ctx, v) return &res, err } @@ -2493,9 +2481,6 @@ func (ec *executionContext) marshalN__DirectiveLocation2string(ctx context.Conte } func (ec *executionContext) unmarshalN__DirectiveLocation2ᚕstringᚄ(ctx context.Context, v interface{}) ([]string, error) { - if v == nil { - return nil, nil - } var vSlice []interface{} if v != nil { if tmp1, ok := v.([]interface{}); ok { diff --git a/example/scalars/generated.go b/example/scalars/generated.go index 54544c72e9..95c630adcf 100644 --- a/example/scalars/generated.go +++ b/example/scalars/generated.go @@ -2373,9 +2373,6 @@ func (ec *executionContext) marshalNPoint2githubᚗcomᚋ99designsᚋgqlgenᚋex } func (ec *executionContext) unmarshalNPoint2ᚖgithubᚗcomᚋ99designsᚋgqlgenᚋexampleᚋscalarsᚋmodelᚐPoint(ctx context.Context, v interface{}) (*model.Point, error) { - if v == nil { - return nil, nil - } res, err := ec.unmarshalNPoint2githubᚗcomᚋ99designsᚋgqlgenᚋexampleᚋscalarsᚋmodelᚐPoint(ctx, v) return &res, err } @@ -2511,9 +2508,6 @@ func (ec *executionContext) marshalN__DirectiveLocation2string(ctx context.Conte } func (ec *executionContext) unmarshalN__DirectiveLocation2ᚕstringᚄ(ctx context.Context, v interface{}) ([]string, error) { - if v == nil { - return nil, nil - } var vSlice []interface{} if v != nil { if tmp1, ok := v.([]interface{}); ok { diff --git a/example/selection/generated.go b/example/selection/generated.go index 266c97d41c..f25e924fbb 100644 --- a/example/selection/generated.go +++ b/example/selection/generated.go @@ -2187,9 +2187,6 @@ func (ec *executionContext) marshalN__DirectiveLocation2string(ctx context.Conte } func (ec *executionContext) unmarshalN__DirectiveLocation2ᚕstringᚄ(ctx context.Context, v interface{}) ([]string, error) { - if v == nil { - return nil, nil - } var vSlice []interface{} if v != nil { if tmp1, ok := v.([]interface{}); ok { diff --git a/example/starwars/generated/exec.go b/example/starwars/generated/exec.go index 150c1a1ec9..35644f0ba0 100644 --- a/example/starwars/generated/exec.go +++ b/example/starwars/generated/exec.go @@ -4248,9 +4248,6 @@ func (ec *executionContext) marshalNEpisode2githubᚗcomᚋ99designsᚋgqlgenᚋ } func (ec *executionContext) unmarshalNEpisode2ᚕgithubᚗcomᚋ99designsᚋgqlgenᚋexampleᚋstarwarsᚋmodelsᚐEpisodeᚄ(ctx context.Context, v interface{}) ([]models.Episode, error) { - if v == nil { - return nil, nil - } var vSlice []interface{} if v != nil { if tmp1, ok := v.([]interface{}); ok { @@ -4378,9 +4375,6 @@ func (ec *executionContext) marshalNInt2int(ctx context.Context, sel ast.Selecti } func (ec *executionContext) unmarshalNInt2ᚕintᚄ(ctx context.Context, v interface{}) ([]int, error) { - if v == nil { - return nil, nil - } var vSlice []interface{} if v != nil { if tmp1, ok := v.([]interface{}); ok { @@ -4410,9 +4404,6 @@ func (ec *executionContext) marshalNInt2ᚕintᚄ(ctx context.Context, sel ast.S } func (ec *executionContext) unmarshalNInt2ᚕᚕintᚄ(ctx context.Context, v interface{}) ([][]int, error) { - if v == nil { - return nil, nil - } var vSlice []interface{} if v != nil { if tmp1, ok := v.([]interface{}); ok { @@ -4631,9 +4622,6 @@ func (ec *executionContext) marshalN__DirectiveLocation2string(ctx context.Conte } func (ec *executionContext) unmarshalN__DirectiveLocation2ᚕstringᚄ(ctx context.Context, v interface{}) ([]string, error) { - if v == nil { - return nil, nil - } var vSlice []interface{} if v != nil { if tmp1, ok := v.([]interface{}); ok { diff --git a/example/todo/generated.go b/example/todo/generated.go index 93c819a01b..cb9b3b617e 100644 --- a/example/todo/generated.go +++ b/example/todo/generated.go @@ -2245,9 +2245,6 @@ func (ec *executionContext) marshalNID2int(ctx context.Context, sel ast.Selectio } func (ec *executionContext) unmarshalNMap2map(ctx context.Context, v interface{}) (map[string]interface{}, error) { - if v == nil { - return nil, nil - } return graphql.UnmarshalMap(v) } @@ -2401,9 +2398,6 @@ func (ec *executionContext) marshalN__DirectiveLocation2string(ctx context.Conte } func (ec *executionContext) unmarshalN__DirectiveLocation2ᚕstringᚄ(ctx context.Context, v interface{}) ([]string, error) { - if v == nil { - return nil, nil - } var vSlice []interface{} if v != nil { if tmp1, ok := v.([]interface{}); ok { diff --git a/example/type-system-extension/generated.go b/example/type-system-extension/generated.go index 0d63ac02aa..318ff3baec 100644 --- a/example/type-system-extension/generated.go +++ b/example/type-system-extension/generated.go @@ -2359,9 +2359,6 @@ func (ec *executionContext) marshalN__DirectiveLocation2string(ctx context.Conte } func (ec *executionContext) unmarshalN__DirectiveLocation2ᚕstringᚄ(ctx context.Context, v interface{}) ([]string, error) { - if v == nil { - return nil, nil - } var vSlice []interface{} if v != nil { if tmp1, ok := v.([]interface{}); ok { diff --git a/integration/generated.go b/integration/generated.go index edf4d1a904..ee0a66a4da 100644 --- a/integration/generated.go +++ b/integration/generated.go @@ -2543,9 +2543,6 @@ func (ec *executionContext) marshalNString2string(ctx context.Context, sel ast.S } func (ec *executionContext) unmarshalNString2ᚕstringᚄ(ctx context.Context, v interface{}) ([]string, error) { - if v == nil { - return nil, nil - } var vSlice []interface{} if v != nil { if tmp1, ok := v.([]interface{}); ok { @@ -2630,9 +2627,6 @@ func (ec *executionContext) marshalN__DirectiveLocation2string(ctx context.Conte } func (ec *executionContext) unmarshalN__DirectiveLocation2ᚕstringᚄ(ctx context.Context, v interface{}) ([]string, error) { - if v == nil { - return nil, nil - } var vSlice []interface{} if v != nil { if tmp1, ok := v.([]interface{}); ok {