Skip to content

Commit

Permalink
Merge pull request #702 from 99designs/drop-automatic-zeroisnull
Browse files Browse the repository at this point in the history
Drop automatic conversion of IsZero to null
  • Loading branch information
vektah committed May 9, 2019
2 parents e49d44f + 75aa99a commit 5586ee2
Show file tree
Hide file tree
Showing 14 changed files with 493 additions and 116 deletions.
19 changes: 0 additions & 19 deletions codegen/config/binder.go
Original file line number Diff line number Diff line change
Expand Up @@ -238,25 +238,6 @@ func (t *TypeReference) IsScalar() bool {
return t.Definition.Kind == ast.Scalar
}

func (t *TypeReference) HasIsZero() bool {
it := t.GO
if ptr, isPtr := it.(*types.Pointer); isPtr {
it = ptr.Elem()
}
namedType, ok := it.(*types.Named)
if !ok {
return false
}

for i := 0; i < namedType.NumMethods(); i++ {
switch namedType.Method(i).Name() {
case "IsZero":
return true
}
}
return false
}

func (t *TypeReference) UniquenessKey() string {
var nullability = "O"
if t.GQL.NonNull {
Expand Down
105 changes: 87 additions & 18 deletions codegen/testserver/generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 8 additions & 9 deletions codegen/type.gotpl
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,6 @@
{{- end }}
return graphql.Null
}
{{- else if $type.HasIsZero }}
if v.IsZero() {
{{- if $type.GQL.NonNull }}
if !ec.HasError(graphql.GetResolverContext(ctx)) {
ec.Errorf(ctx, "must not be null")
}
{{- end }}
return graphql.Null
}
{{- end }}

{{- if $type.IsSlice }}
Expand Down Expand Up @@ -119,6 +110,14 @@
{{- else if $type.Marshaler }}
{{- if $type.IsPtr }}
return ec.{{ $type.Elem.MarshalFunc }}(ctx, sel, *v)
{{- else if $type.GQL.NonNull }}
res := {{ $type.Marshaler | call }}({{- if $type.CastType }}{{ $type.CastType | ref }}(v){{else}}v{{- end }})
if res == graphql.Null {
if !ec.HasError(graphql.GetResolverContext(ctx)) {
ec.Errorf(ctx, "must not be null")
}
}
return res
{{- else }}
return {{ $type.Marshaler | call }}({{- if $type.CastType }}{{ $type.CastType | ref }}(v){{else}}v{{- end }})
{{- end }}
Expand Down
46 changes: 38 additions & 8 deletions example/chat/generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 5586ee2

Please sign in to comment.