Skip to content

Commit eae2679

Browse files
author
Jan Lenoch
authored
Merge pull request #26 from Kentico/guid-named-properties
Get around GraphQL naming limitations
2 parents 6c877a8 + 4bc826f commit eae2679

File tree

3 files changed

+81
-44
lines changed

3 files changed

+81
-44
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ All nodes have a `usedByContentItems` property that reflects the other nodes in
154154

155155
## Troubleshooting
156156

157-
Gatsby's GraphQL libraries won't accept object properties with names starting with numbers. This conflicts with the way image assets are named in the Delivery API response (in the JS SDK output, respectively). Therefore, the names of assets are prefixed by the source plugin with `image-`.
157+
Gatsby's GraphQL libraries won't accept object properties with names starting with numbers. This conflicts with the way image assets and links are named in the Delivery API response (in the JS SDK output, respectively). Therefore, the names of properties with assets and links in Rich text are prefixed by the source plugin with `image-` and `link-` respectively.
158158

159159
"images": {
160160
"image-79bd9e11-f643-4cd6-9ea5-d1be17cf7de2": {
@@ -168,7 +168,7 @@ Gatsby's GraphQL libraries won't accept object properties with names starting wi
168168
"url": "https://assets-eu-01.kc-usercontent.com:443/5ac93d1e-567d-01e6-e3b7-ac435f77b907/36cb3d1b-1aa7-4809-9606-82c3c0f00b7f/fcyTulxr.jpg"
169169
}
170170

171-
Should you need to refer to the properties, just bear in mind the addition of the `image-` suffix. The GUID in the child `image_id` property is never prefixed or otherwise transformed.
171+
Should you need to refer to the properties, just bear in mind the addition of the `image-` and `link-` prefix. In case of images, the GUID in the child `image_id` property is never prefixed or otherwise transformed.
172172

173173
## Further information
174174

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "gatsby-source-kentico-cloud",
33
"description": "Gatsby source plugin for Kentico Cloud",
4-
"version": "2.0.2",
4+
"version": "2.0.3",
55
"repository": {
66
"type": "git",
77
"url": "https://github.com/Kentico/gatsby-source-kentico-cloud"

src/normalize.js

Lines changed: 78 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -60,30 +60,7 @@ const createContentItemNode =
6060
(contentType) => contentType.system.codename
6161
=== contentItem.system.type);
6262

63-
const elements = {};
64-
65-
Object
66-
.keys(contentItem)
67-
.filter((key) => key !== `system` && key !== `elements`)
68-
.forEach((key) => {
69-
let propertyValue;
70-
71-
if (_.has(contentItem[key], `type`)
72-
&& contentItem[key].type === `rich_text`
73-
&& _.has(contentItem.elements[key], `images`)
74-
&& !_.isEmpty(contentItem.elements[key].images)) {
75-
propertyValue = prefixImagesInRichText(contentItem.elements[key]);
76-
} else {
77-
propertyValue = contentItem[key];
78-
}
79-
80-
elements[key] = propertyValue;
81-
});
82-
83-
const itemWithElements = {
84-
system: contentItem.system,
85-
elements: elements,
86-
};
63+
const itemWithElements = parseContentItemContents(contentItem);
8764

8865
const additionalData = {
8966
otherLanguages___NODE: [],
@@ -197,7 +174,9 @@ of valid objects.`);
197174
const linkedNodes = allNodesOfSameLanguage
198175
.filter((node) => {
199176
const match = property.find((propertyValue) => {
200-
return propertyValue.system.codename ===
177+
return propertyValue !== null
178+
&& node !== null
179+
&& propertyValue.system.codename ===
201180
node.system.codename
202181
&& propertyValue.system.type === node.system.type;
203182
});
@@ -241,7 +220,8 @@ of valid objects.`);
241220
const linkedNodes = allNodesOfSameLanguage
242221
.filter((node) => _.has(property, `linkedItemCodenames`)
243222
&& _.isArray(property.linkedItemCodenames)
244-
&& property.linkedItemCodenames.includes(node.system.codename)
223+
&& property.linkedItemCodenames.includes(
224+
node.system.codename)
245225
);
246226

247227
itemNode.elements[linkPropertyName] = [];
@@ -256,16 +236,17 @@ const createKcArtifactNode =
256236
additionalNodeData = null) => {
257237
let processedProperties = [];
258238

259-
// Handle circular references when serializing.
260-
const nodeContent = JSON.stringify(kcArtifact, (key, value) =>{
239+
// Handle eventual circular references when serializing.
240+
const nodeContent = JSON.stringify(kcArtifact, (key, value) => {
261241
if (typeof value === `object` && value !== null) {
262242
if (processedProperties.indexOf(value) !== -1) {
263243
try {
264244
return JSON.parse(JSON.stringify(value));
265245
} catch (error) {
266-
return;
246+
return null;
267247
}
268248
}
249+
269250
processedProperties.push(value);
270251
}
271252
return value;
@@ -317,32 +298,88 @@ const addLinkedItemsLinks = (itemNode, linkedNodes, linkPropertyName) => {
317298
}
318299
};
319300

320-
321-
const prefixImagesInRichText = (richTextPropertyValue) => {
301+
const prefixGuidNamedProperties = (propertyValue) => {
322302
const imagesIdentifier = `images`;
323-
const prefixLiteral = `image-`;
303+
const imagePrefixLiteral = `image-`;
304+
const linksIdentifier = `links`;
305+
const linkPrefixLiteral = `link-`;
324306
let transformedPropertyValue = {};
325307

326308
Object
327-
.keys(richTextPropertyValue)
328-
.filter((key) => key !== imagesIdentifier)
309+
.keys(propertyValue)
310+
.filter((key) => key !== imagesIdentifier && key !== linksIdentifier)
329311
.forEach((key) => {
330-
transformedPropertyValue[key] = richTextPropertyValue[key];
312+
transformedPropertyValue[key] = propertyValue[key];
331313
});
332314

333-
let transformedImagesProperty = {};
315+
transformedPropertyValue[imagesIdentifier] =
316+
prefixProperty(propertyValue, imagesIdentifier, imagePrefixLiteral);
317+
transformedPropertyValue[linksIdentifier] =
318+
prefixProperty(propertyValue, linksIdentifier, linkPrefixLiteral);
319+
320+
return transformedPropertyValue;
321+
};
322+
323+
const prefixProperty = (propertyValue, identifier, prefixLiteral) => {
324+
let transformedProperty = {};
334325

335326
Object
336-
.keys(richTextPropertyValue[imagesIdentifier])
327+
.keys(propertyValue[identifier])
337328
.forEach((key) => {
338329
const prefixedKey = prefixLiteral + key;
339-
transformedImagesProperty[prefixedKey] =
340-
richTextPropertyValue[imagesIdentifier][key];
330+
transformedProperty[prefixedKey] =
331+
propertyValue[identifier][key];
341332
});
342333

343-
transformedPropertyValue[imagesIdentifier] = transformedImagesProperty;
334+
return transformedProperty;
335+
};
344336

345-
return transformedPropertyValue;
337+
const parseContentItemContents = (contentItem, processedContents = []) => {
338+
if (processedContents.indexOf(contentItem.system) !== -1) {
339+
return null;
340+
} else {
341+
processedContents.push(contentItem.system);
342+
const elements = {};
343+
344+
Object
345+
.keys(contentItem)
346+
.filter((key) => key !== `system` && key !== `elements`)
347+
.forEach((key) => {
348+
let propertyValue;
349+
350+
if (_.has(contentItem[key], `type`)
351+
&& contentItem[key].type === `rich_text`) {
352+
if ((_.has(contentItem.elements[key], `images`)
353+
&& !_.isEmpty(contentItem.elements[key].images))
354+
|| (_.has(contentItem.elements[key], `links`)
355+
&& !_.isEmpty(contentItem.elements[key].links))) {
356+
propertyValue =
357+
prefixGuidNamedProperties(contentItem.elements[key]);
358+
}
359+
} else if (contentItem.elements[key].type === `modular_content`
360+
&& !_.isEmpty(contentItem[key])) {
361+
let linkedItems = [];
362+
363+
contentItem[key].forEach((linkedItem) => {
364+
linkedItems.push(
365+
parseContentItemContents(linkedItem, processedContents));
366+
});
367+
368+
propertyValue = linkedItems;
369+
} else {
370+
propertyValue = contentItem[key];
371+
}
372+
373+
elements[key] = propertyValue;
374+
});
375+
376+
const itemWithElements = {
377+
system: contentItem.system,
378+
elements: elements,
379+
};
380+
381+
return itemWithElements;
382+
}
346383
};
347384

348385
exports.createContentTypeNode = createContentTypeNode;

0 commit comments

Comments
 (0)