Skip to content

Instantly share code, notes, and snippets.

@iksent
Last active February 10, 2021 09:31

Revisions

  1. iksent revised this gist Feb 10, 2021. 1 changed file with 8 additions and 4 deletions.
    12 changes: 8 additions & 4 deletions gatsby-node-es6.js
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,10 @@
    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

    @@ -12,12 +16,12 @@ export const createSchemaCustomization = ({ actions, schema }) => {
    .readFileSync(path.resolve('../backend/exports/graphql/schema.graphql'), 'utf8')
    .toString()
    // Changed name for reserved scalars:
    strapiTypes = strapiTypes.replaceAll('scalar JSON', 'scalar JSON__IGNORED')
    strapiTypes = strapiTypes.replaceAll('scalar Date\n', 'scalar Date__IGNORED\n')
    strapiTypes = replaceAll(strapiTypes, 'scalar JSON', 'scalar JSON__IGNORED')
    strapiTypes = replaceAll(strapiTypes, 'scalar Date\n', 'scalar Date__IGNORED\n')
    // Change name for File-type:
    strapiTypes = strapiTypes.replaceAll('type UploadFile {', 'type UploadFile__IGNORED {')
    strapiTypes = replaceAll(strapiTypes, 'type UploadFile {', 'type UploadFile__IGNORED {')
    // Change File-type to type with localFile
    strapiTypes = strapiTypes.replaceAll('UploadFile', 'LocalFile')
    strapiTypes = replaceAll(strapiTypes, 'UploadFile', 'LocalFile')

    const myTypes = `
    type LocalFile {
  2. iksent created this gist Feb 10, 2021.
    66 changes: 66 additions & 0 deletions gatsby-node-es6.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,66 @@
    const path = require('path')
    const fs = require('fs')

    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 = strapiTypes.replaceAll('scalar JSON', 'scalar JSON__IGNORED')
    strapiTypes = strapiTypes.replaceAll('scalar Date\n', 'scalar Date__IGNORED\n')
    // Change name for File-type:
    strapiTypes = strapiTypes.replaceAll('type UploadFile {', 'type UploadFile__IGNORED {')
    // Change File-type to type with localFile
    strapiTypes = strapiTypes.replaceAll('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])
    }
    4 changes: 4 additions & 0 deletions gatsby-node.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,4 @@
    require(`@babel/register`)({
    presets: ['@babel/preset-env', '@babel/preset-react'],
    })
    module.exports = require(`./gatsby-node-es6.js`)
    10 changes: 10 additions & 0 deletions package.json
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,10 @@
    Frontend:
    {
    "gatsby-source-strapi": "^1.0.0-alpha.0",
    "gatsby": "^2.32.3",
    }

    Backend:
    {
    "strapi": "3.4.3",
    }