federation.go 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  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 "Collection":
  69. resolverName, err := entityResolverNameForCollection(ctx, rep)
  70. if err != nil {
  71. return fmt.Errorf(`finding resolver for Entity "Collection": %w`, err)
  72. }
  73. switch resolverName {
  74. case "findCollectionByID":
  75. id0, err := ec.unmarshalNID2string(ctx, rep["id"])
  76. if err != nil {
  77. return fmt.Errorf(`unmarshalling param 0 for findCollectionByID(): %w`, err)
  78. }
  79. entity, err := ec.resolvers.Entity().FindCollectionByID(ctx, id0)
  80. if err != nil {
  81. return fmt.Errorf(`resolving Entity "Collection": %w`, err)
  82. }
  83. list[idx[i]] = entity
  84. return nil
  85. }
  86. case "FulfillmentService":
  87. resolverName, err := entityResolverNameForFulfillmentService(ctx, rep)
  88. if err != nil {
  89. return fmt.Errorf(`finding resolver for Entity "FulfillmentService": %w`, err)
  90. }
  91. switch resolverName {
  92. case "findFulfillmentServiceByID":
  93. id0, err := ec.unmarshalNID2string(ctx, rep["id"])
  94. if err != nil {
  95. return fmt.Errorf(`unmarshalling param 0 for findFulfillmentServiceByID(): %w`, err)
  96. }
  97. entity, err := ec.resolvers.Entity().FindFulfillmentServiceByID(ctx, id0)
  98. if err != nil {
  99. return fmt.Errorf(`resolving Entity "FulfillmentService": %w`, err)
  100. }
  101. list[idx[i]] = entity
  102. return nil
  103. }
  104. case "InventoryLevel":
  105. resolverName, err := entityResolverNameForInventoryLevel(ctx, rep)
  106. if err != nil {
  107. return fmt.Errorf(`finding resolver for Entity "InventoryLevel": %w`, err)
  108. }
  109. switch resolverName {
  110. case "findInventoryLevelByID":
  111. id0, err := ec.unmarshalNID2string(ctx, rep["id"])
  112. if err != nil {
  113. return fmt.Errorf(`unmarshalling param 0 for findInventoryLevelByID(): %w`, err)
  114. }
  115. entity, err := ec.resolvers.Entity().FindInventoryLevelByID(ctx, id0)
  116. if err != nil {
  117. return fmt.Errorf(`resolving Entity "InventoryLevel": %w`, err)
  118. }
  119. list[idx[i]] = entity
  120. return nil
  121. }
  122. case "Product":
  123. resolverName, err := entityResolverNameForProduct(ctx, rep)
  124. if err != nil {
  125. return fmt.Errorf(`finding resolver for Entity "Product": %w`, err)
  126. }
  127. switch resolverName {
  128. case "findProductByID":
  129. id0, err := ec.unmarshalNID2string(ctx, rep["id"])
  130. if err != nil {
  131. return fmt.Errorf(`unmarshalling param 0 for findProductByID(): %w`, err)
  132. }
  133. entity, err := ec.resolvers.Entity().FindProductByID(ctx, id0)
  134. if err != nil {
  135. return fmt.Errorf(`resolving Entity "Product": %w`, err)
  136. }
  137. list[idx[i]] = entity
  138. return nil
  139. }
  140. case "ProductOption":
  141. resolverName, err := entityResolverNameForProductOption(ctx, rep)
  142. if err != nil {
  143. return fmt.Errorf(`finding resolver for Entity "ProductOption": %w`, err)
  144. }
  145. switch resolverName {
  146. case "findProductOptionByID":
  147. id0, err := ec.unmarshalNID2string(ctx, rep["id"])
  148. if err != nil {
  149. return fmt.Errorf(`unmarshalling param 0 for findProductOptionByID(): %w`, err)
  150. }
  151. entity, err := ec.resolvers.Entity().FindProductOptionByID(ctx, id0)
  152. if err != nil {
  153. return fmt.Errorf(`resolving Entity "ProductOption": %w`, err)
  154. }
  155. list[idx[i]] = entity
  156. return nil
  157. }
  158. case "ProductVariant":
  159. resolverName, err := entityResolverNameForProductVariant(ctx, rep)
  160. if err != nil {
  161. return fmt.Errorf(`finding resolver for Entity "ProductVariant": %w`, err)
  162. }
  163. switch resolverName {
  164. case "findProductVariantByID":
  165. id0, err := ec.unmarshalNID2string(ctx, rep["id"])
  166. if err != nil {
  167. return fmt.Errorf(`unmarshalling param 0 for findProductVariantByID(): %w`, err)
  168. }
  169. entity, err := ec.resolvers.Entity().FindProductVariantByID(ctx, id0)
  170. if err != nil {
  171. return fmt.Errorf(`resolving Entity "ProductVariant": %w`, err)
  172. }
  173. list[idx[i]] = entity
  174. return nil
  175. }
  176. }
  177. return fmt.Errorf("%w: %s", ErrUnknownType, typeName)
  178. }
  179. resolveManyEntities := func(ctx context.Context, typeName string, reps []map[string]interface{}, idx []int) (err error) {
  180. // we need to do our own panic handling, because we may be called in a
  181. // goroutine, where the usual panic handling can't catch us
  182. defer func() {
  183. if r := recover(); r != nil {
  184. err = ec.Recover(ctx, r)
  185. }
  186. }()
  187. switch typeName {
  188. default:
  189. return errors.New("unknown type: " + typeName)
  190. }
  191. }
  192. resolveEntityGroup := func(typeName string, reps []map[string]interface{}, idx []int) {
  193. if isMulti(typeName) {
  194. err := resolveManyEntities(ctx, typeName, reps, idx)
  195. if err != nil {
  196. ec.Error(ctx, err)
  197. }
  198. } else {
  199. // if there are multiple entities to resolve, parallelize (similar to
  200. // graphql.FieldSet.Dispatch)
  201. var e sync.WaitGroup
  202. e.Add(len(reps))
  203. for i, rep := range reps {
  204. i, rep := i, rep
  205. go func(i int, rep map[string]interface{}) {
  206. err := resolveEntity(ctx, typeName, rep, idx, i)
  207. if err != nil {
  208. ec.Error(ctx, err)
  209. }
  210. e.Done()
  211. }(i, rep)
  212. }
  213. e.Wait()
  214. }
  215. }
  216. buildRepresentationGroups(representations)
  217. switch len(repsMap) {
  218. case 0:
  219. return list
  220. case 1:
  221. for typeName, reps := range repsMap {
  222. resolveEntityGroup(typeName, reps.r, reps.i)
  223. }
  224. return list
  225. default:
  226. var g sync.WaitGroup
  227. g.Add(len(repsMap))
  228. for typeName, reps := range repsMap {
  229. go func(typeName string, reps []map[string]interface{}, idx []int) {
  230. resolveEntityGroup(typeName, reps, idx)
  231. g.Done()
  232. }(typeName, reps.r, reps.i)
  233. }
  234. g.Wait()
  235. return list
  236. }
  237. }
  238. func entityResolverNameForCollection(ctx context.Context, rep map[string]interface{}) (string, error) {
  239. for {
  240. var (
  241. m map[string]interface{}
  242. val interface{}
  243. ok bool
  244. )
  245. _ = val
  246. m = rep
  247. if _, ok = m["id"]; !ok {
  248. break
  249. }
  250. return "findCollectionByID", nil
  251. }
  252. return "", fmt.Errorf("%w for Collection", ErrTypeNotFound)
  253. }
  254. func entityResolverNameForFulfillmentService(ctx context.Context, rep map[string]interface{}) (string, error) {
  255. for {
  256. var (
  257. m map[string]interface{}
  258. val interface{}
  259. ok bool
  260. )
  261. _ = val
  262. m = rep
  263. if _, ok = m["id"]; !ok {
  264. break
  265. }
  266. return "findFulfillmentServiceByID", nil
  267. }
  268. return "", fmt.Errorf("%w for FulfillmentService", ErrTypeNotFound)
  269. }
  270. func entityResolverNameForInventoryLevel(ctx context.Context, rep map[string]interface{}) (string, error) {
  271. for {
  272. var (
  273. m map[string]interface{}
  274. val interface{}
  275. ok bool
  276. )
  277. _ = val
  278. m = rep
  279. if _, ok = m["id"]; !ok {
  280. break
  281. }
  282. return "findInventoryLevelByID", nil
  283. }
  284. return "", fmt.Errorf("%w for InventoryLevel", ErrTypeNotFound)
  285. }
  286. func entityResolverNameForProduct(ctx context.Context, rep map[string]interface{}) (string, error) {
  287. for {
  288. var (
  289. m map[string]interface{}
  290. val interface{}
  291. ok bool
  292. )
  293. _ = val
  294. m = rep
  295. if _, ok = m["id"]; !ok {
  296. break
  297. }
  298. return "findProductByID", nil
  299. }
  300. return "", fmt.Errorf("%w for Product", ErrTypeNotFound)
  301. }
  302. func entityResolverNameForProductOption(ctx context.Context, rep map[string]interface{}) (string, error) {
  303. for {
  304. var (
  305. m map[string]interface{}
  306. val interface{}
  307. ok bool
  308. )
  309. _ = val
  310. m = rep
  311. if _, ok = m["id"]; !ok {
  312. break
  313. }
  314. return "findProductOptionByID", nil
  315. }
  316. return "", fmt.Errorf("%w for ProductOption", ErrTypeNotFound)
  317. }
  318. func entityResolverNameForProductVariant(ctx context.Context, rep map[string]interface{}) (string, error) {
  319. for {
  320. var (
  321. m map[string]interface{}
  322. val interface{}
  323. ok bool
  324. )
  325. _ = val
  326. m = rep
  327. if _, ok = m["id"]; !ok {
  328. break
  329. }
  330. return "findProductVariantByID", nil
  331. }
  332. return "", fmt.Errorf("%w for ProductVariant", ErrTypeNotFound)
  333. }