federation.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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 "DraftOrder":
  69. resolverName, err := entityResolverNameForDraftOrder(ctx, rep)
  70. if err != nil {
  71. return fmt.Errorf(`finding resolver for Entity "DraftOrder": %w`, err)
  72. }
  73. switch resolverName {
  74. case "findDraftOrderByID":
  75. id0, err := ec.unmarshalNID2string(ctx, rep["id"])
  76. if err != nil {
  77. return fmt.Errorf(`unmarshalling param 0 for findDraftOrderByID(): %w`, err)
  78. }
  79. entity, err := ec.resolvers.Entity().FindDraftOrderByID(ctx, id0)
  80. if err != nil {
  81. return fmt.Errorf(`resolving Entity "DraftOrder": %w`, err)
  82. }
  83. list[idx[i]] = entity
  84. return nil
  85. }
  86. case "Order":
  87. resolverName, err := entityResolverNameForOrder(ctx, rep)
  88. if err != nil {
  89. return fmt.Errorf(`finding resolver for Entity "Order": %w`, err)
  90. }
  91. switch resolverName {
  92. case "findOrderByID":
  93. id0, err := ec.unmarshalNID2string(ctx, rep["id"])
  94. if err != nil {
  95. return fmt.Errorf(`unmarshalling param 0 for findOrderByID(): %w`, err)
  96. }
  97. entity, err := ec.resolvers.Entity().FindOrderByID(ctx, id0)
  98. if err != nil {
  99. return fmt.Errorf(`resolving Entity "Order": %w`, err)
  100. }
  101. list[idx[i]] = entity
  102. return nil
  103. }
  104. }
  105. return fmt.Errorf("%w: %s", ErrUnknownType, typeName)
  106. }
  107. resolveManyEntities := func(ctx context.Context, typeName string, reps []map[string]interface{}, idx []int) (err error) {
  108. // we need to do our own panic handling, because we may be called in a
  109. // goroutine, where the usual panic handling can't catch us
  110. defer func() {
  111. if r := recover(); r != nil {
  112. err = ec.Recover(ctx, r)
  113. }
  114. }()
  115. switch typeName {
  116. default:
  117. return errors.New("unknown type: " + typeName)
  118. }
  119. }
  120. resolveEntityGroup := func(typeName string, reps []map[string]interface{}, idx []int) {
  121. if isMulti(typeName) {
  122. err := resolveManyEntities(ctx, typeName, reps, idx)
  123. if err != nil {
  124. ec.Error(ctx, err)
  125. }
  126. } else {
  127. // if there are multiple entities to resolve, parallelize (similar to
  128. // graphql.FieldSet.Dispatch)
  129. var e sync.WaitGroup
  130. e.Add(len(reps))
  131. for i, rep := range reps {
  132. i, rep := i, rep
  133. go func(i int, rep map[string]interface{}) {
  134. err := resolveEntity(ctx, typeName, rep, idx, i)
  135. if err != nil {
  136. ec.Error(ctx, err)
  137. }
  138. e.Done()
  139. }(i, rep)
  140. }
  141. e.Wait()
  142. }
  143. }
  144. buildRepresentationGroups(representations)
  145. switch len(repsMap) {
  146. case 0:
  147. return list
  148. case 1:
  149. for typeName, reps := range repsMap {
  150. resolveEntityGroup(typeName, reps.r, reps.i)
  151. }
  152. return list
  153. default:
  154. var g sync.WaitGroup
  155. g.Add(len(repsMap))
  156. for typeName, reps := range repsMap {
  157. go func(typeName string, reps []map[string]interface{}, idx []int) {
  158. resolveEntityGroup(typeName, reps, idx)
  159. g.Done()
  160. }(typeName, reps.r, reps.i)
  161. }
  162. g.Wait()
  163. return list
  164. }
  165. }
  166. func entityResolverNameForDraftOrder(ctx context.Context, rep map[string]interface{}) (string, error) {
  167. for {
  168. var (
  169. m map[string]interface{}
  170. val interface{}
  171. ok bool
  172. )
  173. _ = val
  174. m = rep
  175. if _, ok = m["id"]; !ok {
  176. break
  177. }
  178. return "findDraftOrderByID", nil
  179. }
  180. return "", fmt.Errorf("%w for DraftOrder", ErrTypeNotFound)
  181. }
  182. func entityResolverNameForOrder(ctx context.Context, rep map[string]interface{}) (string, error) {
  183. for {
  184. var (
  185. m map[string]interface{}
  186. val interface{}
  187. ok bool
  188. )
  189. _ = val
  190. m = rep
  191. if _, ok = m["id"]; !ok {
  192. break
  193. }
  194. return "findOrderByID", nil
  195. }
  196. return "", fmt.Errorf("%w for Order", ErrTypeNotFound)
  197. }