github.com/Desuuuu/genqlient@v0.5.3/generate/testdata/snapshots/TestGenerate-Flatten.graphql-Flatten.graphql.go (about) 1 // Code generated by github.com/Desuuuu/genqlient, DO NOT EDIT. 2 3 package test 4 5 import ( 6 "encoding/json" 7 "fmt" 8 9 "github.com/Desuuuu/genqlient/graphql" 10 "github.com/Desuuuu/genqlient/internal/testutil" 11 ) 12 13 // ChildVideoFields includes the GraphQL fields of Video requested by the fragment ChildVideoFields. 14 type ChildVideoFields struct { 15 // ID is documented in the Content interface. 16 Id testutil.ID `json:"id"` 17 Name string `json:"name"` 18 } 19 20 // GetId returns ChildVideoFields.Id, and is useful for accessing the field via an interface. 21 func (v *ChildVideoFields) GetId() testutil.ID { return v.Id } 22 23 // GetName returns ChildVideoFields.Name, and is useful for accessing the field via an interface. 24 func (v *ChildVideoFields) GetName() string { return v.Name } 25 26 // ContentFields includes the GraphQL fields of Content requested by the fragment ContentFields. 27 // The GraphQL type's documentation follows. 28 // 29 // Content is implemented by various types like Article, Video, and Topic. 30 // 31 // ContentFields is implemented by the following types: 32 // ContentFieldsArticle 33 // ContentFieldsVideo 34 // ContentFieldsTopic 35 type ContentFields interface { 36 implementsGraphQLInterfaceContentFields() 37 // GetName returns the interface-field "name" from its implementation. 38 GetName() string 39 // GetUrl returns the interface-field "url" from its implementation. 40 GetUrl() string 41 GetContentFieldsArticle() (*ContentFieldsArticle, bool) 42 GetContentFieldsVideo() (*ContentFieldsVideo, bool) 43 GetContentFieldsTopic() (*ContentFieldsTopic, bool) 44 } 45 46 func (v *ContentFieldsArticle) implementsGraphQLInterfaceContentFields() {} 47 func (v *ContentFieldsArticle) GetContentFieldsArticle() (*ContentFieldsArticle, bool) { 48 return v, true 49 } 50 func (v *ContentFieldsArticle) GetContentFieldsVideo() (*ContentFieldsVideo, bool) { 51 return nil, false 52 } 53 func (v *ContentFieldsArticle) GetContentFieldsTopic() (*ContentFieldsTopic, bool) { 54 return nil, false 55 } 56 func (v *ContentFieldsVideo) implementsGraphQLInterfaceContentFields() {} 57 func (v *ContentFieldsVideo) GetContentFieldsArticle() (*ContentFieldsArticle, bool) { 58 return nil, false 59 } 60 func (v *ContentFieldsVideo) GetContentFieldsVideo() (*ContentFieldsVideo, bool) { 61 return v, true 62 } 63 func (v *ContentFieldsVideo) GetContentFieldsTopic() (*ContentFieldsTopic, bool) { 64 return nil, false 65 } 66 func (v *ContentFieldsTopic) implementsGraphQLInterfaceContentFields() {} 67 func (v *ContentFieldsTopic) GetContentFieldsArticle() (*ContentFieldsArticle, bool) { 68 return nil, false 69 } 70 func (v *ContentFieldsTopic) GetContentFieldsVideo() (*ContentFieldsVideo, bool) { 71 return nil, false 72 } 73 func (v *ContentFieldsTopic) GetContentFieldsTopic() (*ContentFieldsTopic, bool) { 74 return v, true 75 } 76 77 func __unmarshalContentFields(b []byte, v *ContentFields) error { 78 if string(b) == "null" { 79 return nil 80 } 81 82 var tn struct { 83 TypeName string `json:"__typename"` 84 } 85 err := json.Unmarshal(b, &tn) 86 if err != nil { 87 return err 88 } 89 90 switch tn.TypeName { 91 case "Article": 92 *v = new(ContentFieldsArticle) 93 return json.Unmarshal(b, *v) 94 case "Video": 95 *v = new(ContentFieldsVideo) 96 return json.Unmarshal(b, *v) 97 case "Topic": 98 *v = new(ContentFieldsTopic) 99 return json.Unmarshal(b, *v) 100 case "": 101 return fmt.Errorf( 102 "response was missing Content.__typename") 103 default: 104 return fmt.Errorf( 105 `unexpected concrete type for ContentFields: "%v"`, tn.TypeName) 106 } 107 } 108 109 func __marshalContentFields(v *ContentFields) ([]byte, error) { 110 111 var typename string 112 switch v := (*v).(type) { 113 case *ContentFieldsArticle: 114 typename = "Article" 115 116 result := struct { 117 TypeName string `json:"__typename"` 118 *ContentFieldsArticle 119 }{typename, v} 120 return json.Marshal(result) 121 case *ContentFieldsVideo: 122 typename = "Video" 123 124 result := struct { 125 TypeName string `json:"__typename"` 126 *ContentFieldsVideo 127 }{typename, v} 128 return json.Marshal(result) 129 case *ContentFieldsTopic: 130 typename = "Topic" 131 132 result := struct { 133 TypeName string `json:"__typename"` 134 *ContentFieldsTopic 135 }{typename, v} 136 return json.Marshal(result) 137 case nil: 138 return []byte("null"), nil 139 default: 140 return nil, fmt.Errorf( 141 `unexpected concrete type for ContentFields: "%T"`, v) 142 } 143 } 144 145 // ContentFields includes the GraphQL fields of Article requested by the fragment ContentFields. 146 // The GraphQL type's documentation follows. 147 // 148 // Content is implemented by various types like Article, Video, and Topic. 149 type ContentFieldsArticle struct { 150 Name string `json:"name"` 151 Url string `json:"url"` 152 } 153 154 // GetName returns ContentFieldsArticle.Name, and is useful for accessing the field via an interface. 155 func (v *ContentFieldsArticle) GetName() string { return v.Name } 156 157 // GetUrl returns ContentFieldsArticle.Url, and is useful for accessing the field via an interface. 158 func (v *ContentFieldsArticle) GetUrl() string { return v.Url } 159 160 // ContentFields includes the GraphQL fields of Topic requested by the fragment ContentFields. 161 // The GraphQL type's documentation follows. 162 // 163 // Content is implemented by various types like Article, Video, and Topic. 164 type ContentFieldsTopic struct { 165 Name string `json:"name"` 166 Url string `json:"url"` 167 } 168 169 // GetName returns ContentFieldsTopic.Name, and is useful for accessing the field via an interface. 170 func (v *ContentFieldsTopic) GetName() string { return v.Name } 171 172 // GetUrl returns ContentFieldsTopic.Url, and is useful for accessing the field via an interface. 173 func (v *ContentFieldsTopic) GetUrl() string { return v.Url } 174 175 // ContentFields includes the GraphQL fields of Video requested by the fragment ContentFields. 176 // The GraphQL type's documentation follows. 177 // 178 // Content is implemented by various types like Article, Video, and Topic. 179 type ContentFieldsVideo struct { 180 Name string `json:"name"` 181 Url string `json:"url"` 182 } 183 184 // GetName returns ContentFieldsVideo.Name, and is useful for accessing the field via an interface. 185 func (v *ContentFieldsVideo) GetName() string { return v.Name } 186 187 // GetUrl returns ContentFieldsVideo.Url, and is useful for accessing the field via an interface. 188 func (v *ContentFieldsVideo) GetUrl() string { return v.Url } 189 190 // InnerQueryFragment includes the GraphQL fields of Query requested by the fragment InnerQueryFragment. 191 // The GraphQL type's documentation follows. 192 // 193 // Query's description is probably ignored by almost all callers. 194 type InnerQueryFragment struct { 195 RandomVideo VideoFields `json:"randomVideo"` 196 RandomItem ContentFields `json:"-"` 197 OtherVideo ContentFieldsVideo `json:"otherVideo"` 198 } 199 200 // GetRandomVideo returns InnerQueryFragment.RandomVideo, and is useful for accessing the field via an interface. 201 func (v *InnerQueryFragment) GetRandomVideo() VideoFields { return v.RandomVideo } 202 203 // GetRandomItem returns InnerQueryFragment.RandomItem, and is useful for accessing the field via an interface. 204 func (v *InnerQueryFragment) GetRandomItem() ContentFields { return v.RandomItem } 205 206 // GetOtherVideo returns InnerQueryFragment.OtherVideo, and is useful for accessing the field via an interface. 207 func (v *InnerQueryFragment) GetOtherVideo() ContentFieldsVideo { return v.OtherVideo } 208 209 func (v *InnerQueryFragment) UnmarshalJSON(b []byte) error { 210 211 if string(b) == "null" { 212 return nil 213 } 214 215 var firstPass struct { 216 *InnerQueryFragment 217 RandomItem json.RawMessage `json:"randomItem"` 218 graphql.NoUnmarshalJSON 219 } 220 firstPass.InnerQueryFragment = v 221 222 err := json.Unmarshal(b, &firstPass) 223 if err != nil { 224 return err 225 } 226 227 { 228 dst := &v.RandomItem 229 src := firstPass.RandomItem 230 if len(src) != 0 && string(src) != "null" { 231 err = __unmarshalContentFields( 232 src, dst) 233 if err != nil { 234 return fmt.Errorf( 235 "Unable to unmarshal InnerQueryFragment.RandomItem: %w", err) 236 } 237 } 238 } 239 return nil 240 } 241 242 type __premarshalInnerQueryFragment struct { 243 RandomVideo VideoFields `json:"randomVideo"` 244 245 RandomItem json.RawMessage `json:"randomItem"` 246 247 OtherVideo ContentFieldsVideo `json:"otherVideo"` 248 } 249 250 func (v *InnerQueryFragment) MarshalJSON() ([]byte, error) { 251 premarshaled, err := v.__premarshalJSON() 252 if err != nil { 253 return nil, err 254 } 255 return json.Marshal(premarshaled) 256 } 257 258 func (v *InnerQueryFragment) __premarshalJSON() (*__premarshalInnerQueryFragment, error) { 259 var retval __premarshalInnerQueryFragment 260 261 retval.RandomVideo = v.RandomVideo 262 { 263 264 dst := &retval.RandomItem 265 src := v.RandomItem 266 var err error 267 *dst, err = __marshalContentFields( 268 &src) 269 if err != nil { 270 return nil, fmt.Errorf( 271 "Unable to marshal InnerQueryFragment.RandomItem: %w", err) 272 } 273 } 274 retval.OtherVideo = v.OtherVideo 275 return &retval, nil 276 } 277 278 // VideoFields includes the GraphQL fields of Video requested by the fragment VideoFields. 279 type VideoFields struct { 280 // ID is documented in the Content interface. 281 Id testutil.ID `json:"id"` 282 Parent VideoFieldsParentTopic `json:"parent"` 283 } 284 285 // GetId returns VideoFields.Id, and is useful for accessing the field via an interface. 286 func (v *VideoFields) GetId() testutil.ID { return v.Id } 287 288 // GetParent returns VideoFields.Parent, and is useful for accessing the field via an interface. 289 func (v *VideoFields) GetParent() VideoFieldsParentTopic { return v.Parent } 290 291 // VideoFieldsParentTopic includes the requested fields of the GraphQL type Topic. 292 type VideoFieldsParentTopic struct { 293 VideoChildren []ChildVideoFields `json:"videoChildren"` 294 } 295 296 // GetVideoChildren returns VideoFieldsParentTopic.VideoChildren, and is useful for accessing the field via an interface. 297 func (v *VideoFieldsParentTopic) GetVideoChildren() []ChildVideoFields { return v.VideoChildren } 298 299 func ComplexNamedFragments( 300 client graphql.Client, 301 ) (*InnerQueryFragment, error) { 302 req := &graphql.Request{ 303 OpName: "ComplexNamedFragments", 304 Query: ` 305 query ComplexNamedFragments { 306 ... QueryFragment 307 } 308 fragment QueryFragment on Query { 309 ... InnerQueryFragment 310 } 311 fragment InnerQueryFragment on Query { 312 randomVideo { 313 ... VideoFields 314 } 315 randomItem { 316 __typename 317 ... ContentFields 318 } 319 otherVideo: randomVideo { 320 ... ContentFields 321 } 322 } 323 fragment VideoFields on Video { 324 id 325 parent { 326 videoChildren { 327 ... ChildVideoFields 328 } 329 } 330 } 331 fragment ContentFields on Content { 332 name 333 url 334 } 335 fragment ChildVideoFields on Video { 336 id 337 name 338 } 339 `, 340 } 341 var err error 342 343 var data InnerQueryFragment 344 resp := &graphql.Response{Data: &data} 345 346 err = client.MakeRequest( 347 nil, 348 req, 349 resp, 350 ) 351 352 return &data, err 353 } 354