-
Notifications
You must be signed in to change notification settings - Fork 10.3k
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
Refactor Contentful Rich Text Field integration #24905
Refactor Contentful Rich Text Field integration #24905
Conversation
I have a working Next step is to allow all possible Contentful node types as reference. |
Will this require a major release of gatsby-source-contentful? |
@KyleAMathews this won't be breaking when not using rich text. Rich text users would require to extend their query if they use linked entities and change 2-3 lines of code per template file. |
this is breaking then, right? If a user has to change their code (e.g. if they're using RichText) then we should designate this release as such (e.g. bump to a new major version). Nicely done here! |
@DSchau yeah, even when its not likely that the page was rendering before the fix, we should mark it as breaking change |
49ac1e4
to
cce9c6b
Compare
Thanks to @vladar we now have an interface for all Contentful content types. Will now work on the rendering part! |
Some features we will get with this PR:
|
@@ -547,13 +546,44 @@ exports.extendNodeType = ({ type, store }) => { | |||
return { | |||
nodeType: { | |||
type: GraphQLString, | |||
deprecationReason: `This field is deprecated, please use 'json' instead.`, | |||
deprecationReason: `This field is deprecated, please use 'raw' instead. @todo add link to migration steps.`, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note to myself:
Make sure to add migrations steps into the warnings!
I pushed the first version of the rendering function. Usage is pretty simple:
import React from "react"
import Layout from "../components/layout"
import SEO from "../components/seo"
/* Import the render function: */
import { renderRichText } from "gatsby-source-contentful/rich-text"
const IndexPage = ({ data }) => {
return (
<Layout>
<SEO title="Home" />
{data.allContentfulPage.nodes.map(({title, richTextField}) => (
<div>
<h1>{title}</h1>
{/* Render the Rich Text field: */}
{richTextField && renderRichText(richTextField, {
// Same options as:
// https://github.com/contentful/rich-text/tree/master/packages/rich-text-react-renderer
})}
</div>
))}
</Layout>
)
}
export const pageQuery = graphql`
query {
allContentfulPage(limit: 1) {
nodes {
title
richTextField {
raw
# Query the fields you need on your references:
references {
contentful_id
... on ContentfulPage {
title
}
... on ContentfulProduct {
name
}
}
}
}
}
}
`
export default IndexPage A few issues:
Ideas and opinions very welcome. I will continue next week. Have a great weekend, |
1409cf9
to
135b32e
Compare
e432498
to
0a5d8f9
Compare
679fd35
to
9c627fc
Compare
5f8d1dd
to
b7eee19
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Overall looks good to me 👍 but added a couple of minor suggestions inline.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't know much about Gatsby but usage of RT seems fine @axe312ger 👍
const dummyResponse = { | ||
items: [ | ||
{ | ||
sys: { type: `Entry` }, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looking at https://github.com/contentful/contentful-resolve-response it doesn't seem necessary to include the sys
unless I am missing something.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It uses the type to create lookup keys and avoid id collisions
https://github.com/contentful/contentful-resolve-response/blob/master/index.js#L20
9c627fc
to
f69face
Compare
5a31d85
to
09ede3c
Compare
f69face
to
14891dc
Compare
…es (#23514) * fix(gatsby-source-contentful): fixed id collision in contentful entries
…o support circular references within Rich Text fields
* Allow circular references * Improve performance and reduce RAM footprint * Query referenced entries and assets via GraphQL fixes #24221 BREAKING CHANGE: * Entities references in Rich Text fields are no more automatically resolved * Use the `raw` subfield instead of `json` * Use GraphQL to define your referenced data with the new `references` field * Removes the `resolveFieldLocales` as the new `references` field automatically resolves locales * To render Rich Text fields unse the new `renderRichText()` function from `gatsby-source-contentful/rich-text`
09ede3c
to
099d0e8
Compare
Complex data models in combination with Contentful rich text can cause issues. The code can become slow, the worst case will crash the build process.
This is a follow up to #15084 and tries to fix #24221
More detailed description here: #24221 (comment)
references
for querying data of references entities via GraphQL.documentToReactComponents
and takes care of resolving the references based on our custom GraphQL Query data.Code demo: