package graphql import ( "context" "fmt" "github.com/gshopify/service-wrapper/fun" "github.com/gshopify/service-wrapper/model" "github.com/gshopify/service-wrapper/server/middleware" "github.com/microcosm-cc/bluemonday" "gshopper.com/gshopify/products/graphql/generated" "gshopper.com/gshopify/products/graphql/helper" ) func (r *queryResolver) Collection(ctx context.Context, handle *string, id *string) (*generated.Collection, error) { var ( inContext *middleware.GShopifyContext err error ) if inContext, err = middleware.InContext(ctx); err != nil { return nil, err } if id == nil && handle == nil { return nil, fmt.Errorf("at least one argument must be specified") } if id != nil { if s, err := model.ParseId(model.GidCollection, *id); err != nil { return nil, err } else { *id = s } } return r.db.Collection(inContext.Language, handle, id) } func (r *queryResolver) Collections(ctx context.Context, after *string, before *string, first *int, last *int, query *string, reverse *bool, sort *generated.CollectionSortKeys) (*generated.CollectionConnection, error) { var ( inContext *middleware.GShopifyContext collections []*generated.Collection err error ) if inContext, err = middleware.InContext(ctx); err != nil { return nil, err } if collections, err = r.db.Collections(inContext.Language); err != nil { return nil, err } collections = helper.SortCollections(collections, sort) return helper.NewCollectionConnection(collections, after, before, first, last, reverse) } func (r *productResolver) Collections(ctx context.Context, product *generated.Product, after *string, before *string, first *int, last *int, reverse *bool) (*generated.CollectionConnection, error) { var ( inContext *middleware.GShopifyContext collections []*generated.Collection err error ) if inContext, err = middleware.InContext(ctx); err != nil { return nil, err } rawId, err := model.ParseId(model.GidProduct, product.ID) if err != nil { return nil, err } if collections, err = r.db.ProductCollections(inContext.Language, rawId); err != nil { return nil, err } return helper.NewCollectionConnection(collections, after, before, first, last, reverse) } func (r *collectionResolver) Description(_ context.Context, collection *generated.Collection, truncateAt *int) (string, error) { return fun.TruncateAt( bluemonday.StrictPolicy().Sanitize(string(collection.DescriptionHTML)), truncateAt) }