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(gatsby-source-shopify): download images option #23840

Merged
Show file tree
Hide file tree
Changes from 18 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
7 changes: 7 additions & 0 deletions packages/gatsby-source-shopify/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,13 @@ plugins: [
// Possible values are: 'shop' and 'content'.
// Defaults to ['shop', 'content'].
includeCollections: ["shop", "content"],
// Download Images Locally
// set to false if you plan on using shopify's CDN
downloadImages: true,
// default image URL
// if you set downloadImages to false then it will set all localFile of file from this URL this is to make graphQL types unchanged
defaultImageURL:
"https://raw.githubusercontent.com/mrhut10/gatsby/Feature/gatsby-source-shopify/downloadImagesOption/packages/gatsby-source-shopify/src/default.png",
mrhut10 marked this conversation as resolved.
Show resolved Hide resolved

// Allow overriding the default queries
// This allows you to include/exclude extra fields when sourcing nodes
Expand Down
Binary file added packages/gatsby-source-shopify/src/default.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
13 changes: 13 additions & 0 deletions packages/gatsby-source-shopify/src/gatsby-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import chalk from "chalk"
import { forEach } from "p-iteration"
import { printGraphQLError, queryAll, queryOnce } from "./lib"
import { createClient } from "./create-client"
import PluginOptions from "./plugin-options"

import {
ArticleNode,
Expand Down Expand Up @@ -56,9 +57,21 @@ export const sourceNodes = async (
verbose = true,
paginationSize = 250,
includeCollections = [SHOP, CONTENT],
downloadImages = true,
defaultImageUrl = `https://raw.githubusercontent.com/mrhut10/gatsby/Feature/gatsby-source-shopify/downloadImagesOption/packages/gatsby-source-shopify/src/default.png`,
shopifyQueries = {},
}
) => {
// sending options into PluginOptions Singleton Class
PluginOptions.setShopName(shopName)
mrhut10 marked this conversation as resolved.
Show resolved Hide resolved
.setAccessToken(accessToken)
.setApiVersion(apiVersion)
.setVerbose(verbose)
.setPaginationSize(paginationSize)
.setIncludeCollections(includeCollections)
.setDownloadImages(downloadImages)
.setDefaultImageUrl(defaultImageUrl)

const client = createClient(shopName, accessToken, apiVersion)

const defaultQueries = {
Expand Down
8 changes: 6 additions & 2 deletions packages/gatsby-source-shopify/src/nodes.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import createNodeHelpers from "gatsby-node-helpers"
import { map } from "p-iteration"
import { createRemoteFileNode } from "gatsby-source-filesystem"
import PluginOptions from "./plugin-options"

import {
TYPE_PREFIX,
Expand Down Expand Up @@ -28,7 +29,10 @@ const downloadImageAndCreateFileNode = async (
) => {
let fileNodeID

const mediaDataCacheKey = `${TYPE_PREFIX}__Media__${url}`
const effectiveUrl = PluginOptions.downloadImages
? url
: PluginOptions.defaultImageUrl
const mediaDataCacheKey = `${TYPE_PREFIX}__Media__${effectiveUrl}`
const cacheMediaData = await cache.get(mediaDataCacheKey)

if (cacheMediaData) {
Expand All @@ -38,7 +42,7 @@ const downloadImageAndCreateFileNode = async (
}

const fileNode = await createRemoteFileNode({
url,
mrhut10 marked this conversation as resolved.
Show resolved Hide resolved
url: effectiveUrl,
store,
cache,
createNode,
Expand Down
93 changes: 93 additions & 0 deletions packages/gatsby-source-shopify/src/plugin-options.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
// created this singalton class as a quick way to reference options anywhere within the plugin

class PluginOptions {
#shopName

#accessToken

#apiVersion

#verbose

#paginationSize

#includeCollections

#downloadImages

#defaultImageUrl

get shopName() {
return this.#shopName
}

get accessToken() {
return this.#accessToken
}

get apiVersion() {
return this.#apiVersion || `2020-01`
}

get verbose() {
return this.#verbose || true
}

get paginationSize() {
return this.#paginationSize || 250
}

get includeCollections() {
return this.#includeCollections || [`shop`, `content`]
}

get downloadImages() {
return this.#downloadImages
}

get defaultImageUrl() {
return this.#defaultImageUrl
}

setShopName(shopName) {
this.#shopName = shopName
return this
}

setAccessToken(accessToken) {
this.#accessToken = accessToken
return this
}

setApiVersion(apiVersion) {
this.#apiVersion = apiVersion
return this
}

setVerbose(verbose) {
this.#verbose = verbose
return this
}

setPaginationSize(paginationSize) {
this.#paginationSize = paginationSize
return this
}

setIncludeCollections(includeCollections) {
this.#includeCollections = includeCollections
return this
}

setDownloadImages(downloadImages) {
this.#downloadImages = downloadImages
return this
}

setDefaultImageUrl(defaultImageUrl) {
this.#defaultImageUrl = defaultImageUrl
return this
}
}

export default new PluginOptions()