Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
29 changes: 19 additions & 10 deletions plugin/src/create-schema-customization.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { GatsbyNode } from "gatsby"
import type { IPluginOptionsInternal } from "./types"
import { gatsbyNodeTypeName, normalizeCollections } from "./utils"
import { isString } from "lodash"
import { isArray, isString } from "lodash"

/**
* By default Gatsby, infers the data types for each node. This can be sometimes brittle or lead to hard-to-debug errors.
Expand All @@ -22,6 +22,7 @@ export const createSchemaCustomization: GatsbyNode[`createSchemaCustomization`]
const { createTypes } = actions

const schemaCustomizations = []
const normalizedUploadTypes = normalizeCollections(pluginOptions.uploadTypes, pluginOptions.endpoint)

if (pluginOptions.imageCdn) {
schemaCustomizations.push(`
Expand All @@ -32,20 +33,28 @@ export const createSchemaCustomization: GatsbyNode[`createSchemaCustomization`]
height: Int!
}
`)
const normalizedUploadTypes = normalizeCollections(pluginOptions.uploadTypes, pluginOptions.endpoint)
normalizedUploadTypes.forEach((uploadType) => {
const type = gatsbyNodeTypeName({
payloadSlug: uploadType.type,
...(isString(pluginOptions.prefix) && { prefix: pluginOptions.prefix as string }),
})
console.log(type)
}

normalizedUploadTypes.forEach((uploadType) => {
const type = gatsbyNodeTypeName({
payloadSlug: uploadType.type,
...(isString(pluginOptions.prefix) && { prefix: pluginOptions.prefix as string }),
})
if (pluginOptions.imageCdn) {
schemaCustomizations.push(`
type ${type} implements Node {
gatsbyImageCdn: Asset @link
}
`)
})
}
}
if (pluginOptions.localFiles === true || (isArray(pluginOptions.localFiles) && pluginOptions.localFiles.includes(uploadType.type))) {
schemaCustomizations.push(`
type ${type} implements Node {
localFile: File @link
}
`)
}
})

/**
* You most often will use SDL syntax to define your data types. However, you can also use type builders for more advanced use cases
Expand Down
7 changes: 5 additions & 2 deletions plugin/src/plugin-options-schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,11 @@ export const pluginOptionsSchema: GatsbyNode["pluginOptionsSchema"] = ({ Joi }):
nodePrefix: Joi.string(),
// Optional. Map Payload locales to different strings in the resulting nodes.
nodeTransform: Joi.object(),
// Optional. Create local file nodes for upload collections.
localFiles: Joi.boolean(),
// Optional. Create local file nodes for upload collections. Pass upload collection slugs to restrict file node creation by collection.
localFiles: Joi.alternatives(
Joi.boolean(),
Joi.array().items(Joi.string()),
),
// Optional. Create Gatsby Image CDN asset nodes for upload collections.
imageCdn: Joi.boolean(),
/** A base URL for constructing imageUrls. Required for `localFiles`. */
Expand Down
10 changes: 6 additions & 4 deletions plugin/src/source-nodes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { fetchEntity, fetchEntities } from "./fetch"
import { normalizeGlobals, payloadImageUrl } from "./utils"
import { gatsbyNodeTypeName, documentRelationships } from "./utils"
import { createRemoteFileNode } from "gatsby-source-filesystem"
import { get, isString, pickBy } from "lodash"
import { get, isArray, isString, pickBy } from "lodash"
import { normalizeCollections } from "./utils"

let isFirstSource = true
Expand Down Expand Up @@ -171,9 +171,9 @@ export const sourceNodes: GatsbyNode[`sourceNodes`] = async (gatsbyApi, pluginOp

for (const result of uploadResults) {
for (const upload of result) {
let imageCdnId
if (pluginOptions.localFiles) {
createLocalFileNode(context, upload, relationshipIds)
let localFileId, imageCdnId
if (pluginOptions.localFiles === true || (isArray(pluginOptions.localFiles) && pluginOptions.localFiles.includes(upload.gatsbyNodeType))) {
localFileId = await createLocalFileNode(context, upload, relationshipIds)
}
if (pluginOptions.imageCdn) {
imageCdnId = await createAssetNode(context, upload, relationshipIds)
Expand All @@ -187,6 +187,7 @@ export const sourceNodes: GatsbyNode[`sourceNodes`] = async (gatsbyApi, pluginOp
}),
data: {
...upload,
...(localFileId && { localFile: localFileId }),
...(imageCdnId && { gatsbyImageCdn: imageCdnId }),
},
},
Expand Down Expand Up @@ -281,6 +282,7 @@ export async function createLocalFileNode(
value: relationships,
})
}
return fileNode.id
}

export function createAssetNode(context: SourceNodesArgs, data: any, relationshipIds?: { [key: string]: string }) {
Expand Down
4 changes: 3 additions & 1 deletion site/gatsby-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,9 @@ const config: GatsbyConfig = {
options: {
endpoint: process.env.PAYLOAD_BASE_URL,
retries: 3,
localFiles: false,
localFiles: [
'marketing-site-images'
],
imageCdn: true,
baseUrl: process.env.PAYLOAD_CDN_URL,
collectionTypes: [
Expand Down