github.com/99designs/gqlgen@v0.17.45/plugin/federation/testdata/allthethings/generated/federation.go (about) 1 // Code generated by github.com/99designs/gqlgen, DO NOT EDIT. 2 3 package generated 4 5 import ( 6 "context" 7 "errors" 8 "fmt" 9 "strings" 10 "sync" 11 12 "github.com/99designs/gqlgen/plugin/federation/fedruntime" 13 "github.com/99designs/gqlgen/plugin/federation/testdata/allthethings/model" 14 ) 15 16 var ( 17 ErrUnknownType = errors.New("unknown type") 18 ErrTypeNotFound = errors.New("type not found") 19 ) 20 21 func (ec *executionContext) __resolve__service(ctx context.Context) (fedruntime.Service, error) { 22 if ec.DisableIntrospection { 23 return fedruntime.Service{}, errors.New("federated introspection disabled") 24 } 25 26 var sdl []string 27 28 for _, src := range sources { 29 if src.BuiltIn { 30 continue 31 } 32 sdl = append(sdl, src.Input) 33 } 34 35 return fedruntime.Service{ 36 SDL: strings.Join(sdl, "\n"), 37 }, nil 38 } 39 40 func (ec *executionContext) __resolve_entities(ctx context.Context, representations []map[string]interface{}) []fedruntime.Entity { 41 list := make([]fedruntime.Entity, len(representations)) 42 43 repsMap := map[string]struct { 44 i []int 45 r []map[string]interface{} 46 }{} 47 48 // We group entities by typename so that we can parallelize their resolution. 49 // This is particularly helpful when there are entity groups in multi mode. 50 buildRepresentationGroups := func(reps []map[string]interface{}) { 51 for i, rep := range reps { 52 typeName, ok := rep["__typename"].(string) 53 if !ok { 54 // If there is no __typename, we just skip the representation; 55 // we just won't be resolving these unknown types. 56 ec.Error(ctx, errors.New("__typename must be an existing string")) 57 continue 58 } 59 60 _r := repsMap[typeName] 61 _r.i = append(_r.i, i) 62 _r.r = append(_r.r, rep) 63 repsMap[typeName] = _r 64 } 65 } 66 67 isMulti := func(typeName string) bool { 68 switch typeName { 69 case "MultiHelloMultiKey": 70 return true 71 default: 72 return false 73 } 74 } 75 76 resolveEntity := func(ctx context.Context, typeName string, rep map[string]interface{}, idx []int, i int) (err error) { 77 // we need to do our own panic handling, because we may be called in a 78 // goroutine, where the usual panic handling can't catch us 79 defer func() { 80 if r := recover(); r != nil { 81 err = ec.Recover(ctx, r) 82 } 83 }() 84 85 switch typeName { 86 case "ExternalExtension": 87 resolverName, err := entityResolverNameForExternalExtension(ctx, rep) 88 if err != nil { 89 return fmt.Errorf(`finding resolver for Entity "ExternalExtension": %w`, err) 90 } 91 switch resolverName { 92 93 case "findExternalExtensionByUpc": 94 id0, err := ec.unmarshalNString2string(ctx, rep["upc"]) 95 if err != nil { 96 return fmt.Errorf(`unmarshalling param 0 for findExternalExtensionByUpc(): %w`, err) 97 } 98 entity, err := ec.resolvers.Entity().FindExternalExtensionByUpc(ctx, id0) 99 if err != nil { 100 return fmt.Errorf(`resolving Entity "ExternalExtension": %w`, err) 101 } 102 103 list[idx[i]] = entity 104 return nil 105 } 106 case "Hello": 107 resolverName, err := entityResolverNameForHello(ctx, rep) 108 if err != nil { 109 return fmt.Errorf(`finding resolver for Entity "Hello": %w`, err) 110 } 111 switch resolverName { 112 113 case "findHelloByName": 114 id0, err := ec.unmarshalNString2string(ctx, rep["name"]) 115 if err != nil { 116 return fmt.Errorf(`unmarshalling param 0 for findHelloByName(): %w`, err) 117 } 118 entity, err := ec.resolvers.Entity().FindHelloByName(ctx, id0) 119 if err != nil { 120 return fmt.Errorf(`resolving Entity "Hello": %w`, err) 121 } 122 123 list[idx[i]] = entity 124 return nil 125 } 126 case "NestedKey": 127 resolverName, err := entityResolverNameForNestedKey(ctx, rep) 128 if err != nil { 129 return fmt.Errorf(`finding resolver for Entity "NestedKey": %w`, err) 130 } 131 switch resolverName { 132 133 case "findNestedKeyByIDAndHelloName": 134 id0, err := ec.unmarshalNString2string(ctx, rep["id"]) 135 if err != nil { 136 return fmt.Errorf(`unmarshalling param 0 for findNestedKeyByIDAndHelloName(): %w`, err) 137 } 138 id1, err := ec.unmarshalNString2string(ctx, rep["hello"].(map[string]interface{})["name"]) 139 if err != nil { 140 return fmt.Errorf(`unmarshalling param 1 for findNestedKeyByIDAndHelloName(): %w`, err) 141 } 142 entity, err := ec.resolvers.Entity().FindNestedKeyByIDAndHelloName(ctx, id0, id1) 143 if err != nil { 144 return fmt.Errorf(`resolving Entity "NestedKey": %w`, err) 145 } 146 147 list[idx[i]] = entity 148 return nil 149 } 150 case "VeryNestedKey": 151 resolverName, err := entityResolverNameForVeryNestedKey(ctx, rep) 152 if err != nil { 153 return fmt.Errorf(`finding resolver for Entity "VeryNestedKey": %w`, err) 154 } 155 switch resolverName { 156 157 case "findVeryNestedKeyByIDAndHelloNameAndWorldFooAndWorldBarAndMoreWorldFoo": 158 id0, err := ec.unmarshalNString2string(ctx, rep["id"]) 159 if err != nil { 160 return fmt.Errorf(`unmarshalling param 0 for findVeryNestedKeyByIDAndHelloNameAndWorldFooAndWorldBarAndMoreWorldFoo(): %w`, err) 161 } 162 id1, err := ec.unmarshalNString2string(ctx, rep["hello"].(map[string]interface{})["name"]) 163 if err != nil { 164 return fmt.Errorf(`unmarshalling param 1 for findVeryNestedKeyByIDAndHelloNameAndWorldFooAndWorldBarAndMoreWorldFoo(): %w`, err) 165 } 166 id2, err := ec.unmarshalNString2string(ctx, rep["world"].(map[string]interface{})["foo"]) 167 if err != nil { 168 return fmt.Errorf(`unmarshalling param 2 for findVeryNestedKeyByIDAndHelloNameAndWorldFooAndWorldBarAndMoreWorldFoo(): %w`, err) 169 } 170 id3, err := ec.unmarshalNInt2int(ctx, rep["world"].(map[string]interface{})["bar"]) 171 if err != nil { 172 return fmt.Errorf(`unmarshalling param 3 for findVeryNestedKeyByIDAndHelloNameAndWorldFooAndWorldBarAndMoreWorldFoo(): %w`, err) 173 } 174 id4, err := ec.unmarshalNString2string(ctx, rep["more"].(map[string]interface{})["world"].(map[string]interface{})["foo"]) 175 if err != nil { 176 return fmt.Errorf(`unmarshalling param 4 for findVeryNestedKeyByIDAndHelloNameAndWorldFooAndWorldBarAndMoreWorldFoo(): %w`, err) 177 } 178 entity, err := ec.resolvers.Entity().FindVeryNestedKeyByIDAndHelloNameAndWorldFooAndWorldBarAndMoreWorldFoo(ctx, id0, id1, id2, id3, id4) 179 if err != nil { 180 return fmt.Errorf(`resolving Entity "VeryNestedKey": %w`, err) 181 } 182 183 entity.ID, err = ec.unmarshalNString2string(ctx, rep["id"]) 184 if err != nil { 185 return err 186 } 187 entity.Hello.Secondary, err = ec.unmarshalNString2string(ctx, rep["hello"].(map[string]interface{})["secondary"]) 188 if err != nil { 189 return err 190 } 191 list[idx[i]] = entity 192 return nil 193 } 194 case "World": 195 resolverName, err := entityResolverNameForWorld(ctx, rep) 196 if err != nil { 197 return fmt.Errorf(`finding resolver for Entity "World": %w`, err) 198 } 199 switch resolverName { 200 201 case "findWorldByFoo": 202 id0, err := ec.unmarshalNString2string(ctx, rep["foo"]) 203 if err != nil { 204 return fmt.Errorf(`unmarshalling param 0 for findWorldByFoo(): %w`, err) 205 } 206 entity, err := ec.resolvers.Entity().FindWorldByFoo(ctx, id0) 207 if err != nil { 208 return fmt.Errorf(`resolving Entity "World": %w`, err) 209 } 210 211 list[idx[i]] = entity 212 return nil 213 case "findWorldByBar": 214 id0, err := ec.unmarshalNInt2int(ctx, rep["bar"]) 215 if err != nil { 216 return fmt.Errorf(`unmarshalling param 0 for findWorldByBar(): %w`, err) 217 } 218 entity, err := ec.resolvers.Entity().FindWorldByBar(ctx, id0) 219 if err != nil { 220 return fmt.Errorf(`resolving Entity "World": %w`, err) 221 } 222 223 list[idx[i]] = entity 224 return nil 225 } 226 227 } 228 return fmt.Errorf("%w: %s", ErrUnknownType, typeName) 229 } 230 231 resolveManyEntities := func(ctx context.Context, typeName string, reps []map[string]interface{}, idx []int) (err error) { 232 // we need to do our own panic handling, because we may be called in a 233 // goroutine, where the usual panic handling can't catch us 234 defer func() { 235 if r := recover(); r != nil { 236 err = ec.Recover(ctx, r) 237 } 238 }() 239 240 switch typeName { 241 242 case "MultiHelloMultiKey": 243 resolverName, err := entityResolverNameForMultiHelloMultiKey(ctx, reps[0]) 244 if err != nil { 245 return fmt.Errorf(`finding resolver for Entity "MultiHelloMultiKey": %w`, err) 246 } 247 switch resolverName { 248 249 case "findManyMultiHelloMultiKeyByNames": 250 _reps := make([]*model.MultiHelloMultiKeyByNamesInput, len(reps)) 251 252 for i, rep := range reps { 253 id0, err := ec.unmarshalNString2string(ctx, rep["name"]) 254 if err != nil { 255 return errors.New(fmt.Sprintf("Field %s undefined in schema.", "name")) 256 } 257 258 _reps[i] = &model.MultiHelloMultiKeyByNamesInput{ 259 Name: id0, 260 } 261 } 262 263 entities, err := ec.resolvers.Entity().FindManyMultiHelloMultiKeyByNames(ctx, _reps) 264 if err != nil { 265 return err 266 } 267 268 for i, entity := range entities { 269 list[idx[i]] = entity 270 } 271 return nil 272 273 case "findManyMultiHelloMultiKeyByKey2s": 274 _reps := make([]*model.MultiHelloMultiKeyByKey2sInput, len(reps)) 275 276 for i, rep := range reps { 277 id0, err := ec.unmarshalNString2string(ctx, rep["key2"]) 278 if err != nil { 279 return errors.New(fmt.Sprintf("Field %s undefined in schema.", "key2")) 280 } 281 282 _reps[i] = &model.MultiHelloMultiKeyByKey2sInput{ 283 Key2: id0, 284 } 285 } 286 287 entities, err := ec.resolvers.Entity().FindManyMultiHelloMultiKeyByKey2s(ctx, _reps) 288 if err != nil { 289 return err 290 } 291 292 for i, entity := range entities { 293 list[idx[i]] = entity 294 } 295 return nil 296 297 default: 298 return fmt.Errorf("unknown resolver: %s", resolverName) 299 } 300 301 default: 302 return errors.New("unknown type: " + typeName) 303 } 304 } 305 306 resolveEntityGroup := func(typeName string, reps []map[string]interface{}, idx []int) { 307 if isMulti(typeName) { 308 err := resolveManyEntities(ctx, typeName, reps, idx) 309 if err != nil { 310 ec.Error(ctx, err) 311 } 312 } else { 313 // if there are multiple entities to resolve, parallelize (similar to 314 // graphql.FieldSet.Dispatch) 315 var e sync.WaitGroup 316 e.Add(len(reps)) 317 for i, rep := range reps { 318 i, rep := i, rep 319 go func(i int, rep map[string]interface{}) { 320 err := resolveEntity(ctx, typeName, rep, idx, i) 321 if err != nil { 322 ec.Error(ctx, err) 323 } 324 e.Done() 325 }(i, rep) 326 } 327 e.Wait() 328 } 329 } 330 buildRepresentationGroups(representations) 331 332 switch len(repsMap) { 333 case 0: 334 return list 335 case 1: 336 for typeName, reps := range repsMap { 337 resolveEntityGroup(typeName, reps.r, reps.i) 338 } 339 return list 340 default: 341 var g sync.WaitGroup 342 g.Add(len(repsMap)) 343 for typeName, reps := range repsMap { 344 go func(typeName string, reps []map[string]interface{}, idx []int) { 345 resolveEntityGroup(typeName, reps, idx) 346 g.Done() 347 }(typeName, reps.r, reps.i) 348 } 349 g.Wait() 350 return list 351 } 352 } 353 354 func entityResolverNameForExternalExtension(ctx context.Context, rep map[string]interface{}) (string, error) { 355 for { 356 var ( 357 m map[string]interface{} 358 val interface{} 359 ok bool 360 ) 361 _ = val 362 m = rep 363 if _, ok = m["upc"]; !ok { 364 break 365 } 366 return "findExternalExtensionByUpc", nil 367 } 368 return "", fmt.Errorf("%w for ExternalExtension", ErrTypeNotFound) 369 } 370 371 func entityResolverNameForHello(ctx context.Context, rep map[string]interface{}) (string, error) { 372 for { 373 var ( 374 m map[string]interface{} 375 val interface{} 376 ok bool 377 ) 378 _ = val 379 m = rep 380 if _, ok = m["name"]; !ok { 381 break 382 } 383 return "findHelloByName", nil 384 } 385 return "", fmt.Errorf("%w for Hello", ErrTypeNotFound) 386 } 387 388 func entityResolverNameForMultiHelloMultiKey(ctx context.Context, rep map[string]interface{}) (string, error) { 389 for { 390 var ( 391 m map[string]interface{} 392 val interface{} 393 ok bool 394 ) 395 _ = val 396 m = rep 397 if _, ok = m["name"]; !ok { 398 break 399 } 400 return "findManyMultiHelloMultiKeyByNames", nil 401 } 402 for { 403 var ( 404 m map[string]interface{} 405 val interface{} 406 ok bool 407 ) 408 _ = val 409 m = rep 410 if _, ok = m["key2"]; !ok { 411 break 412 } 413 return "findManyMultiHelloMultiKeyByKey2s", nil 414 } 415 return "", fmt.Errorf("%w for MultiHelloMultiKey", ErrTypeNotFound) 416 } 417 418 func entityResolverNameForNestedKey(ctx context.Context, rep map[string]interface{}) (string, error) { 419 for { 420 var ( 421 m map[string]interface{} 422 val interface{} 423 ok bool 424 ) 425 _ = val 426 m = rep 427 if _, ok = m["id"]; !ok { 428 break 429 } 430 m = rep 431 if val, ok = m["hello"]; !ok { 432 break 433 } 434 if m, ok = val.(map[string]interface{}); !ok { 435 break 436 } 437 if _, ok = m["name"]; !ok { 438 break 439 } 440 return "findNestedKeyByIDAndHelloName", nil 441 } 442 return "", fmt.Errorf("%w for NestedKey", ErrTypeNotFound) 443 } 444 445 func entityResolverNameForVeryNestedKey(ctx context.Context, rep map[string]interface{}) (string, error) { 446 for { 447 var ( 448 m map[string]interface{} 449 val interface{} 450 ok bool 451 ) 452 _ = val 453 m = rep 454 if _, ok = m["id"]; !ok { 455 break 456 } 457 m = rep 458 if val, ok = m["hello"]; !ok { 459 break 460 } 461 if m, ok = val.(map[string]interface{}); !ok { 462 break 463 } 464 if _, ok = m["name"]; !ok { 465 break 466 } 467 m = rep 468 if val, ok = m["world"]; !ok { 469 break 470 } 471 if m, ok = val.(map[string]interface{}); !ok { 472 break 473 } 474 if _, ok = m["foo"]; !ok { 475 break 476 } 477 m = rep 478 if val, ok = m["world"]; !ok { 479 break 480 } 481 if m, ok = val.(map[string]interface{}); !ok { 482 break 483 } 484 if _, ok = m["bar"]; !ok { 485 break 486 } 487 m = rep 488 if val, ok = m["more"]; !ok { 489 break 490 } 491 if m, ok = val.(map[string]interface{}); !ok { 492 break 493 } 494 if val, ok = m["world"]; !ok { 495 break 496 } 497 if m, ok = val.(map[string]interface{}); !ok { 498 break 499 } 500 if _, ok = m["foo"]; !ok { 501 break 502 } 503 return "findVeryNestedKeyByIDAndHelloNameAndWorldFooAndWorldBarAndMoreWorldFoo", nil 504 } 505 return "", fmt.Errorf("%w for VeryNestedKey", ErrTypeNotFound) 506 } 507 508 func entityResolverNameForWorld(ctx context.Context, rep map[string]interface{}) (string, error) { 509 for { 510 var ( 511 m map[string]interface{} 512 val interface{} 513 ok bool 514 ) 515 _ = val 516 m = rep 517 if _, ok = m["foo"]; !ok { 518 break 519 } 520 return "findWorldByFoo", nil 521 } 522 for { 523 var ( 524 m map[string]interface{} 525 val interface{} 526 ok bool 527 ) 528 _ = val 529 m = rep 530 if _, ok = m["bar"]; !ok { 531 break 532 } 533 return "findWorldByBar", nil 534 } 535 return "", fmt.Errorf("%w for World", ErrTypeNotFound) 536 }