Skip to content

Instantly share code, notes, and snippets.

@iksent
Last active February 10, 2021 09:31
Show Gist options
  • Save iksent/ef80fca97e18c16a0a6042214918ebe9 to your computer and use it in GitHub Desktop.
Save iksent/ef80fca97e18c16a0a6042214918ebe9 to your computer and use it in GitHub Desktop.
Gatsby + Strapi: Solving "dummy-content" problem for "Dynamic Zones"
const path = require('path')
const fs = require('fs')
function replaceAll(str, find, replace) {
return str.replace(new RegExp(find, 'g'), replace)
}
export const createSchemaCustomization = ({ actions, schema }) => {
const { createTypes } = actions
//
// Solving "dummy-content" problem for Strapi Dynamic Zones:
//
let strapiTypes = fs
.readFileSync(path.resolve('../backend/exports/graphql/schema.graphql'), 'utf8')
.toString()
// Changed name for reserved scalars:
strapiTypes = replaceAll(strapiTypes, 'scalar JSON', 'scalar JSON__IGNORED')
strapiTypes = replaceAll(strapiTypes, 'scalar Date\n', 'scalar Date__IGNORED\n')
// Change name for File-type:
strapiTypes = replaceAll(strapiTypes, 'type UploadFile {', 'type UploadFile__IGNORED {')
// Change File-type to type with localFile
strapiTypes = replaceAll(strapiTypes, 'UploadFile', 'LocalFile')
const myTypes = `
type LocalFile {
localFile: File @link(from: "localFile___NODE")
}
type StrapiProject implements Node {
sections_start: [UnionDynamicSection]
sections_end: [UnionDynamicSection]
}
type StrapiOutsourcingPage implements Node {
sections: [UnionDynamicSection]
}
type StrapiCompanyPage implements Node {
sections: [UnionDynamicSection]
}
type StrapiDesignPage implements Node {
sections: [UnionDynamicSection]
}
type StrapiPortfolioPage implements Node {
dynamic: [UnionDynamicSection]
}
`
const ComponentsByType = {
image_text: 'ComponentSectionsSectionImageText',
counters: 'ComponentSectionsSectionCounters',
texts: 'ComponentSectionsSectionTexts',
techs: 'ComponentSectionsSectionTechs',
dev: 'ComponentSectionsSectionDev',
advanced: 'ComponentSectionsSectionAdvanced',
}
const PortfolioPageDynamicDynamicZone = schema.buildUnionType({
name: `UnionDynamicSection`,
types: Object.values(ComponentsByType),
resolveType: (value, info) => {
// Each section has a special hidden param "type"
return ComponentsByType[value.type] || 'ComponentSectionsSectionTexts'
},
})
createTypes(strapiTypes)
createTypes(myTypes)
createTypes([PortfolioPageDynamicDynamicZone])
}
require(`@babel/register`)({
presets: ['@babel/preset-env', '@babel/preset-react'],
})
module.exports = require(`./gatsby-node-es6.js`)
Frontend:
{
"gatsby-source-strapi": "^1.0.0-alpha.0",
"gatsby": "^2.32.3",
}
Backend:
{
"strapi": "3.4.3",
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment