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

Is there a workaround for customising schema from a Gatsby Theme? #24428

Closed
JohJonker opened this issue May 25, 2020 · 1 comment
Closed

Is there a workaround for customising schema from a Gatsby Theme? #24428

JohJonker opened this issue May 25, 2020 · 1 comment
Labels
type: question or discussion Issue discussing or asking a question about Gatsby

Comments

@JohJonker
Copy link

Summary

I'm trying to specify schema using exports.createSchemaCustomization = ({ actions }, siteOptions) in my Gatsby Theme, but it's being ignored by the site that uses the theme. This is a known limitation which has seen some discussion (#15544, #16529).

My question: As of now, is there a workaround to do this?

I know we could move this to the site's gatsby-node.js, but that's unideal — as it creates unnecessary code duplication across sites, when it could just live in the theme — and I'm also having issues (#24427) accessing the config in the site.

Relevant information

My Gatsby theme allows my sites to specify Markdown content types with frontmatter fields, like so:

gatsby-config.js:

module.exports = {
  plugins: [
    {
      resolve: `MY_THEME_SAMPLE`,
      options: {
        contentTypes: [
          {
            id: 'news',
            name: 'News',
            urlBase: '/news/',
            singleTemplate: 'NewsArticle',
            archiveTemplate: 'News',
            frontmatterFields: [
              { name: 'thumbnail', type: 'File!' },
              { name: 'title', type: 'String!' },
              { name: 'date', type: 'Date!' },
              { name: 'summary', type: 'String!' },
              {
                name: 'people',
                type:
                  '[MarkdownRemark]! @link(by: "fields.slug", from: "people")',
              },
              { name: 'topics', type: '[String]!' },
            ],
          },
        ]
      }
    }
  ]
}

Ideally, I would've liked then liked to use this info to generate schema customisations in the theme's gatsby-node.js, like below. This code currently runs, but it's being ignored, i.e. the schema customisations aren't being applied. In the build output on the site using the theme, I get:

warn Plugin 'MY_THEME_SAMPLE' tried to define the GraphQL type 'MarkdownRemark', which has already been defined by the plugin

// Explicitly specify GraphQL schema for better performance and reliability
exports.createSchemaCustomization = ({ actions }, siteOptions) => {
  const { createTypes } = actions;

  const { contentTypes } = createConfig(siteOptions);
  const contentTypeDefinitions = {};
  for (let i = 0; i < contentTypes.length; i += 1) {
    for (let j = 0; j < contentTypes[i].frontmatterFields.length; j += 1) {
      if (
        contentTypeDefinitions[contentTypes[i].frontmatterFields[j].name] ===
        undefined
      ) {
        // Checks for duplicate field declarations; only the first will be declared
        contentTypeDefinitions[contentTypes[i].frontmatterFields[j].name] =
          contentTypes[i].frontmatterFields[j].type;
      }
    }
  }
  const finalDefinitions = `
  type MarkdownRemark implements Node {
    fields: Fields
    frontmatter: Frontmatter    
  }
  type Fields {
    slug: String
    collection: String
  }
  type Frontmatter {
      ${Object.keys(contentTypeDefinitions)
        .map((key) => `${key}: ${contentTypeDefinitions[key]}`)
        .join('\n')}
  }
`;
  createTypes(finalDefinitions);
};
@JohJonker JohJonker added the type: question or discussion Issue discussing or asking a question about Gatsby label May 25, 2020
@gatsbot gatsbot bot added the status: triage needed Issue or pull request that need to be triaged and assigned to a reviewer label May 25, 2020
@LekoArts
Copy link
Contributor

This feels like a bit of a duplicate of your other issue so I'm just referring to my other answer: #24427 (comment)

@LekoArts LekoArts removed the status: triage needed Issue or pull request that need to be triaged and assigned to a reviewer label May 25, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: question or discussion Issue discussing or asking a question about Gatsby
Projects
None yet
Development

No branches or pull requests

2 participants