2
0

query_product.go 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. package graphql
  2. import (
  3. "context"
  4. "fmt"
  5. "github.com/gshopify/service-wrapper/fun"
  6. "github.com/gshopify/service-wrapper/model"
  7. "github.com/gshopify/service-wrapper/server/middleware"
  8. "github.com/microcosm-cc/bluemonday"
  9. "gshopper.com/gshopify/products/graphql/generated"
  10. "gshopper.com/gshopify/products/graphql/helper"
  11. "strings"
  12. )
  13. func (r *queryResolver) Product(ctx context.Context, handle *string, id *string) (*generated.Product, error) {
  14. var (
  15. inContext *middleware.GShopifyContext
  16. err error
  17. )
  18. if inContext, err = middleware.InContext(ctx); err != nil {
  19. return nil, err
  20. }
  21. if id == nil && handle == nil {
  22. return nil, fmt.Errorf("at least one argument must be specified")
  23. }
  24. if id != nil {
  25. if s, err := model.ParseId(model.GidProduct, *id); err != nil {
  26. return nil, err
  27. } else {
  28. *id = s
  29. }
  30. }
  31. return r.db.Product(inContext.Language, handle, id)
  32. }
  33. func (r *queryResolver) Products(ctx context.Context,
  34. after *string, before *string, first *int, last *int, query *string, reverse *bool, sort *generated.ProductSortKeys) (*generated.ProductConnection, error) {
  35. var (
  36. inContext *middleware.GShopifyContext
  37. products []*generated.Product
  38. err error
  39. )
  40. if inContext, err = middleware.InContext(ctx); err != nil {
  41. return nil, err
  42. }
  43. if products, err = r.db.Products(inContext.Language, nil); err != nil {
  44. return nil, err
  45. }
  46. products = helper.SortProducts(products, sort)
  47. return helper.NewProductConnection(products, after, before, first, last, reverse)
  48. }
  49. func (r *productResolver) Description(_ context.Context, product *generated.Product, truncateAt *int) (string, error) {
  50. return fun.TruncateAt(
  51. bluemonday.StrictPolicy().Sanitize(string(product.DescriptionHTML)),
  52. truncateAt)
  53. }
  54. // Products retrieves Product list by collection
  55. func (r *collectionResolver) Products(ctx context.Context, collection *generated.Collection,
  56. after *string, before *string, filters []*generated.ProductFilter, first *int, last *int, reverse *bool, sort *generated.ProductCollectionSortKeys) (*generated.ProductConnection, error) {
  57. var (
  58. inContext *middleware.GShopifyContext
  59. products []*generated.Product
  60. err error
  61. )
  62. if inContext, err = middleware.InContext(ctx); err != nil {
  63. return nil, err
  64. }
  65. rawId, err := model.ParseId(model.GidCollection, collection.ID)
  66. if err != nil {
  67. return nil, err
  68. }
  69. if products, err = r.db.Products(inContext.Language, &rawId); err != nil {
  70. return nil, err
  71. }
  72. products = helper.FilterProducts(products, filters)
  73. products = helper.SortProducts(products, sort)
  74. return helper.NewProductConnection(products, after, before, first, last, reverse)
  75. }
  76. func (r *productResolver) Options(ctx context.Context, product *generated.Product, first *int) ([]*generated.ProductOption, error) {
  77. var (
  78. inContext *middleware.GShopifyContext
  79. options []*generated.ProductOption
  80. err error
  81. )
  82. if inContext, err = middleware.InContext(ctx); err != nil {
  83. return nil, err
  84. }
  85. rawId, err := model.ParseId(model.GidProduct, product.ID)
  86. if err != nil {
  87. return nil, err
  88. }
  89. if options, err = r.db.ProductOptions(inContext.Language, rawId); err != nil {
  90. return nil, err
  91. }
  92. return fun.First(options, first), nil
  93. }
  94. func (r *productResolver) Variants(ctx context.Context, product *generated.Product,
  95. first *int, after *string, last *int, before *string, reverse *bool, sort *generated.ProductVariantSortKeys) (*generated.ProductVariantConnection, error) {
  96. var (
  97. inContext *middleware.GShopifyContext
  98. variants []*generated.ProductVariant
  99. err error
  100. )
  101. inContext, err = middleware.InContext(ctx)
  102. if err != nil {
  103. return nil, err
  104. }
  105. rawId, err := model.ParseId(model.GidProduct, product.ID)
  106. if err != nil {
  107. return nil, err
  108. }
  109. variants, err = r.db.ProductVariants(inContext, rawId)
  110. if err != nil {
  111. return nil, err
  112. }
  113. return helper.NewProductVariantConnection(variants, after, before, first, last, reverse, sort)
  114. }
  115. func (r *productResolver) VariantBySelectedOptions(ctx context.Context, product *generated.Product, input []*generated.SelectedOptionInput) (*generated.ProductVariant, error) {
  116. options, err := r.Options(ctx, product, nil)
  117. if err != nil {
  118. return nil, err
  119. }
  120. size := len(options)
  121. if size != len(input) {
  122. return nil, fmt.Errorf("illegal SelectedOptionInput")
  123. }
  124. type ext struct {
  125. id string
  126. index int
  127. }
  128. var selected = make([]ext, 0)
  129. for i := range input {
  130. for k := range options {
  131. if options[k].Name != input[i].Name {
  132. continue
  133. }
  134. for j, value := range options[k].Values {
  135. if value != input[i].Value {
  136. continue
  137. }
  138. selected = append(selected, ext{
  139. id: options[k].ID,
  140. index: j + 1,
  141. })
  142. break
  143. }
  144. }
  145. }
  146. if size != len(selected) {
  147. return nil, fmt.Errorf("illegal SelectedOptionInput")
  148. }
  149. variants, err := r.Variants(ctx, product, nil, nil, nil, nil, nil, nil)
  150. if err != nil {
  151. return nil, err
  152. }
  153. for _, node := range variants.Nodes {
  154. var i = 0
  155. for _, e := range selected {
  156. for _, meta := range node.Metafields {
  157. if meta.Namespace != model.GidOption.String() {
  158. continue
  159. }
  160. if t := model.MetaFieldType(meta.Type); !t.IsValid() || model.MetaFieldTypeNumberInteger != t {
  161. continue
  162. }
  163. if e.id == meta.Key && fmt.Sprintf("%d", e.index) == meta.Value {
  164. i++
  165. break
  166. }
  167. }
  168. }
  169. if i == size {
  170. return node, nil
  171. }
  172. }
  173. return nil, nil
  174. }
  175. func (r *productVariantResolver) Product(ctx context.Context, variant *generated.ProductVariant) (*generated.Product, error) {
  176. var (
  177. inContext *middleware.GShopifyContext
  178. err error
  179. )
  180. if inContext, err = middleware.InContext(ctx); err != nil {
  181. return nil, err
  182. }
  183. if variant.Product == nil {
  184. return nil, fmt.Errorf("unknown product.ID for variant: `%s`", variant.ID)
  185. }
  186. rawId, err := model.ParseId(model.GidProduct, variant.Product.ID)
  187. if err != nil {
  188. return nil, err
  189. }
  190. return r.db.Product(inContext.Language, nil, &rawId)
  191. }
  192. // StoreAvailability TODO: implement
  193. func (r *productVariantResolver) StoreAvailability(ctx context.Context, variant *generated.ProductVariant,
  194. first *int, after *string, last *int, before *string, reverse *bool) (*generated.StoreAvailabilityConnection, error) {
  195. panic("not implemented")
  196. }
  197. func (r *productVariantResolver) SelectedOptions(ctx context.Context, variant *generated.ProductVariant) ([]*generated.SelectedOption, error) {
  198. var (
  199. inContext *middleware.GShopifyContext
  200. err error
  201. )
  202. if inContext, err = middleware.InContext(ctx); err != nil {
  203. return nil, err
  204. }
  205. rawId, err := model.ParseId(model.GidVariant, variant.ID)
  206. if err != nil {
  207. return nil, fmt.Errorf("unknown ID for variant: `%s`", variant.ID)
  208. }
  209. return r.db.ProductVariantOptions(inContext.Language, rawId)
  210. }
  211. func (r *productVariantResolver) Title(ctx context.Context, variant *generated.ProductVariant) (string, error) {
  212. opts, err := r.SelectedOptions(ctx, variant)
  213. if err != nil {
  214. return "", err
  215. }
  216. s := make([]string, len(opts))
  217. for i := range opts {
  218. s[i] = opts[i].Value
  219. }
  220. return strings.Join(s, " / "), nil
  221. }
  222. func (r *queryResolver) ProductTags(_ context.Context, first int) (*generated.StringConnection, error) {
  223. tags, err := r.db.ProductTags()
  224. if err != nil {
  225. return nil, err
  226. }
  227. return helper.NewStringConnection(tags, &first, nil, nil)
  228. }
  229. func (r *queryResolver) ProductTypes(_ context.Context, first int) (*generated.StringConnection, error) {
  230. types, err := r.db.ProductTypes()
  231. if err != nil {
  232. return nil, err
  233. }
  234. return helper.NewStringConnection(types, &first, nil, nil)
  235. }
  236. func (r *queryResolver) ProductRecommendations(ctx context.Context, productID string) ([]*generated.Product, error) {
  237. panic("not implemented")
  238. }