2
0

query_product.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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. product *generated.Product
  17. err error
  18. )
  19. if inContext, err = middleware.InContext(ctx); err != nil {
  20. return nil, err
  21. }
  22. if id == nil && handle == nil {
  23. return nil, fmt.Errorf("at least one argument must be specified")
  24. }
  25. if id != nil {
  26. if s, err := model.ParseId(model.GidProduct, *id); err != nil {
  27. return nil, err
  28. } else {
  29. *id = s
  30. }
  31. }
  32. if product, err = r.db.Product(inContext.Language, handle, id); err != nil {
  33. return nil, err
  34. }
  35. return product, nil
  36. }
  37. func (r *productResolver) Description(_ context.Context, product *generated.Product, truncateAt *int) (string, error) {
  38. return fun.TruncateAt(
  39. bluemonday.StrictPolicy().Sanitize(string(product.DescriptionHTML)),
  40. truncateAt)
  41. }
  42. // Products retrieves Product list by collection
  43. // TODO: implement filters
  44. // TODO: implement sort
  45. func (r *collectionResolver) Products(ctx context.Context, collection *generated.Collection,
  46. after *string, before *string, filters []*generated.ProductFilter, first *int, last *int, reverse *bool, sort *generated.ProductCollectionSortKeys) (*generated.ProductConnection, error) {
  47. var (
  48. inContext *middleware.GShopifyContext
  49. products []*generated.Product
  50. err error
  51. )
  52. if inContext, err = middleware.InContext(ctx); err != nil {
  53. return nil, err
  54. }
  55. rawId, err := model.ParseId(model.GidCollection, collection.ID)
  56. if err != nil {
  57. return nil, err
  58. }
  59. if products, err = r.db.CollectionProducts(inContext.Language, rawId); err != nil {
  60. return nil, err
  61. }
  62. return helper.NewProductConnection(products, after, before, first, last, reverse)
  63. }
  64. func (r *productResolver) Options(ctx context.Context, product *generated.Product, first *int) ([]*generated.ProductOption, error) {
  65. var (
  66. inContext *middleware.GShopifyContext
  67. options []*generated.ProductOption
  68. err error
  69. )
  70. if inContext, err = middleware.InContext(ctx); err != nil {
  71. return nil, err
  72. }
  73. rawId, err := model.ParseId(model.GidProduct, product.ID)
  74. if err != nil {
  75. return nil, err
  76. }
  77. if options, err = r.db.ProductOptions(inContext.Language, rawId); err != nil {
  78. return nil, err
  79. }
  80. return fun.First(options, first), nil
  81. }
  82. func (r *productResolver) Variants(ctx context.Context, product *generated.Product,
  83. first *int, after *string, last *int, before *string, reverse *bool, sort *generated.ProductVariantSortKeys) (*generated.ProductVariantConnection, error) {
  84. var (
  85. inContext *middleware.GShopifyContext
  86. variants []*generated.ProductVariant
  87. err error
  88. )
  89. inContext, err = middleware.InContext(ctx)
  90. if err != nil {
  91. return nil, err
  92. }
  93. rawId, err := model.ParseId(model.GidProduct, product.ID)
  94. if err != nil {
  95. return nil, err
  96. }
  97. variants, err = r.db.ProductVariants(inContext, rawId)
  98. if err != nil {
  99. return nil, err
  100. }
  101. return helper.NewProductVariantConnection(variants, after, before, first, last, reverse, sort)
  102. }
  103. func (r *productVariantResolver) Product(ctx context.Context, variant *generated.ProductVariant) (*generated.Product, error) {
  104. var (
  105. inContext *middleware.GShopifyContext
  106. err error
  107. )
  108. if inContext, err = middleware.InContext(ctx); err != nil {
  109. return nil, err
  110. }
  111. if variant.Product == nil {
  112. return nil, fmt.Errorf("unknown product.ID for variant: `%s`", variant.ID)
  113. }
  114. rawId, err := model.ParseId(model.GidProduct, variant.Product.ID)
  115. if err != nil {
  116. return nil, err
  117. }
  118. return r.db.Product(inContext.Language, nil, &rawId)
  119. }
  120. // StoreAvailability TODO: implement
  121. func (r *productVariantResolver) StoreAvailability(ctx context.Context, variant *generated.ProductVariant,
  122. first *int, after *string, last *int, before *string, reverse *bool) (*generated.StoreAvailabilityConnection, error) {
  123. panic("not implemented")
  124. }
  125. func (r *productVariantResolver) SelectedOptions(ctx context.Context, variant *generated.ProductVariant) ([]*generated.SelectedOption, error) {
  126. var (
  127. inContext *middleware.GShopifyContext
  128. err error
  129. )
  130. if inContext, err = middleware.InContext(ctx); err != nil {
  131. return nil, err
  132. }
  133. rawId, err := model.ParseId(model.GidVariant, variant.ID)
  134. if err != nil {
  135. return nil, fmt.Errorf("unknown ID for variant: `%s`", variant.ID)
  136. }
  137. return r.db.ProductVariantOptions(inContext.Language, rawId)
  138. }
  139. func (r *productVariantResolver) Title(ctx context.Context, variant *generated.ProductVariant) (string, error) {
  140. opts, err := r.SelectedOptions(ctx, variant)
  141. if err != nil {
  142. return "", err
  143. }
  144. s := make([]string, len(opts))
  145. for i := range opts {
  146. s[i] = opts[i].Value
  147. }
  148. return strings.Join(s, " / "), nil
  149. }