Skip to content

Commit

Permalink
feat: fallback image for marketing collection (#4942)
Browse files Browse the repository at this point in the history
* wip

* feat: representative artwork id for marketing collection

* Sync gravity schema, fix incorrect naming

* test fix

* renaming

* test fix

---------

Co-authored-by: Nikita Skalkin <[email protected]>
  • Loading branch information
rajsam003 and nickskalkin committed Apr 14, 2023
1 parent d0081a7 commit a591ca9
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 3 deletions.
3 changes: 3 additions & 0 deletions _schemaV2.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -10840,6 +10840,9 @@ type MarketingCollection {
# Returns collections by the same artist if more than 4 exist, otherwise returns collections from the same category
relatedCollections(size: Int = 10): [MarketingCollection!]!

# ID of an artwork with highest merchandis ability
representativeArtworkID: String

# Show featured artists section in header. Defaults to true
showFeaturedArtists: Boolean!

Expand Down
5 changes: 5 additions & 0 deletions src/data/gravity.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -1152,6 +1152,11 @@ type MarketingCollection {
"""
relatedCollections(size: Int = 10): [MarketingCollection!]!

"""
ID of an artwork with highest merchandis ability
"""
representativeArtworkID: String

"""
Show featured artists section in header. Defaults to true
"""
Expand Down
42 changes: 41 additions & 1 deletion src/lib/stitching/gravity/__tests__/stitching.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1080,12 +1080,17 @@ describe("gravity/stitching", () => {
info: expect.anything(),
})
})
})

describe("#thumbnailImage", () => {
it("resolves the thumbnailImage field with a copy of the request context", async () => {
const { resolvers } = await getGravityStitchedSchema()
const { thumbnailImage } = resolvers.MarketingCollection

const parent = { image_url: "https://www.example.com/image.jpg" }
const parent = {
image_url: "https://www.example.com/image.jpg",
representativeArtworkID: "representativeArtworkID123",
}
const args = {}
const sharedContext = {}
const info = { mergeInfo: { delegateToSchema: jest.fn() } }
Expand All @@ -1102,6 +1107,41 @@ describe("gravity/stitching", () => {
})
)
})

it("resolves the thumbnailImage field with a fallback image from representative artwork ID if imageURL is empty", async () => {
const { resolvers } = await getGravityStitchedSchema()
const { thumbnailImage } = resolvers.MarketingCollection

const parent = {
image_url: null,
representativeArtworkID: "representative-artwork-id",
}
const args = {}
const info = { mergeInfo: { delegateToSchema: jest.fn() } }

const imageData = {
image_url: "cat.jpg",
}

const artworkLoader = () =>
Promise.resolve({
images: [imageData],
})
const sharedContext = {
artworkLoader,
}

await thumbnailImage.resolve(parent, args, sharedContext, info)

expect(info.mergeInfo.delegateToSchema).toHaveBeenCalledWith({
args: expect.anything(),
operation: "query",
fieldName: "_do_not_use_image",
schema: expect.anything(),
context: expect.objectContaining({ imageData }),
info: expect.anything(),
})
})
})
})
})
19 changes: 17 additions & 2 deletions src/lib/stitching/gravity/v2/stitching.ts
Original file line number Diff line number Diff line change
Expand Up @@ -302,16 +302,31 @@ export const gravityStitchingEnvironment = (
fragment: gql`
... on MarketingCollection {
image_url: thumbnail
representativeArtworkID
}
`,
resolve: async ({ image_url }, _args, context, info) => {
resolve: async (
{ representativeArtworkID, image_url },
args,
context,
info
) => {
let imageData: unknown
if (image_url) {
imageData = normalizeImageData(image_url)
} else if (representativeArtworkID) {
const { artworkLoader } = context
const { images } = await artworkLoader(representativeArtworkID)
imageData = normalizeImageData(getDefault(images))
}
return info.mergeInfo.delegateToSchema({
args,
schema: localSchema,
operation: "query",
fieldName: "_do_not_use_image",
context: {
...context,
imageData: normalizeImageData(image_url),
imageData,
},
info,
})
Expand Down

0 comments on commit a591ca9

Please sign in to comment.