feat: gatsby source medusa (#924)

This commit is contained in:
Kasper Fabricius Kristensen authored and GitHub committed 2021-12-14 20:56:31 +01:00
1 parent d0d8dd7bf6
commit b8ff364276
18 files changed
+12013

No files matched your search

@@ -0,0 +1,227 @@
import {
CreateResolversArgs,
GatsbyCache,
Node,
PluginOptionsSchemaArgs,
Reporter,
SourceNodesArgs,
Store,
} from "gatsby"
import { createRemoteFileNode } from "gatsby-source-filesystem"
import { makeSourceFromOperation } from "./make-source-from-operation"
import { createOperations } from "./operations"
export function pluginOptionsSchema({ Joi }: PluginOptionsSchemaArgs): any {
return Joi.object({
storeUrl: Joi.string().required(),
apiKey: Joi.string().optional(),
})
}
async function sourceAllNodes(
gatsbyApi: SourceNodesArgs,
pluginOptions: MedusaPluginOptions
): Promise<void> {
const {
createProductsOperation,
createRegionsOperation,
createOrdersOperation,
createCollectionsOperation,
} = createOperations(pluginOptions, gatsbyApi)
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)
}
}
const medusaNodeTypes = [
"MedusaRegions",
"MedusaProducts",
"MedusaOrders",
"MedusaCollections",
]
async function sourceUpdatedNodes(
gatsbyApi: SourceNodesArgs,
pluginOptions: MedusaPluginOptions
): Promise<void> {
const {
incrementalProductsOperation,
incrementalRegionsOperation,
incrementalOrdersOperation,
incrementalCollectionsOperation,
} = createOperations(pluginOptions, gatsbyApi)
const lastBuildTime = new Date(
gatsbyApi.store.getState().status.plugins?.[`gatsby-source-medusa`]?.[
`lastBuildTime`
]
)
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)
}
}
export async function sourceNodes(
gatsbyApi: SourceNodesArgs,
pluginOptions: MedusaPluginOptions
): Promise<void> {
const pluginStatus =
gatsbyApi.store.getState().status.plugins?.[`gatsby-source-medusa`]
const lastBuildTime = pluginStatus?.[`lastBuildTime`]
if (lastBuildTime !== undefined) {
gatsbyApi.reporter.info(
`Cache is warm, but incremental builds are currently not supported. Running a clean build.`
)
await sourceAllNodes(gatsbyApi, pluginOptions)
} else {
gatsbyApi.reporter.info(`Cache is cold, running a clean build.`)
await sourceAllNodes(gatsbyApi, pluginOptions)
}
gatsbyApi.reporter.info(`Finished sourcing nodes, caching last build time`)
gatsbyApi.actions.setPluginStatus(
pluginStatus !== undefined
? {
...pluginStatus,
[`lastBuildTime`]: Date.now(),
}
: {
[`lastBuildTime`]: Date.now(),
}
)
}
export function createResolvers({ createResolvers }: CreateResolversArgs): any {
const resolvers = {
MedusaProducts: {
images: {
type: ["MedusaImages"],
resolve: async (
source: any,
_args: any,
context: any,
_info: any
): Promise<any> => {
const { entries } = await context.nodeModel.findAll({
query: {
filter: { parent: { id: { eq: source.id } } },
},
type: "MedusaImages",
})
return entries
},
},
},
}
createResolvers(resolvers)
}
export async function createSchemaCustomization({
actions: { createTypes },
}: {
actions: { createTypes: any }
schema: any
}): Promise<void> {
createTypes(`
type MedusaProducts implements Node {
thumbnail: File @link(from: "fields.localThumbnail")
}
type MedusaImages implements Node {
image: File @link(from: "fields.localImage")
}
`)
}
export async function onCreateNode({
actions: { createNode, createNodeField },
cache,
createNodeId,
node,
store,
reporter,
}: {
actions: { createNode: any; createNodeField: any }
cache: GatsbyCache
createNodeId: any
node: Node
store: Store
reporter: Reporter
}): Promise<void> {
if (node.internal.type === `MedusaProducts`) {
if (node.thumbnail !== null) {
const thumbnailNode: Node | null = await createRemoteFileNode({
url: `${node.thumbnail}`,
parentNodeId: node.id,
createNode,
createNodeId,
cache,
store,
reporter,
})
if (thumbnailNode) {
createNodeField({
node,
name: `localThumbnail`,
value: thumbnailNode.id,
})
}
}
}
if (node.internal.type === `MedusaImages`) {
const imageNode: Node | null = await createRemoteFileNode({
url: `${node.url}`,
parentNodeId: node.id,
createNode,
createNodeId,
cache,
store,
reporter,
})
if (imageNode) {
createNodeField({
node,
name: `localImage`,
value: imageNode.id,
})
}
}
}