Skip to content

feat: Allow themes to modify GraphQL types - #16928

Closed
stefanprobst wants to merge 5 commits into
gatsbyjs:masterfrom
stefanprobst:allow-themes-to-modify-types
Closed

stefanprobst wants to merge 5 commits into
gatsbyjs:masterfrom
stefanprobst:allow-themes-to-modify-types

Conversation

@stefanprobst

@stefanprobst stefanprobst commented Aug 21, 2019 •

Copy link
Copy Markdown
Contributor

Currently, a GraphQL type can only be modified by the plugin which created that type, or by a user's gatsby-node. This excludes a theme's gatsby-node.

This PR allows a theme's gatsby-node to modify types. A top-level gatsby-node always takes precedence. If a type has already been modified by another theme, modification is not allowed.

Fixes #16529
Fixes #15544

@stefanprobst
stefanprobst requested a review from a team August 21, 2019 20:19
@stefanprobst

Copy link
Copy Markdown
Contributor Author

apparently node 11 (or v8 really) has changed the sort implementation, which is why this doesn't work anymore:

const sortedTypes = types.sort(
type => type.plugin && type.plugin.name === `default-site-plugin`
)

@lannonbr lannonbr added topic: GraphQL Related to Gatsby's GraphQL layer topic: themes labels Aug 22, 2019
Comment thread packages/gatsby/src/schema/schema.js Outdated
plugin.name === `default-site-plugin` ||
plugin.name === typeOwner
(plugin.name.startsWith(`gatsby-theme-`) &&
!typeOwner.startsWith(`gatsby-theme-`))

@wangyi7099 wangyi7099 Aug 23, 2019 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just wonder that does the name of a theme have to start with "gatsby-theme" ? If so, I think that's a little unreasonable. My theme plugin's name is antdsite , not starts with "gatsby-theme". I think we should judge theme in another way.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the gatsby-theme- naming is a requirement since #16620, so we can rely on that. it's not totally optimal since i don't think it is actually enforced anywhere, otoh the only thing you'll lose (currently) when naming your theme differently is the ability to modify types owned by plugins. i'm open to other proposals, please post them in #16529.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lol All right, since you haved discussed.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sorry, looks like my assumption was wrong

wardpeet
wardpeet previously approved these changes Aug 30, 2019

@wardpeet wardpeet left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code looks 👍 I added a suggestion to fix sorting.

I do have a question about the themes spec. Don't we want themes to override/use other themes and maybe also augment the graphql types of those child themes?

cc @gatsbyjs/themes-core

Comment thread packages/gatsby/src/schema/index.js Resolved
@michaellopez

Copy link
Copy Markdown
Contributor

@wardpeet

I do have a question about the themes spec. Don't we want themes to override/use other themes and maybe also augment the graphql types of those child themes?

We have this use case, currently we work around it via createNode + onCreateNode plus mediaType. But would definitely want that in core if possible.

@wardpeet

Copy link
Copy Markdown
Contributor

Sweet! Good workaround 👍. My comment about parent/child augmentation isn't blocking to get this PR merged so most people will already be happy

@stefanprobst

Copy link
Copy Markdown
Contributor Author

Don't we want themes to override/use other themes and maybe also augment the graphql types of those child themes?

#16529 did not really get lots of responses so i think this needs more discussion wrt to themes modifying types owned by other themes. this PR is a conservative start (following @KyleAMathews comment on slack)

Comment thread packages/gatsby/src/schema/schema.js Outdated
plugin.name === typeOwner ||
plugin.name === `default-site-plugin` ||
plugin.name === typeOwner
(plugin.name.startsWith(`gatsby-theme-`) &&

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a heavy handed suggestion but not a requirement, as detailed in #16529 (comment)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also this code would need to be modified to work for scoped packages like @namespace/gatsby-theme-thing as well

@wardpeet

wardpeet commented Sep 2, 2019

Copy link
Copy Markdown
Contributor

Could you add some tests for non vs scoped packages?

@stefanprobst

Copy link
Copy Markdown
Contributor Author

Sure, but let's first decide if we can actually rely on the naming convention?

@freiksenet

Copy link
Copy Markdown
Contributor

@stefanprobst I feel that we should probably remove restriction on modifying types by other plugins. I'm not sure there is any other solution, we don't want a "theme-class" and "non-theme-class" plugins.

CC @ChristopherBiscardi @KyleAMathews

@KyleAMathews

Copy link
Copy Markdown
Contributor

Perhaps we could do what we do for nodes? E.g. reserve a "fields" key on every type that other plugins can add additional fields to.

@chenlijun99

Copy link
Copy Markdown

@KyleAMathews What I'd like to be possible is to override some types, instead of extending them.
E.g. override and make explicit, instead of inferred, the type of a frontmatter from markdown.

@stefanprobst

Copy link
Copy Markdown
Contributor Author

@freiksenet

I feel that we should probably remove restriction on modifying types by other plugins. I'm not sure there is any other solution, we don't want a "theme-class" and "non-theme-class" plugins.

I'm not really clear why that is? To me it seems that the issue of "who is allowed to modify which types" is exactly a case where having some way to differentiate between themes and other-plugins would make sense (even if only by naming convention). Not sure I like dropping type ownership just to preserve the theme/plugin symmetry.

@KyleAMathews

Copy link
Copy Markdown
Contributor

What we have to prevent is scenarios where s site is working and someone installs a plugin (or theme) and the site stops working. This is incredibly confusing for everyone.

Allowing plugins or themes to override types leads to exactly that sort of problem.

@stefanprobst

Copy link
Copy Markdown
Contributor Author

ok, I'm going to close this as there doesn't seem to be consensus wrt to type modifications. if I understand correctly, @freiksenet's position is to remove restrictions for type modifications and allow them for all plugins, while @KyleAMathews's position is to not let plugins modify types at all.

let's clarify this in #16529

mrmartineau added a commit to mrmartineau/gatsby-theme-code-notes that referenced this pull request Apr 22, 2021
They don’t appear to be working even though themes are allowed to customise schemas.

Reference:
- gatsbyjs/gatsby#26864
- https://github.com/gatsbyjs/gatsby/issues/15544\
- gatsbyjs/gatsby#16928
mrmartineau added a commit to mrmartineau/gatsby-theme-code-notes that referenced this pull request Apr 22, 2021
* chore: bump gatsby, TS & React dependencies

* Remove custom schema types

They don’t appear to be working even though themes are allowed to customise schemas.

Reference:
- gatsbyjs/gatsby#26864
- https://github.com/gatsbyjs/gatsby/issues/15544\
- gatsbyjs/gatsby#16928

* Update peer deps for Gatsby
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

topic: GraphQL Related to Gatsby's GraphQL layer

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Clarify GraphQL type ownership and permissions for themes Allow themes to modify existing GraphQL types

9 participants