Skip to content

Commit ab68eca

Browse files
Revert "Fix marshaling interfaces and union types (#3211)" (#3289)
This reverts commit 3556475.
1 parent 26da8a6 commit ab68eca

File tree

19 files changed

+9
-520
lines changed

19 files changed

+9
-520
lines changed

_examples/federation/accounts/graph/generated.go

-12
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

_examples/federation/products/graph/generated.go

-12
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

_examples/federation/reviews/graph/generated.go

-24
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

_examples/selection/generated.go

-12
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

_examples/starwars/generated/exec.go

-30
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

_examples/type-system-extension/generated.go

-12
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

codegen/interface.gotpl

-3
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,6 @@ func (ec *executionContext) _{{$interface.Name}}(ctx context.Context, sel ast.Se
66
return graphql.Null
77
{{- range $implementor := $interface.Implementors }}
88
case {{$implementor.Type | ref}}:
9-
if len(graphql.CollectFields(ec.OperationContext, sel, []string{"{{$interface.Type | typeName }}", "{{$implementor.Type | typeName}}"})) == 0 {
10-
return graphql.Empty{}
11-
}
129
{{- if $implementor.CanBeNil }}
1310
if obj == nil {
1411
return graphql.Null

codegen/templates/templates.go

+5-16
Original file line numberDiff line numberDiff line change
@@ -88,15 +88,15 @@ func Render(cfg Options) error {
8888
}
8989

9090
roots := make([]string, 0, len(t.Templates()))
91-
for _, templ := range t.Templates() {
91+
for _, template := range t.Templates() {
9292
// templates that end with _.gotpl are special files we don't want to include
93-
if strings.HasSuffix(templ.Name(), "_.gotpl") ||
93+
if strings.HasSuffix(template.Name(), "_.gotpl") ||
9494
// filter out templates added with {{ template xxx }} syntax inside the template file
95-
!strings.HasSuffix(templ.Name(), ".gotpl") {
95+
!strings.HasSuffix(template.Name(), ".gotpl") {
9696
continue
9797
}
9898

99-
roots = append(roots, templ.Name())
99+
roots = append(roots, template.Name())
100100
}
101101

102102
// then execute all the important looking ones in order, adding them to the same file
@@ -220,7 +220,6 @@ func Funcs() template.FuncMap {
220220
"render": func(filename string, tpldata any) (*bytes.Buffer, error) {
221221
return render(resolveName(filename, 0), tpldata)
222222
},
223-
"typeName": typeName,
224223
}
225224
}
226225

@@ -648,16 +647,6 @@ func resolveName(name string, skip int) string {
648647
return filepath.Join(filepath.Dir(callerFile), name)
649648
}
650649

651-
func typeName(t types.Type) string {
652-
name := types.TypeString(t, func(*types.Package) string {
653-
return ""
654-
})
655-
if name != "" && strings.HasPrefix(name, "*") {
656-
return name[1:]
657-
}
658-
return name
659-
}
660-
661650
func render(filename string, tpldata any) (*bytes.Buffer, error) {
662651
t := template.New("").Funcs(Funcs())
663652

@@ -683,7 +672,7 @@ func write(filename string, b []byte, packages *code.Packages) error {
683672

684673
formatted, err := imports.Prune(filename, b, packages)
685674
if err != nil {
686-
_, _ = fmt.Fprintf(os.Stderr, "gofmt failed on %s: %s\n", filepath.Base(filename), err.Error())
675+
fmt.Fprintf(os.Stderr, "gofmt failed on %s: %s\n", filepath.Base(filename), err.Error())
687676
formatted = b
688677
}
689678

codegen/templates/templates_test.go

-26
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package templates
33
import (
44
"embed"
55
"fmt"
6-
"go/types"
76
"os"
87
"path/filepath"
98
"testing"
@@ -356,28 +355,3 @@ func TestRenderFS(t *testing.T) {
356355
// don't look at last character since it's \n on Linux and \r\n on Windows
357356
assert.Equal(t, expectedString, actualContentsStr[:len(expectedString)])
358357
}
359-
360-
func TestTypeName(t *testing.T) {
361-
testType := types.NewNamed(
362-
types.NewTypeName(0, types.NewPackage(
363-
"github.com/99designs/gqlgen/codegen/templates",
364-
"templates",
365-
), "testType", nil),
366-
types.NewStruct(nil, nil),
367-
nil,
368-
)
369-
370-
tests := []struct {
371-
input types.Type
372-
expected string
373-
}{
374-
{testType, "testType"},
375-
{types.NewPointer(testType), "testType"},
376-
{types.NewPointer(types.NewPointer(testType)), "*testType"},
377-
}
378-
379-
for _, test := range tests {
380-
result := typeName(test.input)
381-
assert.Equal(t, test.expected, result)
382-
}
383-
}

codegen/testserver/followschema/generated_test.go

-25
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,6 @@ func TestUnionFragments(t *testing.T) {
3737
resolvers.QueryResolver.ShapeUnion = func(ctx context.Context) (ShapeUnion, error) {
3838
return &Circle{Radius: 32}, nil
3939
}
40-
resolvers.QueryResolver.Shapes = func(ctx context.Context) ([]Shape, error) {
41-
return []Shape{
42-
&Circle{Radius: 45},
43-
&Circle{Radius: 54},
44-
}, nil
45-
}
4640

4741
srv := handler.NewDefaultServer(NewExecutableSchema(Config{Resolvers: resolvers}))
4842
c := client.New(srv)
@@ -84,23 +78,4 @@ func TestUnionFragments(t *testing.T) {
8478
`, &resp)
8579
require.NotEmpty(t, resp.ShapeUnion.Radius)
8680
})
87-
88-
t.Run("without circle", func(t *testing.T) {
89-
var resp struct {
90-
Shapes []struct {
91-
Length, Width float64
92-
}
93-
}
94-
require.Empty(t, resp.Shapes)
95-
c.MustPost(`query {
96-
shapes {
97-
... on Rectangle {
98-
length
99-
width
100-
}
101-
}
102-
}
103-
`, &resp)
104-
require.Empty(t, resp.Shapes)
105-
})
10681
}

0 commit comments

Comments
 (0)