mirror of
https://github.com/vercel/commerce.git
synced 2025-07-26 19:51:23 +00:00
feat: get-blog-list
This commit is contained in:
@@ -9,6 +9,9 @@ import type {
|
||||
GetAllProductsOperation,
|
||||
GetProductOperation,
|
||||
} from '../types/product'
|
||||
import type {
|
||||
GetAllBlogsOperation
|
||||
} from '../types/blogs'
|
||||
import type { APIProvider, CommerceAPI } from '.'
|
||||
import { GetAllCollectionsOperation } from '@commerce/types/collection';
|
||||
|
||||
@@ -27,7 +30,7 @@ export const OPERATIONS = [
|
||||
'getProduct',
|
||||
'getAllFacets',
|
||||
'getAllCollections',
|
||||
|
||||
'getAllBlogs'
|
||||
] as const
|
||||
|
||||
export const defaultOperations = OPERATIONS.reduce((ops, k) => {
|
||||
@@ -144,6 +147,24 @@ export type Operations<P extends APIProvider> = {
|
||||
): Promise<T['data']>
|
||||
}
|
||||
|
||||
|
||||
getAllBlogs: {
|
||||
<T extends GetAllBlogsOperation>(opts: {
|
||||
variables?: T['variables']
|
||||
config?: P['config']
|
||||
preview?: boolean
|
||||
}): Promise<T['data']>
|
||||
|
||||
<T extends GetAllBlogsOperation>(
|
||||
opts: {
|
||||
variables?: T['variables']
|
||||
config?: P['config']
|
||||
preview?: boolean
|
||||
} & OperationOptions
|
||||
): Promise<T['data']>
|
||||
}
|
||||
|
||||
|
||||
getProduct: {
|
||||
<T extends GetProductOperation>(opts: {
|
||||
variables: T['variables']
|
||||
|
26
framework/commerce/types/blogs.ts
Normal file
26
framework/commerce/types/blogs.ts
Normal file
@@ -0,0 +1,26 @@
|
||||
import { SearchResultSortParameter } from "@framework/schema";
|
||||
import { Asset, BlogTranslation, Maybe, Product } from './../../vendure/schema.d';
|
||||
|
||||
export type BlogList = Node &{
|
||||
id: string
|
||||
featuredAsset?: Maybe<Asset>
|
||||
isHidden:Boolean
|
||||
translations: Array<BlogTranslation>
|
||||
authorName: string
|
||||
authorAvatarAsset:Array<Asset>
|
||||
relevantProducts: Product
|
||||
}
|
||||
export type BlogsType = {
|
||||
items: BlogList
|
||||
totalItems: number
|
||||
}
|
||||
export type GetAllBlogsOperation<T extends BlogsType = BlogsType> = {
|
||||
data: { items: T['items'][] }
|
||||
variables: {
|
||||
take?: number
|
||||
skip?: number
|
||||
sort?: SearchResultSortParameter
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -11,7 +11,7 @@ import getProduct from './operations/get-product'
|
||||
import getSiteInfo from './operations/get-site-info'
|
||||
import login from './operations/login'
|
||||
import fetchGraphqlApi from './utils/fetch-graphql-api'
|
||||
|
||||
import getAllBlogs from './operations/get-all-blogs'
|
||||
|
||||
export interface VendureConfig extends CommerceAPIConfig {}
|
||||
|
||||
@@ -44,6 +44,7 @@ const operations = {
|
||||
getProduct,
|
||||
getAllFacets,
|
||||
getAllCollections,
|
||||
getAllBlogs
|
||||
}
|
||||
|
||||
export const provider = { config, operations }
|
||||
|
52
framework/vendure/api/operations/get-all-blogs.ts
Normal file
52
framework/vendure/api/operations/get-all-blogs.ts
Normal file
@@ -0,0 +1,52 @@
|
||||
import { OperationContext } from '@commerce/api/operations'
|
||||
import { Provider, VendureConfig } from '..'
|
||||
import { GetAllBlogsQuery,BlogList } from '../../schema'
|
||||
import { getAllBlogsQuery } from '../../utils/queries/get-all-blog-query'
|
||||
|
||||
export type BlogVariables = { take?: number,skip?:number }
|
||||
|
||||
export default function getAllBlogsOperation({
|
||||
commerce,
|
||||
}: OperationContext<Provider>) {
|
||||
async function getAllBlogs(opts?: {
|
||||
variables?: BlogVariables
|
||||
config?: Partial<VendureConfig>
|
||||
preview?: boolean
|
||||
}): Promise<{ blogs: GetAllBlogsQuery,totalItems:number }>
|
||||
|
||||
async function getAllBlogs({
|
||||
query = getAllBlogsQuery,
|
||||
variables: { ...vars } = {},
|
||||
config: cfg,
|
||||
}: {
|
||||
query?: string
|
||||
variables?: BlogVariables
|
||||
config?: Partial<VendureConfig>
|
||||
preview?: boolean
|
||||
} = {}): Promise<{ blogs: GetAllBlogsQuery | any[] ,totalItems?:number }> {
|
||||
const config = commerce.getConfig(cfg)
|
||||
const variables = {
|
||||
options: {
|
||||
take: vars.take,
|
||||
skip: vars.skip,
|
||||
},
|
||||
}
|
||||
const { data } = await config.fetch<GetAllBlogsQuery>(query, {
|
||||
variables,
|
||||
})
|
||||
|
||||
return {
|
||||
blogs: data?.blogs?.items?.map((val:BlogList)=>({
|
||||
id: val.id,
|
||||
title: val.translations[0]?.title,
|
||||
imageSrc: val.featuredAsset?.preview ?? null,
|
||||
slug: val.translations[0]?.slug,
|
||||
description: val.translations[0]?.description,
|
||||
isHidden: val.isHidden
|
||||
})),
|
||||
totalItems: data?.blogs?.totalItems || null
|
||||
}
|
||||
}
|
||||
|
||||
return getAllBlogs
|
||||
}
|
41
framework/vendure/schema.d.ts
vendored
41
framework/vendure/schema.d.ts
vendored
@@ -2312,6 +2312,46 @@ export type Product = Node & {
|
||||
customFields?: Maybe<Scalars['JSON']>
|
||||
}
|
||||
|
||||
export type BlogList = Node &{
|
||||
id: ID!
|
||||
createdAt: DateTime!
|
||||
updatedAt: DateTime!
|
||||
featuredAsset?: Maybe<Asset>
|
||||
isHidden:Boolean
|
||||
translations: Array<BlogTranslation>
|
||||
authorName: Scalars['String']
|
||||
authorAvatarAsset:Array<Asset>
|
||||
relevantProducts: Product
|
||||
}
|
||||
|
||||
export type BlogTranslation = {
|
||||
__typename?: 'BlogTranslation'
|
||||
id: Scalars['ID']
|
||||
createdAt: Scalars['DateTime']
|
||||
updatedAt: Scalars['DateTime']
|
||||
languageCode: LanguageCode
|
||||
title: Scalars['String']
|
||||
slug: Scalars['String']
|
||||
description: Scalars['String']
|
||||
content: Scalars['String']
|
||||
}
|
||||
|
||||
export type GetAllBlogsQuery = PaginatedList & {
|
||||
blogs: { __typename?: 'BlogList' } & {
|
||||
items: Array<{ __typename?: 'Blog' } & BlogList!>,
|
||||
'totalItems'
|
||||
}
|
||||
}
|
||||
|
||||
export type QueryBlogs = {
|
||||
options: BlogListOptions
|
||||
}
|
||||
|
||||
export type BlogListOptions = {
|
||||
skip?: Maybe<Scalars['Int']>
|
||||
take?: Maybe<Scalars['Int']>
|
||||
}
|
||||
|
||||
export type ProductTranslation = {
|
||||
__typename?: 'ProductTranslation'
|
||||
id: Scalars['ID']
|
||||
@@ -3364,3 +3404,4 @@ export type SearchQuery = { __typename?: 'Query' } & {
|
||||
'totalItems'
|
||||
> & { items: Array<{ __typename?: 'SearchResult' } & SearchResultFragment> }
|
||||
}
|
||||
|
||||
|
20
framework/vendure/utils/queries/get-all-blog-query.ts
Normal file
20
framework/vendure/utils/queries/get-all-blog-query.ts
Normal file
@@ -0,0 +1,20 @@
|
||||
export const getAllBlogsQuery = /* GraphQL */ `
|
||||
query GetBlogs ($options: BlogListOptions){
|
||||
blogs(options: $options){
|
||||
totalItems
|
||||
items {
|
||||
id
|
||||
isHidden
|
||||
featuredAsset {
|
||||
preview
|
||||
}
|
||||
translations {
|
||||
title
|
||||
slug
|
||||
description
|
||||
content
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
`
|
Reference in New Issue
Block a user