Skip to content

Commit 16e8086

Browse files
committed
refactor: align Rich Text field structure to Contentful GraphQL API (#31122)
1 parent 3a836cd commit 16e8086

File tree

6 files changed

+311
-207
lines changed

6 files changed

+311
-207
lines changed

e2e-tests/contentful/src/pages/rich-text.js

Lines changed: 67 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,13 @@ function renderReferencedComponent(ref) {
2020
return <Component {...ref} />
2121
}
2222

23-
const options = {
23+
const makeOptions = ({ assetBlockMap, entryBlockMap, entryInlineMap }) => ({
2424
renderMark: {
2525
[MARKS.BOLD]: text => <strong data-cy-strong>{text}</strong>,
2626
},
2727
renderNode: {
2828
[BLOCKS.EMBEDDED_ASSET]: node => {
29-
const asset = node.data.target
29+
const asset = assetBlockMap.get(node?.data?.target?.sys.id)
3030
if (asset.fluid) {
3131
return <GatsbyImage {...asset} style={{ width: 200 }} />
3232
}
@@ -40,7 +40,7 @@ const options = {
4040
)
4141
},
4242
[BLOCKS.EMBEDDED_ENTRY]: node => {
43-
const entry = node?.data?.target
43+
const entry = entryBlockMap.get(node?.data?.target?.sys.id)
4444
if (!entry) {
4545
throw new Error(
4646
`Entity not available for node:\n${JSON.stringify(node, null, 2)}`
@@ -49,7 +49,7 @@ const options = {
4949
return renderReferencedComponent(entry)
5050
},
5151
[INLINES.EMBEDDED_ENTRY]: node => {
52-
const entry = node.data.target
52+
const entry = entryInlineMap.get(node?.data?.target?.sys.id)
5353
if (entry.__typename === "ContentfulText") {
5454
return (
5555
<span data-cy-id="inline-text">
@@ -64,7 +64,7 @@ const options = {
6464
)
6565
},
6666
},
67-
}
67+
})
6868

6969
const RichTextPage = ({ data }) => {
7070
const entries = data.allContentfulRichText.nodes
@@ -75,7 +75,7 @@ const RichTextPage = ({ data }) => {
7575
return (
7676
<div data-cy-id={slug} key={id}>
7777
<h2>{title}</h2>
78-
{renderRichText(richText, options)}
78+
{renderRichText(richText, makeOptions)}
7979
<hr />
8080
</div>
8181
)
@@ -93,77 +93,94 @@ export const pageQuery = graphql`
9393
id
9494
title
9595
richText {
96-
raw
97-
references {
98-
__typename
99-
sys {
100-
id
101-
}
102-
... on ContentfulAsset {
103-
fluid(maxWidth: 200) {
104-
...GatsbyContentfulFluid
105-
}
106-
}
107-
... on ContentfulText {
108-
title
109-
short
110-
}
111-
... on ContentfulLocation {
112-
location {
113-
lat
114-
lon
96+
json
97+
links {
98+
assets {
99+
block {
100+
sys {
101+
id
102+
}
103+
fluid(maxWidth: 200) {
104+
...GatsbyContentfulFluid
105+
}
115106
}
116107
}
117-
... on ContentfulContentReference {
118-
title
119-
one {
108+
entries {
109+
block {
120110
__typename
121111
sys {
122112
id
113+
type
123114
}
124115
... on ContentfulText {
125116
title
126117
short
127118
}
119+
... on ContentfulLocation {
120+
location {
121+
lat
122+
lon
123+
}
124+
}
128125
... on ContentfulContentReference {
129126
title
130127
one {
128+
__typename
129+
sys {
130+
id
131+
}
132+
... on ContentfulText {
133+
title
134+
short
135+
}
131136
... on ContentfulContentReference {
132137
title
138+
one {
139+
... on ContentfulContentReference {
140+
title
141+
}
142+
}
143+
many {
144+
... on ContentfulContentReference {
145+
title
146+
}
147+
}
133148
}
134149
}
135150
many {
151+
__typename
152+
sys {
153+
id
154+
}
155+
... on ContentfulText {
156+
title
157+
short
158+
}
159+
... on ContentfulNumber {
160+
title
161+
integer
162+
}
136163
... on ContentfulContentReference {
137164
title
165+
one {
166+
... on ContentfulContentReference {
167+
title
168+
}
169+
}
170+
many {
171+
... on ContentfulContentReference {
172+
title
173+
}
174+
}
138175
}
139176
}
140177
}
141178
}
142-
many {
179+
inline {
143180
__typename
144181
sys {
145182
id
146-
}
147-
... on ContentfulText {
148-
title
149-
short
150-
}
151-
... on ContentfulNumber {
152-
title
153-
integer
154-
}
155-
... on ContentfulContentReference {
156-
title
157-
one {
158-
... on ContentfulContentReference {
159-
title
160-
}
161-
}
162-
many {
163-
... on ContentfulContentReference {
164-
title
165-
}
166-
}
183+
type
167184
}
168185
}
169186
}

packages/gatsby-source-contentful/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
},
99
"dependencies": {
1010
"@babel/runtime": "^7.12.5",
11+
"@contentful/rich-text-links": "^14.1.2",
1112
"@contentful/rich-text-react-renderer": "^14.1.2",
1213
"@contentful/rich-text-types": "^14.1.2",
1314
"@hapi/joi": "^15.1.1",

0 commit comments

Comments
 (0)