fix(gatsby-source-medusa): adds support for incremental sourcing of medusa data (#1217)

This commit is contained in:
Sebastian Rindom authored and GitHub committed 2022-03-18 15:00:36 +01:00
1 parent 3cd4108915
commit 23f8399c16
4 files changed
+60 -92

No files matched your search

+33 -45
View File
@@ -1,5 +1,4 @@
import axios, { AxiosPromise, AxiosRequestConfig } from "axios" import axios, { AxiosPromise, AxiosRequestConfig } from "axios"
import { Reporter } from "gatsby-cli/lib/reporter/reporter"
function medusaRequest( function medusaRequest(
storeURL: string, storeURL: string,
@@ -18,34 +17,28 @@ function medusaRequest(
return client(options) return client(options)
} }
export const createClient = ( export const createClient = (options: MedusaPluginOptions): any => {
options: MedusaPluginOptions,
reporter: Reporter
): any => {
const { storeUrl, authToken } = options as any const { storeUrl, authToken } = options as any
/** /**
*
* @param {string} _date used fetch products updated since the specified date * @param {string} _date used fetch products updated since the specified date
* @return {Promise<any[]>} * @return {Promise<any[]>} products to create nodes from
*/ */
async function products(_date?: string): Promise<any[]> { async function products(_date?: string): Promise<any[]> {
let products: any[] = [] let products: any[] = []
let offset = 0 let offset = 0
let count = 1 let count = 1
do { do {
await medusaRequest(storeUrl, `/store/products?offset=${offset}`) let path = `/store/products?offset=${offset}`
.then(({ data }) => { if (_date) {
products = [...products, ...data.products] path += `&updated_at[gt]=${_date}`
count = data.count }
offset = data.products.length
}) await medusaRequest(storeUrl, path).then(({ data }) => {
.catch((error) => { products = [...products, ...data.products]
reporter.error( count = data.count
`"The following error status was produced while attempting to fetch products: ${error}` offset = data.products.length
) })
return []
})
} while (products.length < count) } while (products.length < count)
return products return products
@@ -53,27 +46,25 @@ export const createClient = (
/** /**
* *
* @param {string} date used fetch regions updated since the specified date * @param {string} _date used fetch regions updated since the specified date
* @return {Promise<any[]>} * @return {Promise<any[]>} regions to create nodes from
*/ */
async function regions(_date?: string): Promise<any[]> { async function regions(_date?: string): Promise<any[]> {
const regions = await medusaRequest(storeUrl, `/store/regions`) let path = `/store/regions`
.then(({ data }) => { if (_date) {
return data.regions path += `?updated_at[gt]=${_date}`
}) }
.catch((error) => {
console.warn(` const regions = await medusaRequest(storeUrl, path).then(({ data }) => {
"The following error status was produced while attempting to fetch regions: ${error} return data.regions
`) })
return []
})
return regions return regions
} }
/** /**
* *
* @param {string} _date used fetch regions updated since the specified date * @param {string} _date used fetch regions updated since the specified date
* @return {Promise<any[]>} * @return {Promise<any[]>} orders to create nodes from
*/ */
async function orders(_date?: string): Promise<any[]> { async function orders(_date?: string): Promise<any[]> {
const orders = await medusaRequest(storeUrl, `/admin/orders`, { const orders = await medusaRequest(storeUrl, `/admin/orders`, {
@@ -95,25 +86,22 @@ export const createClient = (
/** /**
* *
* @param {string} _date used fetch regions updated since the specified date * @param {string} _date used fetch regions updated since the specified date
* @return {Promise<any[]>} * @return {Promise<any[]>} collections to create nodes from
*/ */
async function collections(_date?: string): Promise<any[]> { async function collections(_date?: string): Promise<any[]> {
let collections: any[] = [] let collections: any[] = []
let offset = 0 let offset = 0
let count = 1 let count = 1
do { do {
await medusaRequest(storeUrl, `/store/collections?offset=${offset}`) let path = `/store/collections?offset=${offset}`
.then(({ data }) => { if (_date) {
collections = [...collections, ...data.collections] path += `&updated_at[gt]=${_date}`
count = data.count }
offset = data.collections.length await medusaRequest(storeUrl, path).then(({ data }) => {
}) collections = [...collections, ...data.collections]
.catch((error) => { count = data.count
reporter.error( offset = data.collections.length
`"The following error status was produced while attempting to fetch products: ${error}` })
)
return []
})
} while (collections.length < count) } while (collections.length < count)
return collections return collections
@@ -27,7 +27,7 @@ async function sourceAllNodes(
createRegionsOperation, createRegionsOperation,
createOrdersOperation, createOrdersOperation,
createCollectionsOperation, createCollectionsOperation,
} = createOperations(pluginOptions, gatsbyApi) } = createOperations(pluginOptions)
const operations = [ const operations = [
createProductsOperation, createProductsOperation,
@@ -54,22 +54,17 @@ const medusaNodeTypes = [
"MedusaCollections", "MedusaCollections",
] ]
export async function sourceUpdatedNodes( async function sourceUpdatedNodes(
gatsbyApi: SourceNodesArgs, gatsbyApi: SourceNodesArgs,
pluginOptions: MedusaPluginOptions pluginOptions: MedusaPluginOptions,
lastBuildTime: string
): Promise<void> { ): Promise<void> {
const { const {
incrementalProductsOperation, incrementalProductsOperation,
incrementalRegionsOperation, incrementalRegionsOperation,
incrementalOrdersOperation, incrementalOrdersOperation,
incrementalCollectionsOperation, incrementalCollectionsOperation,
} = createOperations(pluginOptions, gatsbyApi) } = createOperations(pluginOptions)
const lastBuildTime = new Date(
gatsbyApi.store.getState().status.plugins?.[`gatsby-source-medusa`]?.[
`lastBuildTime`
]
)
for (const nodeType of medusaNodeTypes) { for (const nodeType of medusaNodeTypes) {
gatsbyApi gatsbyApi
@@ -94,36 +89,25 @@ export async function sourceUpdatedNodes(
} }
} }
export async function onPostBuild({ cache }: { cache: any }): Promise<void> {
await cache.set("timestamp", Date.now())
}
export async function sourceNodes( export async function sourceNodes(
gatsbyApi: SourceNodesArgs, gatsbyApi: SourceNodesArgs,
pluginOptions: MedusaPluginOptions pluginOptions: MedusaPluginOptions
): Promise<void> { ): Promise<void> {
const pluginStatus = const { cache } = gatsbyApi
gatsbyApi.store.getState().status.plugins?.[`gatsby-source-medusa`] const lastBuildTime = await cache.get("timestamp")
const lastBuildTime = pluginStatus?.[`lastBuildTime`]
if (lastBuildTime !== undefined) { if (lastBuildTime !== undefined) {
gatsbyApi.reporter.info( await sourceUpdatedNodes(gatsbyApi, pluginOptions, lastBuildTime)
`Cache is warm, but incremental builds are currently not supported. Running a clean build.`
)
await sourceAllNodes(gatsbyApi, pluginOptions)
} else { } else {
gatsbyApi.reporter.info(`Cache is cold, running a clean build.`) gatsbyApi.reporter.info(`Cache is cold, running a clean build.`)
await sourceAllNodes(gatsbyApi, pluginOptions) await sourceAllNodes(gatsbyApi, pluginOptions)
} }
gatsbyApi.reporter.info(`Finished sourcing nodes, caching last build time`) gatsbyApi.reporter.info(`Finished sourcing nodes`)
gatsbyApi.actions.setPluginStatus(
pluginStatus !== undefined
? {
...pluginStatus,
[`lastBuildTime`]: Date.now(),
}
: {
[`lastBuildTime`]: Date.now(),
}
)
} }
export function createResolvers({ createResolvers }: CreateResolversArgs): any { export function createResolvers({ createResolvers }: CreateResolversArgs): any {
+10 -14
View File
@@ -1,11 +1,7 @@
import { SourceNodesArgs } from "gatsby"
import { createClient } from "./client" import { createClient } from "./client"
export function createOperations( export function createOperations(options: MedusaPluginOptions): IOperations {
options: MedusaPluginOptions, const client = createClient(options)
{ reporter }: SourceNodesArgs
): IOperations {
const client = createClient(options, reporter)
function createOperation( function createOperation(
name: "products" | "collections" | "regions" | "orders", name: "products" | "collections" | "regions" | "orders",
@@ -22,13 +18,13 @@ export function createOperations(
createCollectionsOperation: createOperation("collections"), createCollectionsOperation: createOperation("collections"),
createRegionsOperation: createOperation("regions"), createRegionsOperation: createOperation("regions"),
createOrdersOperation: createOperation("orders"), createOrdersOperation: createOperation("orders"),
incrementalProductsOperation: (date: Date): any => incrementalProductsOperation: (date: string): any =>
createOperation("products", date.toISOString()), createOperation("products", date),
incrementalCollectionsOperation: (date: Date): any => incrementalCollectionsOperation: (date: string): any =>
createOperation("collections", date.toISOString()), createOperation("collections", date),
incrementalRegionsOperation: (date: Date): any => incrementalRegionsOperation: (date: string): any =>
createOperation("regions", date.toISOString()), createOperation("regions", date),
incrementalOrdersOperation: (date: Date): any => incrementalOrdersOperation: (date: string): any =>
createOperation("orders", date.toISOString()), createOperation("orders", date),
} }
} }
+4 -4
View File
@@ -22,8 +22,8 @@ interface IOperations {
createCollectionsOperation: IMedusaOperation createCollectionsOperation: IMedusaOperation
createRegionsOperation: IMedusaOperation createRegionsOperation: IMedusaOperation
createOrdersOperation: IMedusaOperation createOrdersOperation: IMedusaOperation
incrementalProductsOperation: (date: Date) => IMedusaOperation incrementalProductsOperation: (date: string) => IMedusaOperation
incrementalCollectionsOperation: (date: Date) => IMedusaOperation incrementalCollectionsOperation: (date: string) => IMedusaOperation
incrementalRegionsOperation: (date: Date) => IMedusaOperation incrementalRegionsOperation: (date: string) => IMedusaOperation
incrementalOrdersOperation: (date: Date) => IMedusaOperation incrementalOrdersOperation: (date: string) => IMedusaOperation
} }