Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/img filter #32

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).

## [Unreleased]
### Added
- Arg for images resolver to be able to filter images. Create arg `WITH_LABEL` filter.

## [0.11.0] - 2019-11-12
### Added
Expand Down
27 changes: 26 additions & 1 deletion graphql/types/Product.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -173,13 +173,29 @@ type SKU {
measurementUnit: String
unitMultiplier: Float
kitItems: [KitItem]
images(quantity: Int = 10): [Image]
images(
"""
Returns at most this quantity of images. Applies after the filter arg.
"""
quantity: Int = 10
"""
Type of filter to be applied to images.
"""
filter: SkuImagesFilter
): [Image]
videos: [Video]
sellers: [Seller]
variations: [Property]
attachments: [Attachment] @deprecated(reason: "Use itemMetaData instead")
}

enum SkuImagesFilter {
"""
Will return only the first image along with others that have imagelabel with a value
"""
WITH_LABEL
}

type skuSpecification {
fieldName: String @translatableV2
fieldValues: [String] @translatableV2
Expand Down Expand Up @@ -224,8 +240,17 @@ type DomainValues {
}

enum InstallmentsCriteria {
"""
Returns installment with largest amount of installments.
"""
MAX
"""
Returns installment with smallest amount of installments.
"""
MIN
"""
Returns all installments.
"""
ALL
}

Expand Down
37 changes: 26 additions & 11 deletions node/resolvers/search/sku.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import { find, head, map, replace, slice } from 'ramda'
import { find, head, map, tail } from 'ramda'

enum ImagesFilter {
WITH_LABEL = 'WITH_LABEL',
}

export const resolvers = {
SKU: {
Expand All @@ -13,16 +17,27 @@ export const resolvers = {

images: (
{ images = [] }: SearchItem,
{ quantity }: { quantity: number }
) =>
map(
image => ({
cacheId: image.imageId,
...image,
imageUrl: replace('http://', 'https://', image.imageUrl),
}),
quantity > 0 ? slice(0, quantity, images) : images
),
{ quantity, filter }: { quantity: number; filter: ImagesFilter }
) => {
let filtered = images
if (filter === ImagesFilter.WITH_LABEL && images.length > 1) {
// We still want to return the first image because it usually is the main image
filtered = [
head(images),
...tail(images).filter(({ imageLabel }) => !!imageLabel),
]
}

const sliced =
quantity > 0 && quantity > filtered.length
? filtered.slice(0, quantity)
: filtered
return sliced.map(image => ({
cacheId: image.imageId,
...image,
imageUrl: image.imageUrl.replace('http://', 'https://'),
}))
},

kitItems: (
{ kitItems }: SearchItem,
Expand Down