github.com/operandinc/gqlgen@v0.16.1/codegen/testserver/followschema/ptr_to_ptr_input_test.go (about) 1 package followschema 2 3 import ( 4 "context" 5 "testing" 6 7 "github.com/operandinc/gqlgen/client" 8 "github.com/operandinc/gqlgen/graphql/handler" 9 "github.com/stretchr/testify/require" 10 ) 11 12 type UpdatePtrToPtrResults struct { 13 UpdatedPtrToPtr PtrToPtrOuter `json:"updatePtrToPtr"` 14 } 15 16 func TestPtrToPtr(t *testing.T) { 17 resolvers := &Stub{} 18 19 c := client.New(handler.NewDefaultServer(NewExecutableSchema(Config{Resolvers: resolvers}))) 20 21 resolvers.MutationResolver.UpdatePtrToPtr = func(ctx context.Context, in UpdatePtrToPtrOuter) (ret *PtrToPtrOuter, err error) { 22 ret = &PtrToPtrOuter{ 23 Name: "oldName", 24 Inner: &PtrToPtrInner{ 25 Key: "oldKey", 26 Value: "oldValue", 27 }, 28 StupidInner: nest7(&PtrToPtrInner{ 29 Key: "oldStupidKey", 30 Value: "oldStupidValue", 31 }), 32 } 33 34 if in.Name != nil { 35 ret.Name = *in.Name 36 } 37 38 if in.Inner != nil { 39 inner := *in.Inner 40 if inner == nil { 41 ret.Inner = nil 42 } else { 43 if in.Inner == nil { 44 ret.Inner = &PtrToPtrInner{} 45 } 46 if inner.Key != nil { 47 ret.Inner.Key = *inner.Key 48 } 49 if inner.Value != nil { 50 ret.Inner.Value = *inner.Value 51 } 52 } 53 } 54 55 if in.StupidInner != nil { 56 si := *in.StupidInner 57 if si == nil { 58 ret.StupidInner = nil 59 } else { 60 deepIn := ******si 61 deepOut := ******ret.StupidInner 62 if deepIn.Key != nil { 63 deepOut.Key = *deepIn.Key 64 } 65 if deepIn.Value != nil { 66 deepOut.Value = *deepIn.Value 67 } 68 } 69 } 70 return 71 } 72 73 t.Run("pointer to pointer input missing", func(t *testing.T) { 74 var resp UpdatePtrToPtrResults 75 76 err := c.Post(`mutation { updatePtrToPtr(input: { name: "newName" }) { name, inner { key, value }, stupidInner { key, value }}}`, &resp) 77 require.NoError(t, err) 78 79 require.Equal(t, resp.UpdatedPtrToPtr.Name, "newName") 80 require.NotNil(t, resp.UpdatedPtrToPtr.Inner) 81 require.Equal(t, resp.UpdatedPtrToPtr.Inner.Key, "oldKey") 82 require.Equal(t, resp.UpdatedPtrToPtr.Inner.Value, "oldValue") 83 require.NotNil(t, resp.UpdatedPtrToPtr.StupidInner) 84 require.NotNil(t, ******resp.UpdatedPtrToPtr.StupidInner) 85 require.Equal(t, (******resp.UpdatedPtrToPtr.StupidInner).Key, "oldStupidKey") 86 require.Equal(t, (******resp.UpdatedPtrToPtr.StupidInner).Value, "oldStupidValue") 87 }) 88 89 t.Run("pointer to pointer input non-null", func(t *testing.T) { 90 var resp UpdatePtrToPtrResults 91 92 err := c.Post(`mutation { 93 updatePtrToPtr(input: { 94 inner: { 95 key: "newKey" 96 value: "newValue" 97 } 98 }) 99 { name, inner { key, value }, stupidInner { key, value }} 100 }`, &resp) 101 require.NoError(t, err) 102 103 require.Equal(t, resp.UpdatedPtrToPtr.Name, "oldName") 104 require.NotNil(t, resp.UpdatedPtrToPtr.Inner) 105 require.Equal(t, resp.UpdatedPtrToPtr.Inner.Key, "newKey") 106 require.Equal(t, resp.UpdatedPtrToPtr.Inner.Value, "newValue") 107 require.NotNil(t, resp.UpdatedPtrToPtr.StupidInner) 108 require.NotNil(t, ******resp.UpdatedPtrToPtr.StupidInner) 109 require.Equal(t, (******resp.UpdatedPtrToPtr.StupidInner).Key, "oldStupidKey") 110 require.Equal(t, (******resp.UpdatedPtrToPtr.StupidInner).Value, "oldStupidValue") 111 }) 112 113 t.Run("pointer to pointer input null", func(t *testing.T) { 114 var resp UpdatePtrToPtrResults 115 116 err := c.Post(`mutation { updatePtrToPtr(input: { inner: null }) { name, inner { key, value }, stupidInner { key, value }}}`, &resp) 117 require.NoError(t, err) 118 119 require.Equal(t, resp.UpdatedPtrToPtr.Name, "oldName") 120 require.Nil(t, resp.UpdatedPtrToPtr.Inner) 121 require.NotNil(t, resp.UpdatedPtrToPtr.StupidInner) 122 require.NotNil(t, ******resp.UpdatedPtrToPtr.StupidInner) 123 require.Equal(t, (******resp.UpdatedPtrToPtr.StupidInner).Key, "oldStupidKey") 124 require.Equal(t, (******resp.UpdatedPtrToPtr.StupidInner).Value, "oldStupidValue") 125 }) 126 127 t.Run("many pointers input non-null", func(t *testing.T) { 128 var resp UpdatePtrToPtrResults 129 130 err := c.Post(`mutation { 131 updatePtrToPtr(input: { 132 stupidInner: { 133 key: "newKey" 134 value: "newValue" 135 } 136 }) 137 { name, inner { key, value }, stupidInner { key, value }} 138 }`, &resp) 139 require.NoError(t, err) 140 141 require.Equal(t, resp.UpdatedPtrToPtr.Name, "oldName") 142 require.NotNil(t, resp.UpdatedPtrToPtr.Inner) 143 require.Equal(t, resp.UpdatedPtrToPtr.Inner.Key, "oldKey") 144 require.Equal(t, resp.UpdatedPtrToPtr.Inner.Value, "oldValue") 145 require.NotNil(t, resp.UpdatedPtrToPtr.StupidInner) 146 require.NotNil(t, ******resp.UpdatedPtrToPtr.StupidInner) 147 require.Equal(t, (******resp.UpdatedPtrToPtr.StupidInner).Key, "newKey") 148 require.Equal(t, (******resp.UpdatedPtrToPtr.StupidInner).Value, "newValue") 149 }) 150 151 t.Run("many pointers input null", func(t *testing.T) { 152 var resp UpdatePtrToPtrResults 153 154 err := c.Post(`mutation { updatePtrToPtr(input: { stupidInner: null }) { name, inner { key, value }, stupidInner { key, value }}}`, &resp) 155 require.NoError(t, err) 156 157 require.Equal(t, resp.UpdatedPtrToPtr.Name, "oldName") 158 require.NotNil(t, resp.UpdatedPtrToPtr.Inner) 159 require.Equal(t, resp.UpdatedPtrToPtr.Inner.Key, "oldKey") 160 require.Equal(t, resp.UpdatedPtrToPtr.Inner.Value, "oldValue") 161 require.Nil(t, resp.UpdatedPtrToPtr.StupidInner) 162 }) 163 } 164 165 func nest7(in *PtrToPtrInner) *******PtrToPtrInner { 166 si2 := &in 167 si3 := &si2 168 si4 := &si3 169 si5 := &si4 170 si6 := &si5 171 si7 := &si6 172 173 return si7 174 }