cbdc5b7774
**What** - Moves `sourceUpdatedNodes` and `sourceAllNodes` to a separate file, to prevent warning of unsupported exports. See #1455. - Adds warnings if `GET /store/products`, `GET /store/regions`, and `GET /store/collections` return empty arrays. This should help new users more easily figure out why their storefronts does not work as expected. - Adds schema to plugin, so that node types for products, regions and collections are always created. This will prevent errors such as `allMedusaRegions query failed` from happening in the storefront, as the query will be valid as the type exists, even if it returns nothing. This should make the gatsby plugin/starter easier to use. This error is an reoccurring issue in our discord, when new users try to run the Gatsby starter without seeding the storefront beforehand. Resolves #1455 Co-authored-by: Sebastian Rindom <7554214+srindom@users.noreply.github.com>
75 lines
1.8 KiB
TypeScript
75 lines
1.8 KiB
TypeScript
import { SourceNodesArgs } from "gatsby"
|
|
import { makeSourceFromOperation } from "./make-source-from-operation"
|
|
import { createOperations } from "./operations"
|
|
|
|
const medusaNodeTypes = [
|
|
"MedusaRegions",
|
|
"MedusaProducts",
|
|
"MedusaOrders",
|
|
"MedusaCollections",
|
|
]
|
|
|
|
export async function sourceAllNodes(
|
|
gatsbyApi: SourceNodesArgs,
|
|
pluginOptions: MedusaPluginOptions
|
|
): Promise<void> {
|
|
const {
|
|
createProductsOperation,
|
|
createRegionsOperation,
|
|
createOrdersOperation,
|
|
createCollectionsOperation,
|
|
} = createOperations(pluginOptions)
|
|
|
|
const operations = [
|
|
createProductsOperation,
|
|
createRegionsOperation,
|
|
createCollectionsOperation,
|
|
]
|
|
|
|
// if auth token is provided then source orders
|
|
if (pluginOptions.apiKey) {
|
|
operations.push(createOrdersOperation)
|
|
}
|
|
|
|
const sourceFromOperation = makeSourceFromOperation(gatsbyApi)
|
|
|
|
for (const op of operations) {
|
|
await sourceFromOperation(op)
|
|
}
|
|
}
|
|
|
|
export async function sourceUpdatedNodes(
|
|
gatsbyApi: SourceNodesArgs,
|
|
pluginOptions: MedusaPluginOptions,
|
|
lastBuildTime: string
|
|
): Promise<void> {
|
|
const {
|
|
incrementalProductsOperation,
|
|
incrementalRegionsOperation,
|
|
incrementalOrdersOperation,
|
|
incrementalCollectionsOperation,
|
|
} = createOperations(pluginOptions)
|
|
|
|
for (const nodeType of medusaNodeTypes) {
|
|
gatsbyApi
|
|
.getNodesByType(nodeType)
|
|
.forEach((node) => gatsbyApi.actions.touchNode(node))
|
|
}
|
|
|
|
const operations = [
|
|
incrementalProductsOperation(lastBuildTime),
|
|
incrementalRegionsOperation(lastBuildTime),
|
|
incrementalCollectionsOperation(lastBuildTime),
|
|
]
|
|
|
|
if (pluginOptions.apiKey) {
|
|
operations.push(incrementalOrdersOperation(lastBuildTime))
|
|
}
|
|
|
|
const sourceFromOperation = makeSourceFromOperation(gatsbyApi)
|
|
|
|
for (const op of operations) {
|
|
await sourceFromOperation(op)
|
|
}
|
|
}
|