federation.go 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. // Code generated by github.com/99designs/gqlgen, DO NOT EDIT.
  2. package generated
  3. import (
  4. "context"
  5. "errors"
  6. "fmt"
  7. "strings"
  8. "sync"
  9. "github.com/99designs/gqlgen/plugin/federation/fedruntime"
  10. )
  11. var (
  12. ErrUnknownType = errors.New("unknown type")
  13. ErrTypeNotFound = errors.New("type not found")
  14. )
  15. func (ec *executionContext) __resolve__service(ctx context.Context) (fedruntime.Service, error) {
  16. if ec.DisableIntrospection {
  17. return fedruntime.Service{}, errors.New("federated introspection disabled")
  18. }
  19. var sdl []string
  20. for _, src := range sources {
  21. if src.BuiltIn {
  22. continue
  23. }
  24. sdl = append(sdl, src.Input)
  25. }
  26. return fedruntime.Service{
  27. SDL: strings.Join(sdl, "\n"),
  28. }, nil
  29. }
  30. func (ec *executionContext) __resolve_entities(ctx context.Context, representations []map[string]interface{}) []fedruntime.Entity {
  31. list := make([]fedruntime.Entity, len(representations))
  32. repsMap := map[string]struct {
  33. i []int
  34. r []map[string]interface{}
  35. }{}
  36. // We group entities by typename so that we can parallelize their resolution.
  37. // This is particularly helpful when there are entity groups in multi mode.
  38. buildRepresentationGroups := func(reps []map[string]interface{}) {
  39. for i, rep := range reps {
  40. typeName, ok := rep["__typename"].(string)
  41. if !ok {
  42. // If there is no __typename, we just skip the representation;
  43. // we just won't be resolving these unknown types.
  44. ec.Error(ctx, errors.New("__typename must be an existing string"))
  45. continue
  46. }
  47. _r := repsMap[typeName]
  48. _r.i = append(_r.i, i)
  49. _r.r = append(_r.r, rep)
  50. repsMap[typeName] = _r
  51. }
  52. }
  53. isMulti := func(typeName string) bool {
  54. switch typeName {
  55. default:
  56. return false
  57. }
  58. }
  59. resolveEntity := func(ctx context.Context, typeName string, rep map[string]interface{}, idx []int, i int) (err error) {
  60. // we need to do our own panic handling, because we may be called in a
  61. // goroutine, where the usual panic handling can't catch us
  62. defer func() {
  63. if r := recover(); r != nil {
  64. err = ec.Recover(ctx, r)
  65. }
  66. }()
  67. switch typeName {
  68. case "App":
  69. resolverName, err := entityResolverNameForApp(ctx, rep)
  70. if err != nil {
  71. return fmt.Errorf(`finding resolver for Entity "App": %w`, err)
  72. }
  73. switch resolverName {
  74. case "findAppByID":
  75. id0, err := ec.unmarshalNID2string(ctx, rep["id"])
  76. if err != nil {
  77. return fmt.Errorf(`unmarshalling param 0 for findAppByID(): %w`, err)
  78. }
  79. entity, err := ec.resolvers.Entity().FindAppByID(ctx, id0)
  80. if err != nil {
  81. return fmt.Errorf(`resolving Entity "App": %w`, err)
  82. }
  83. list[idx[i]] = entity
  84. return nil
  85. }
  86. case "Job":
  87. resolverName, err := entityResolverNameForJob(ctx, rep)
  88. if err != nil {
  89. return fmt.Errorf(`finding resolver for Entity "Job": %w`, err)
  90. }
  91. switch resolverName {
  92. case "findJobByID":
  93. id0, err := ec.unmarshalNID2string(ctx, rep["id"])
  94. if err != nil {
  95. return fmt.Errorf(`unmarshalling param 0 for findJobByID(): %w`, err)
  96. }
  97. entity, err := ec.resolvers.Entity().FindJobByID(ctx, id0)
  98. if err != nil {
  99. return fmt.Errorf(`resolving Entity "Job": %w`, err)
  100. }
  101. list[idx[i]] = entity
  102. return nil
  103. }
  104. case "Market":
  105. resolverName, err := entityResolverNameForMarket(ctx, rep)
  106. if err != nil {
  107. return fmt.Errorf(`finding resolver for Entity "Market": %w`, err)
  108. }
  109. switch resolverName {
  110. case "findMarketByID":
  111. id0, err := ec.unmarshalNID2string(ctx, rep["id"])
  112. if err != nil {
  113. return fmt.Errorf(`unmarshalling param 0 for findMarketByID(): %w`, err)
  114. }
  115. entity, err := ec.resolvers.Entity().FindMarketByID(ctx, id0)
  116. if err != nil {
  117. return fmt.Errorf(`resolving Entity "Market": %w`, err)
  118. }
  119. list[idx[i]] = entity
  120. return nil
  121. }
  122. case "MarketWebPresence":
  123. resolverName, err := entityResolverNameForMarketWebPresence(ctx, rep)
  124. if err != nil {
  125. return fmt.Errorf(`finding resolver for Entity "MarketWebPresence": %w`, err)
  126. }
  127. switch resolverName {
  128. case "findMarketWebPresenceByID":
  129. id0, err := ec.unmarshalNID2string(ctx, rep["id"])
  130. if err != nil {
  131. return fmt.Errorf(`unmarshalling param 0 for findMarketWebPresenceByID(): %w`, err)
  132. }
  133. entity, err := ec.resolvers.Entity().FindMarketWebPresenceByID(ctx, id0)
  134. if err != nil {
  135. return fmt.Errorf(`resolving Entity "MarketWebPresence": %w`, err)
  136. }
  137. list[idx[i]] = entity
  138. return nil
  139. }
  140. case "Shop":
  141. resolverName, err := entityResolverNameForShop(ctx, rep)
  142. if err != nil {
  143. return fmt.Errorf(`finding resolver for Entity "Shop": %w`, err)
  144. }
  145. switch resolverName {
  146. case "findShopByID":
  147. id0, err := ec.unmarshalNID2string(ctx, rep["id"])
  148. if err != nil {
  149. return fmt.Errorf(`unmarshalling param 0 for findShopByID(): %w`, err)
  150. }
  151. entity, err := ec.resolvers.Entity().FindShopByID(ctx, id0)
  152. if err != nil {
  153. return fmt.Errorf(`resolving Entity "Shop": %w`, err)
  154. }
  155. list[idx[i]] = entity
  156. return nil
  157. }
  158. }
  159. return fmt.Errorf("%w: %s", ErrUnknownType, typeName)
  160. }
  161. resolveManyEntities := func(ctx context.Context, typeName string, reps []map[string]interface{}, idx []int) (err error) {
  162. // we need to do our own panic handling, because we may be called in a
  163. // goroutine, where the usual panic handling can't catch us
  164. defer func() {
  165. if r := recover(); r != nil {
  166. err = ec.Recover(ctx, r)
  167. }
  168. }()
  169. switch typeName {
  170. default:
  171. return errors.New("unknown type: " + typeName)
  172. }
  173. }
  174. resolveEntityGroup := func(typeName string, reps []map[string]interface{}, idx []int) {
  175. if isMulti(typeName) {
  176. err := resolveManyEntities(ctx, typeName, reps, idx)
  177. if err != nil {
  178. ec.Error(ctx, err)
  179. }
  180. } else {
  181. // if there are multiple entities to resolve, parallelize (similar to
  182. // graphql.FieldSet.Dispatch)
  183. var e sync.WaitGroup
  184. e.Add(len(reps))
  185. for i, rep := range reps {
  186. i, rep := i, rep
  187. go func(i int, rep map[string]interface{}) {
  188. err := resolveEntity(ctx, typeName, rep, idx, i)
  189. if err != nil {
  190. ec.Error(ctx, err)
  191. }
  192. e.Done()
  193. }(i, rep)
  194. }
  195. e.Wait()
  196. }
  197. }
  198. buildRepresentationGroups(representations)
  199. switch len(repsMap) {
  200. case 0:
  201. return list
  202. case 1:
  203. for typeName, reps := range repsMap {
  204. resolveEntityGroup(typeName, reps.r, reps.i)
  205. }
  206. return list
  207. default:
  208. var g sync.WaitGroup
  209. g.Add(len(repsMap))
  210. for typeName, reps := range repsMap {
  211. go func(typeName string, reps []map[string]interface{}, idx []int) {
  212. resolveEntityGroup(typeName, reps, idx)
  213. g.Done()
  214. }(typeName, reps.r, reps.i)
  215. }
  216. g.Wait()
  217. return list
  218. }
  219. }
  220. func entityResolverNameForApp(ctx context.Context, rep map[string]interface{}) (string, error) {
  221. for {
  222. var (
  223. m map[string]interface{}
  224. val interface{}
  225. ok bool
  226. )
  227. _ = val
  228. m = rep
  229. if _, ok = m["id"]; !ok {
  230. break
  231. }
  232. return "findAppByID", nil
  233. }
  234. return "", fmt.Errorf("%w for App", ErrTypeNotFound)
  235. }
  236. func entityResolverNameForJob(ctx context.Context, rep map[string]interface{}) (string, error) {
  237. for {
  238. var (
  239. m map[string]interface{}
  240. val interface{}
  241. ok bool
  242. )
  243. _ = val
  244. m = rep
  245. if _, ok = m["id"]; !ok {
  246. break
  247. }
  248. return "findJobByID", nil
  249. }
  250. return "", fmt.Errorf("%w for Job", ErrTypeNotFound)
  251. }
  252. func entityResolverNameForMarket(ctx context.Context, rep map[string]interface{}) (string, error) {
  253. for {
  254. var (
  255. m map[string]interface{}
  256. val interface{}
  257. ok bool
  258. )
  259. _ = val
  260. m = rep
  261. if _, ok = m["id"]; !ok {
  262. break
  263. }
  264. return "findMarketByID", nil
  265. }
  266. return "", fmt.Errorf("%w for Market", ErrTypeNotFound)
  267. }
  268. func entityResolverNameForMarketWebPresence(ctx context.Context, rep map[string]interface{}) (string, error) {
  269. for {
  270. var (
  271. m map[string]interface{}
  272. val interface{}
  273. ok bool
  274. )
  275. _ = val
  276. m = rep
  277. if _, ok = m["id"]; !ok {
  278. break
  279. }
  280. return "findMarketWebPresenceByID", nil
  281. }
  282. return "", fmt.Errorf("%w for MarketWebPresence", ErrTypeNotFound)
  283. }
  284. func entityResolverNameForShop(ctx context.Context, rep map[string]interface{}) (string, error) {
  285. for {
  286. var (
  287. m map[string]interface{}
  288. val interface{}
  289. ok bool
  290. )
  291. _ = val
  292. m = rep
  293. if _, ok = m["id"]; !ok {
  294. break
  295. }
  296. return "findShopByID", nil
  297. }
  298. return "", fmt.Errorf("%w for Shop", ErrTypeNotFound)
  299. }