feat(gatsby-source-contentful): Add option to use Content Type ID for GraphQL schema generation #20265
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description
The
gatsby-source-contentful
plugin generates a GraphQL interface to Contentful. Fields are generated in the GraphQL schema based on the Content Types that are available in your Contentful space. At-present, the names of these fields are generated from the names of the content types in Contentful. As discussed in #9568 (and elsewhere, I'm sure) this is pretty fragile, as the name in Contentful is:Any changes to the display name in Contentful means refactoring to your project's code base in the following knock-on ways:
It's problematic that the display name is used for both natural-language UX as well as a unique identifier.
I understand this choice was originally made to combat the fact that Contentful will automatically generate you an ID if one is not specified at creation time (e.g. by not supplying one in the UI of the website, or when using the API) e.g.
c6XwpTaSiiI2Ak2Ww0oi6qa
. Using this ID for GraphQL and subsequent code references would obviously not be ideal.This PR introduces a config flag to specify whether the developer wants to use the Name or the ID of their content types for the GraphQL schema. Obviously, backwards compatibility is a must, as we cannot force everybody upgrading this package to refactor their entire codebase, so the flag defaults to using the Name - keeping identical behaviour as before (this is evident in the test snapshots that none of them had to be updated).
The flag is called
useNameForId
and it defaults totrue
. The documentation suggests that people should disable this flag if they are using natural-language IDs, or leave it enabled if you have a lot of generated IDs.The flag is structured this-way-around such that it may eventually default to
false
(a more natural default value). It seems to me thatid
is a much better property to be used as an identifier thanname
. Having an optional flag will still support people who need to deal with a lot of generated IDs. However, I don't know how we would migrate fromtrue
tofalse
as the default; perhaps when the package leaps its next major semver.Documentation
The README.md of the
gatsby-source-contentful
package has been updated to outline the config flag, what it does, and when developers should usetrue
vs.false
.Related Issues