2
0

query_collections.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. )
  12. func (r *queryResolver) Collection(ctx context.Context, handle *string, id *string) (*generated.Collection, error) {
  13. var (
  14. inContext *middleware.GShopifyContext
  15. err error
  16. )
  17. if inContext, err = middleware.InContext(ctx); err != nil {
  18. return nil, err
  19. }
  20. if id == nil && handle == nil {
  21. return nil, fmt.Errorf("at least one argument must be specified")
  22. }
  23. if id != nil {
  24. if s, err := model.ParseId(model.GidCollection, *id); err != nil {
  25. return nil, err
  26. } else {
  27. *id = s
  28. }
  29. }
  30. return r.db.Collection(inContext.Language, handle, id)
  31. }
  32. func (r *queryResolver) Collections(ctx context.Context,
  33. after *string, before *string, first *int, last *int, query *string, reverse *bool, sort *generated.CollectionSortKeys) (*generated.CollectionConnection, error) {
  34. var (
  35. inContext *middleware.GShopifyContext
  36. collections []*generated.Collection
  37. err error
  38. )
  39. if inContext, err = middleware.InContext(ctx); err != nil {
  40. return nil, err
  41. }
  42. if collections, err = r.db.Collections(inContext.Language); err != nil {
  43. return nil, err
  44. }
  45. collections = helper.SortCollections(collections, sort)
  46. return helper.NewCollectionConnection(collections, after, before, first, last, reverse)
  47. }
  48. func (r *productResolver) Collections(ctx context.Context, product *generated.Product,
  49. after *string, before *string, first *int, last *int, reverse *bool) (*generated.CollectionConnection, error) {
  50. var (
  51. inContext *middleware.GShopifyContext
  52. collections []*generated.Collection
  53. err error
  54. )
  55. if inContext, err = middleware.InContext(ctx); err != nil {
  56. return nil, err
  57. }
  58. rawId, err := model.ParseId(model.GidProduct, product.ID)
  59. if err != nil {
  60. return nil, err
  61. }
  62. if collections, err = r.db.ProductCollections(inContext.Language, rawId); err != nil {
  63. return nil, err
  64. }
  65. return helper.NewCollectionConnection(collections, after, before, first, last, reverse)
  66. }
  67. func (r *collectionResolver) Description(_ context.Context, collection *generated.Collection, truncateAt *int) (string, error) {
  68. return fun.TruncateAt(
  69. bluemonday.StrictPolicy().Sanitize(string(collection.DescriptionHTML)),
  70. truncateAt)
  71. }