From 6ea332de897903e19d7750dd28256d864426f9f0 Mon Sep 17 00:00:00 2001 From: spetrunin Date: Thu, 14 Nov 2024 23:35:51 +0200 Subject: [PATCH] fix: fix merging of response nodes of enum type --- .../engine/postprocess/merge_fields_test.go | 108 +++++++++++++++++- v2/pkg/engine/resolve/node_enum.go | 9 +- 2 files changed, 112 insertions(+), 5 deletions(-) diff --git a/v2/pkg/engine/postprocess/merge_fields_test.go b/v2/pkg/engine/postprocess/merge_fields_test.go index ee0a666657..948f84c94c 100644 --- a/v2/pkg/engine/postprocess/merge_fields_test.go +++ b/v2/pkg/engine/postprocess/merge_fields_test.go @@ -4,16 +4,17 @@ import ( "testing" "github.com/stretchr/testify/assert" + "github.com/wundergraph/graphql-go-tools/v2/pkg/engine/resolve" ) func TestMergeFields_Process(t *testing.T) { - runTest := func(in, out resolve.Node) func(t *testing.T) { + runTest := func(in, expected resolve.Node) func(t *testing.T) { return func(t *testing.T) { m := &mergeFields{} m.Process(in) - assert.Equal(t, out, in) + assert.Equal(t, expected, in) } } @@ -43,6 +44,109 @@ func TestMergeFields_Process(t *testing.T) { }, )) + t.Run("merge enum fields", runTest( + &resolve.Object{ + Fields: []*resolve.Field{ + { + Name: []byte(`interfaceField`), + Value: &resolve.Object{ + Fields: []*resolve.Field{ + { + Name: []byte(`enumField`), + Value: &resolve.Enum{ + Values: []string{`a`}, + InaccessibleValues: []string{}, + TypeName: `Enum`, + }, + }, + }, + }, + OnTypeNames: [][]byte{[]byte(`A`), []byte(`B`)}, + }, + { + Name: []byte(`interfaceField`), + Value: &resolve.Object{ + Fields: []*resolve.Field{ + { + Name: []byte(`stringField`), + Value: &resolve.String{}, + }, + }, + }, + OnTypeNames: [][]byte{[]byte(`A`), []byte(`B`)}, + }, + }, + }, + &resolve.Object{ + Fields: []*resolve.Field{ + { + Name: []byte(`interfaceField`), + Value: &resolve.Object{ + Fields: []*resolve.Field{ + { + Name: []byte(`enumField`), + Value: &resolve.Enum{ + Values: []string{`a`}, + InaccessibleValues: []string{}, + TypeName: `Enum`, + }, + ParentOnTypeNames: []resolve.ParentOnTypeNames{ + { + Depth: 1, + Names: [][]byte{[]byte(`A`)}, + }, + }, + }, + { + Name: []byte(`stringField`), + Value: &resolve.String{}, + ParentOnTypeNames: []resolve.ParentOnTypeNames{ + { + Depth: 1, + Names: [][]byte{[]byte(`A`)}, + }, + }, + }, + }, + }, + OnTypeNames: [][]byte{[]byte(`A`)}, + }, + { + Name: []byte(`interfaceField`), + Value: &resolve.Object{ + Fields: []*resolve.Field{ + { + Name: []byte(`enumField`), + Value: &resolve.Enum{ + Values: []string{`a`}, + InaccessibleValues: []string{}, + TypeName: `Enum`, + }, + ParentOnTypeNames: []resolve.ParentOnTypeNames{ + { + Depth: 1, + Names: [][]byte{[]byte(`B`)}, + }, + }, + }, + { + Name: []byte(`stringField`), + Value: &resolve.String{}, + ParentOnTypeNames: []resolve.ParentOnTypeNames{ + { + Depth: 1, + Names: [][]byte{[]byte(`B`)}, + }, + }, + }, + }, + }, + OnTypeNames: [][]byte{[]byte(`B`)}, + }, + }, + }, + )) + t.Run("merge fields at the end of an object reverse", runTest( &resolve.Object{ Fields: []*resolve.Field{ diff --git a/v2/pkg/engine/resolve/node_enum.go b/v2/pkg/engine/resolve/node_enum.go index 9843fd6ee2..926a6e0c3f 100644 --- a/v2/pkg/engine/resolve/node_enum.go +++ b/v2/pkg/engine/resolve/node_enum.go @@ -17,9 +17,12 @@ func (_ *Enum) NodeKind() NodeKind { func (e *Enum) Copy() Node { return &Enum{ - Path: e.Path, - Nullable: e.Nullable, - Export: e.Export, + Path: e.Path, + Nullable: e.Nullable, + Export: e.Export, + TypeName: e.TypeName, + Values: e.Values, + InaccessibleValues: e.InaccessibleValues, } }