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: reviews query resolver #2649

Merged
merged 2 commits into from
Feb 7, 2025
Merged
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
63 changes: 63 additions & 0 deletions packages/api/src/__generated__/schema.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,14 @@ export enum ProductReviewsInputOrderBy {
locale = 'Locale',
}

export type ProductReviewsInputOrderWay = 'asc' | 'desc'

export interface ProductReviewsInput {
searchTerm?: string
from?: number
to?: number
orderBy?: ProductReviewsInputOrderBy
orderWay?: 'asc' | 'desc'
orderWay?: ProductReviewsInputOrderWay
status?: boolean
productId?: string
rating?: number
Expand Down
32 changes: 32 additions & 0 deletions packages/api/src/platforms/vtex/resolvers/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,16 @@ import type {
QuerySellersArgs,
QueryShippingArgs,
QueryRedirectArgs,
QueryReviewsArgs,
} from '../../../__generated__/schema'
import type { CategoryTree } from '../clients/commerce/types/CategoryTree'
import type { Context } from '../index'
import { isValidSkuId, pickBestSku } from '../utils/sku'
import type { SearchArgs } from '../clients/search'
import {
ProductReviewsInputOrderBy,
type ProductReviewsInputOrderWay,
} from '../clients/commerce/types/ProductReview'

export const Query = {
product: async (_: unknown, { locator }: QueryProductArgs, ctx: Context) => {
Expand Down Expand Up @@ -335,4 +340,31 @@ export const Query = {
sellers,
}
},
reviews: async (
_: unknown,
{ productId, after, first, rating, sort }: QueryReviewsArgs,
ctx: Context
) => {
const {
clients: { commerce },
} = ctx

const from = after ?? 0
const to = from + (first ?? 6)

const [orderByKey, orderWay] = sort?.split('_') as [
keyof typeof ProductReviewsInputOrderBy,
ProductReviewsInputOrderWay,
]

return await commerce.reviews.list({
productId,
from,
to,
orderBy: ProductReviewsInputOrderBy[orderByKey],
orderWay,
status: true,
rating: rating ?? undefined,
})
},
}
84 changes: 84 additions & 0 deletions packages/api/src/typeDefs/productReview.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
type StoreProductReview {
"""
Review ID.
"""
id: String!
"""
Product ID.
"""
productId: String!
"""
Review rating.
"""
rating: Int!
"""
Review title.
"""
title: String!
"""
Review content.
"""
text: String!
"""
Review author name.
"""
reviewerName: String
"""
Review author ID.
"""
shopperId: String!
"""
Review creation date.
"""
reviewDateTime: String!
"""
Indicates if the review was made by a verified purchaser.
"""
verifiedPurchaser: Boolean!
"""
Indicates if the review was approved by the store owner.
"""
approved: Boolean!
}

type StoreProductListReviewsRange {
"""
Total number of reviews.
"""
total: Int!
"""
Index of the first review
"""
from: Int!
"""
Index of the last review
"""
to: Int!
}

type StoreProductListReviewsResult {
"""
Array of product reviews.
"""
data: [StoreProductReview!]!
range: StoreProductListReviewsRange!
}

enum StoreProductListReviewsSort {
"""
Sort by review creation date, from newest to oldest.
"""
reviewDateTime_desc
"""
Sort by review creation date, from oldest to newest.
"""
reviewDateTime_asc
"""
Sort by review rating, from highest to lowest.
"""
rating_desc
"""
Sort by review rating, from lowest to highest.
"""
rating_asc
}
27 changes: 27 additions & 0 deletions packages/api/src/typeDefs/query.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,33 @@ type Query {
salesChannel: String
): SellersData
@cacheControl(scope: "public", sMaxAge: 120, staleWhileRevalidate: 3600)

"""
Returns a list of approved reviews for a specific product.
"""
reviews(
"""
Product Id
"""
productId: String!
"""
Reviews results sorting mode
"""
sort: StoreProductListReviewsSort = reviewDateTime_desc
"""
Reviews pagination argument, indicating how many items should be returned from the complete result list.
"""
first: Int = 6
"""
Reviews pagination argument, indicating the cursor corresponding with the item after which the items should be fetched.
"""
after: Int = 0
"""
Rating filter
"""
rating: Int
): StoreProductListReviewsResult
@cacheControl(scope: "public", sMaxAge: 120, staleWhileRevalidate: 3600)
}

"""
Expand Down
1 change: 1 addition & 0 deletions packages/api/test/schema.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ const QUERIES = [
'shipping',
'redirect',
'sellers',
'reviews',
]

const MUTATIONS = ['validateCart', 'validateSession', 'subscribeToNewsletter']
Expand Down
1 change: 1 addition & 0 deletions packages/core/test/server/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ const QUERIES = [
'shipping',
'redirect',
'sellers',
'reviews',
]

const MUTATIONS = ['validateCart', 'validateSession', 'subscribeToNewsletter']
Expand Down