Skip to content

Commit

Permalink
feat(gatsby-source-drupal): add typePrefix option (#37967)
Browse files Browse the repository at this point in the history
  • Loading branch information
ascorbic committed Apr 27, 2023
1 parent b116f7e commit 3f8c55b
Show file tree
Hide file tree
Showing 9 changed files with 205 additions and 131 deletions.
1 change: 1 addition & 0 deletions examples/using-drupal/gatsby-config.js
Expand Up @@ -8,6 +8,7 @@ module.exports = {
options: {
baseUrl: `https://live-contentacms.pantheonsite.io/`,
apiBase: `api`,
typePrefix: "Drupal",
},
},
{
Expand Down
12 changes: 6 additions & 6 deletions examples/using-drupal/src/pages/index.js
Expand Up @@ -220,11 +220,11 @@ export default IndexPage

export const pageQuery = graphql`
{
topRecipe: allRecipes(sort: { createdAt: ASC }, limit: 1) {
topRecipe: allDrupalRecipes(sort: { createdAt: ASC }, limit: 1) {
edges {
node {
title
gatsbyPath(filePath: "/{Recipes.title}")
gatsbyPath(filePath: "/{DrupalRecipes.title}")
relationships {
image {
relationships {
Expand All @@ -243,15 +243,15 @@ export const pageQuery = graphql`
}
}
}
nextTwoPromotedRecipes: allRecipes(
nextTwoPromotedRecipes: allDrupalRecipes(
sort: { createdAt: ASC }
limit: 2
skip: 1
) {
edges {
node {
title
gatsbyPath(filePath: "/{Recipes.title}")
gatsbyPath(filePath: "/{DrupalRecipes.title}")
relationships {
category {
name
Expand All @@ -273,7 +273,7 @@ export const pageQuery = graphql`
}
}
}
nextFourPromotedRecipes: allRecipes(
nextFourPromotedRecipes: allDrupalRecipes(
sort: { createdAt: ASC }
limit: 4
skip: 3
Expand All @@ -282,7 +282,7 @@ export const pageQuery = graphql`
node {
id
title
gatsbyPath(filePath: "/{Recipes.title}")
gatsbyPath(filePath: "/{DrupalRecipes.title}")
relationships {
category {
name
Expand Down
4 changes: 2 additions & 2 deletions examples/using-drupal/src/pages/recipes.js
Expand Up @@ -23,11 +23,11 @@ export default Recipes

export const query = graphql`
query {
recipes: allRecipes(limit: 1000) {
recipes: allDrupalRecipes(limit: 1000) {
edges {
node {
title
gatsbyPath(filePath: "/{Recipes.title}")
gatsbyPath(filePath: "/{DrupalRecipes.title}")
}
}
}
Expand Down
Expand Up @@ -83,8 +83,8 @@ const RecipeTemplate = ({ data }) => (
export default RecipeTemplate

export const query = graphql`
query($id: String!) {
recipe: recipes(id: { eq: $id }) {
query ($id: String!) {
recipe: drupalRecipes(id: { eq: $id }) {
title
preparationTime
difficulty
Expand Down
2 changes: 1 addition & 1 deletion packages/babel-preset-gatsby-package/package.json
Expand Up @@ -44,6 +44,6 @@
"scripts": {
"build": "",
"prepare": "cross-env NODE_ENV=production npm run build",
"watch": "babel -w src --out-dir dist/ --ignore \"**/__tests__\" --extensions \".ts,.js\""
"watch": ""
}
}
31 changes: 31 additions & 0 deletions packages/gatsby-source-drupal/README.md
Expand Up @@ -486,6 +486,37 @@ module.exports = {

Some entities are not translatable like Drupal files and will return null result when language code from parent entity doesn't match up. These items can be specified as nonTranslatableEntities and receive the defaultLanguage as fallback.

## Type prefix

By default, types are created with names that match the types in Drupal. However you can use the `typePrefix` option to add a prefix to all types. This is useful if you have multiple Drupal sources and want to differentiate between them, or if you have Drupal types that conflict with other types in your site.

```javascript
// In your gatsby-config.js
module.exports = {
plugins: [
{
resolve: `gatsby-source-drupal`,
options: {
baseUrl: `https://live-contentacms.pantheonsite.io/`,
typePrefix: `Drupal`,
},
},
],
}
```

You would then query for `allDrupalArticle` instead of `allArticle`.

```graphql
{
allDrupalArticle {
nodes {
title
}
}
}
```

## Gatsby Preview (experimental)

You will need to have the Drupal module installed, more information on that here: https://www.drupal.org/project/gatsby
Expand Down
25 changes: 13 additions & 12 deletions packages/gatsby-source-drupal/src/gatsby-node.ts
Expand Up @@ -23,15 +23,14 @@ import {
imageCDNState,
} from "./normalize"

const {
import {
handleReferences,
handleWebhookUpdate,
createNodeIfItDoesNotExist,
handleDeletedNode,
drupalCreateNodeManifest,
getExtendedFileNodeData,
} = require(`./utils`)

} from "./utils"
const imageCdnDocs = `https://github.com/gatsbyjs/gatsby/tree/master/packages/gatsby-source-drupal#readme`

const agent = {
Expand Down Expand Up @@ -243,6 +242,7 @@ ${JSON.stringify(webhookBody, null, 4)}`
createNodeId,
createContentDigest,
entityReferenceRevisions,
pluginOptions,
})
reporter.log(`Deleted node: ${deletedNode.id}`)
}
Expand All @@ -264,6 +264,7 @@ ${JSON.stringify(webhookBody, null, 4)}`
createContentDigest,
getNode,
reporter,
pluginOptions,
})
}

Expand All @@ -278,8 +279,6 @@ ${JSON.stringify(webhookBody, null, 4)}`
getCache,
getNode,
reporter,
store,
languageConfig,
},
pluginOptions
)
Expand Down Expand Up @@ -402,6 +401,7 @@ ${JSON.stringify(webhookBody, null, 4)}`
createContentDigest,
getNode,
reporter,
pluginOptions,
})
}
}
Expand All @@ -416,6 +416,7 @@ ${JSON.stringify(webhookBody, null, 4)}`
createNodeId,
createContentDigest,
entityReferenceRevisions,
pluginOptions,
})
} else {
// The data could be a single Drupal entity or an array of Drupal
Expand All @@ -436,8 +437,6 @@ ${JSON.stringify(webhookBody, null, 4)}`
getCache,
getNode,
reporter,
store,
languageConfig,
},
pluginOptions
)
Expand Down Expand Up @@ -773,6 +772,7 @@ ${JSON.stringify(webhookBody, null, 4)}`
createNodeId,
cache,
entityReferenceRevisions,
pluginOptions,
})
}

Expand All @@ -782,7 +782,9 @@ ${JSON.stringify(webhookBody, null, 4)}`
reporter.info(`Downloading remote files from Drupal`)

// Download all files (await for each pool to complete to fix concurrency issues)
const fileNodes = [...nodes.values()].filter(isFileNode)
const fileNodes = [...nodes.values()].filter(node =>
isFileNode(node, pluginOptions.typePrefix)
)

if (fileNodes.length) {
const downloadingFilesActivity = reporter.activityTimer(
Expand All @@ -795,12 +797,10 @@ ${JSON.stringify(webhookBody, null, 4)}`
await downloadFile(
{
node,
store,
cache,
createNode,
createNodeId,
getCache,
reporter,
},
pluginOptions
)
Expand Down Expand Up @@ -833,7 +833,6 @@ exports.onCreateDevServer = (
createNodeId,
getNode,
actions,
store,
cache,
createContentDigest,
getCache,
Expand Down Expand Up @@ -873,7 +872,6 @@ exports.onCreateDevServer = (
getCache,
getNode,
reporter,
store,
},
pluginOptions
)
Expand Down Expand Up @@ -913,6 +911,9 @@ exports.pluginOptionsSchema = ({ Joi }) =>
disallowedLinkTypes: Joi.array().items(Joi.string()),
skipFileDownloads: Joi.boolean(),
imageCDN: Joi.boolean().default(true),
typePrefix: Joi.string()
.description(`Prefix for Drupal node types`)
.default(``),
fastBuilds: Joi.boolean(),
entityReferenceRevisions: Joi.array().items(Joi.string()),
secret: Joi.string().description(
Expand Down

0 comments on commit 3f8c55b

Please sign in to comment.